Prefer valueOf() to create Number values

Update Long/Integer constructor calls with `valueOf` which can make use
of global caches.

Closes gh-4688
pull/4674/merge
mnhock 9 years ago committed by Phillip Webb
parent dc3ead4c39
commit fcf6e5d6eb

@ -92,7 +92,7 @@ public class Metric<T extends Number> {
*/ */
public Metric<Long> increment(int amount) { public Metric<Long> increment(int amount) {
return new Metric<Long>(this.getName(), return new Metric<Long>(this.getName(),
new Long(this.getValue().longValue() + amount)); Long.valueOf(this.getValue().longValue() + amount));
} }
/** /**

@ -57,7 +57,7 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe
metric.increment(amount).getValue(), timestamp); metric.increment(amount).getValue(), timestamp);
} }
else { else {
return new Metric<Long>(metricName, new Long(amount), timestamp); return new Metric<Long>(metricName, Long.valueOf(amount), timestamp);
} }
} }
}); });

@ -64,12 +64,12 @@ public class MetricRegistryMetricReaderTests {
@Override @Override
public Number getValue() { public Number getValue() {
return new Integer(5); return Integer.valueOf(5);
} }
}); });
Metric<Integer> metric = (Metric<Integer>) this.metricReader.findOne("test"); Metric<Integer> metric = (Metric<Integer>) this.metricReader.findOne("test");
assertThat(metric.getValue(), equalTo(new Integer(5))); assertThat(metric.getValue(), equalTo(Integer.valueOf(5)));
this.metricRegistry.remove("test"); this.metricRegistry.remove("test");
assertThat(this.metricReader.findOne("test"), is(nullValue())); assertThat(this.metricReader.findOne("test"), is(nullValue()));
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -127,7 +127,7 @@ public class InMemoryRepositoryTests {
for (Future<Boolean> future : all) { for (Future<Boolean> future : all) {
assertTrue(future.get(1, TimeUnit.SECONDS)); assertTrue(future.get(1, TimeUnit.SECONDS));
} }
assertEquals(new Integer(0), repository.findOne("foo")); assertEquals(Integer.valueOf(0), repository.findOne("foo"));
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2014 the original author or authors. * Copyright 2013-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -100,7 +100,7 @@ public class DataSourceInitializerTests {
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
assertNotNull(dataSource); assertNotNull(dataSource);
JdbcOperations template = new JdbcTemplate(dataSource); JdbcOperations template = new JdbcTemplate(dataSource);
assertEquals(new Integer(1), assertEquals(Integer.valueOf(1),
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); template.queryForObject("SELECT COUNT(*) from BAR", Integer.class));
} }
@ -119,7 +119,7 @@ public class DataSourceInitializerTests {
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
assertNotNull(dataSource); assertNotNull(dataSource);
JdbcOperations template = new JdbcTemplate(dataSource); JdbcOperations template = new JdbcTemplate(dataSource);
assertEquals(new Integer(1), assertEquals(Integer.valueOf(1),
template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); template.queryForObject("SELECT COUNT(*) from FOO", Integer.class));
} }
@ -142,9 +142,9 @@ public class DataSourceInitializerTests {
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
assertNotNull(dataSource); assertNotNull(dataSource);
JdbcOperations template = new JdbcTemplate(dataSource); JdbcOperations template = new JdbcTemplate(dataSource);
assertEquals(new Integer(1), assertEquals(Integer.valueOf(1),
template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); template.queryForObject("SELECT COUNT(*) from FOO", Integer.class));
assertEquals(new Integer(0), assertEquals(Integer.valueOf(0),
template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)); template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class));
} }
@ -165,7 +165,7 @@ public class DataSourceInitializerTests {
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
assertNotNull(dataSource); assertNotNull(dataSource);
JdbcOperations template = new JdbcTemplate(dataSource); JdbcOperations template = new JdbcTemplate(dataSource);
assertEquals(new Integer(2), assertEquals(Integer.valueOf(2),
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); template.queryForObject("SELECT COUNT(*) from BAR", Integer.class));
assertEquals("bar", assertEquals("bar",
template.queryForObject("SELECT name from BAR WHERE id=1", String.class)); template.queryForObject("SELECT name from BAR WHERE id=1", String.class));

@ -67,7 +67,7 @@ public class HibernateJpaAutoConfigurationTests
"spring.datasource.schema:classpath:/ddl.sql"); "spring.datasource.schema:classpath:/ddl.sql");
setupTestConfiguration(); setupTestConfiguration();
this.context.refresh(); this.context.refresh();
assertEquals(new Integer(1), assertEquals(Integer.valueOf(1),
new JdbcTemplate(this.context.getBean(DataSource.class)) new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForObject("SELECT COUNT(*) from CITY", Integer.class)); .queryForObject("SELECT COUNT(*) from CITY", Integer.class));
} }
@ -80,7 +80,7 @@ public class HibernateJpaAutoConfigurationTests
"spring.datasource.data:classpath:/city.sql"); "spring.datasource.data:classpath:/city.sql");
setupTestConfiguration(); setupTestConfiguration();
this.context.refresh(); this.context.refresh();
assertEquals(new Integer(1), assertEquals(Integer.valueOf(1),
new JdbcTemplate(this.context.getBean(DataSource.class)) new JdbcTemplate(this.context.getBean(DataSource.class))
.queryForObject("SELECT COUNT(*) from CITY", Integer.class)); .queryForObject("SELECT COUNT(*) from CITY", Integer.class));
} }

Loading…
Cancel
Save