From fcf6e5d6eb7fe877b8dfe3c6a4fa5b2600887259 Mon Sep 17 00:00:00 2001 From: mnhock Date: Sun, 6 Dec 2015 15:01:07 +0100 Subject: [PATCH] Prefer valueOf() to create Number values Update Long/Integer constructor calls with `valueOf` which can make use of global caches. Closes gh-4688 --- .../springframework/boot/actuate/metrics/Metric.java | 2 +- .../metrics/repository/InMemoryMetricRepository.java | 2 +- .../reader/MetricRegistryMetricReaderTests.java | 4 ++-- .../metrics/util/InMemoryRepositoryTests.java | 4 ++-- .../jdbc/DataSourceInitializerTests.java | 12 ++++++------ .../orm/jpa/HibernateJpaAutoConfigurationTests.java | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/Metric.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/Metric.java index f8d71c61fb..f9b8421aa8 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/Metric.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/Metric.java @@ -92,7 +92,7 @@ public class Metric { */ public Metric increment(int amount) { return new Metric(this.getName(), - new Long(this.getValue().longValue() + amount)); + Long.valueOf(this.getValue().longValue() + amount)); } /** diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/InMemoryMetricRepository.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/InMemoryMetricRepository.java index 14df26ff6c..ecd3a48156 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/InMemoryMetricRepository.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/repository/InMemoryMetricRepository.java @@ -57,7 +57,7 @@ public class InMemoryMetricRepository implements MetricRepository, MultiMetricRe metric.increment(amount).getValue(), timestamp); } else { - return new Metric(metricName, new Long(amount), timestamp); + return new Metric(metricName, Long.valueOf(amount), timestamp); } } }); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java index ddd5b3d6ab..9caadbd029 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java @@ -64,12 +64,12 @@ public class MetricRegistryMetricReaderTests { @Override public Number getValue() { - return new Integer(5); + return Integer.valueOf(5); } }); Metric metric = (Metric) this.metricReader.findOne("test"); - assertThat(metric.getValue(), equalTo(new Integer(5))); + assertThat(metric.getValue(), equalTo(Integer.valueOf(5))); this.metricRegistry.remove("test"); assertThat(this.metricReader.findOne("test"), is(nullValue())); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/util/InMemoryRepositoryTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/util/InMemoryRepositoryTests.java index 4e1d9143c4..c551356044 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/util/InMemoryRepositoryTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/util/InMemoryRepositoryTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -127,7 +127,7 @@ public class InMemoryRepositoryTests { for (Future future : all) { assertTrue(future.get(1, TimeUnit.SECONDS)); } - assertEquals(new Integer(0), repository.findOne("foo")); + assertEquals(Integer.valueOf(0), repository.findOne("foo")); } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java index b7d6c382bc..cbf7ac033d 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java @@ -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"); * 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); assertNotNull(dataSource); JdbcOperations template = new JdbcTemplate(dataSource); - assertEquals(new Integer(1), + assertEquals(Integer.valueOf(1), template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); } @@ -119,7 +119,7 @@ public class DataSourceInitializerTests { assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertNotNull(dataSource); JdbcOperations template = new JdbcTemplate(dataSource); - assertEquals(new Integer(1), + assertEquals(Integer.valueOf(1), template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); } @@ -142,9 +142,9 @@ public class DataSourceInitializerTests { assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertNotNull(dataSource); JdbcOperations template = new JdbcTemplate(dataSource); - assertEquals(new Integer(1), + assertEquals(Integer.valueOf(1), template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); - assertEquals(new Integer(0), + assertEquals(Integer.valueOf(0), template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)); } @@ -165,7 +165,7 @@ public class DataSourceInitializerTests { assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertNotNull(dataSource); JdbcOperations template = new JdbcTemplate(dataSource); - assertEquals(new Integer(2), + assertEquals(Integer.valueOf(2), template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); assertEquals("bar", template.queryForObject("SELECT name from BAR WHERE id=1", String.class)); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java index 3606cf77e9..7d4fee64f5 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java @@ -67,7 +67,7 @@ public class HibernateJpaAutoConfigurationTests "spring.datasource.schema:classpath:/ddl.sql"); setupTestConfiguration(); this.context.refresh(); - assertEquals(new Integer(1), + assertEquals(Integer.valueOf(1), new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForObject("SELECT COUNT(*) from CITY", Integer.class)); } @@ -80,7 +80,7 @@ public class HibernateJpaAutoConfigurationTests "spring.datasource.data:classpath:/city.sql"); setupTestConfiguration(); this.context.refresh(); - assertEquals(new Integer(1), + assertEquals(Integer.valueOf(1), new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForObject("SELECT COUNT(*) from CITY", Integer.class)); }