Make fields immutable (final) where possible

This commit is contained in:
René Scheibe
2022-06-08 16:13:03 +02:00
committed by GitHub
parent 9f4428153c
commit 80af14a2e6
40 changed files with 80 additions and 84 deletions

View File

@@ -25,11 +25,9 @@ public class Delivery {
private static final String SEPARATOR = "-----------------------";
private final List<Drink> deliveredDrinks;
private List<Drink> deliveredDrinks;
private int orderNumber;
private final int orderNumber;
public Delivery(List<Drink> deliveredDrinks) {
assert(deliveredDrinks.size() > 0);
@@ -37,7 +35,6 @@ public class Delivery {
this.orderNumber = deliveredDrinks.get(0).getOrderNumber();
}
public int getOrderNumber() {
return orderNumber;
}

View File

@@ -21,15 +21,14 @@ package org.springframework.integration.samples.cafe;
*/
public class Drink {
private boolean iced;
private final boolean iced;
private int shots;
private final int shots;
private DrinkType drinkType;
private final DrinkType drinkType;
private int orderNumber;
private final int orderNumber;
public Drink(int orderNumber, DrinkType drinkType, boolean hot, int shots) {
this.orderNumber = orderNumber;
this.drinkType = drinkType;
@@ -37,7 +36,6 @@ public class Drink {
this.shots = shots;
}
public int getOrderNumber() {
return orderNumber;
}

View File

@@ -25,9 +25,9 @@ import java.util.List;
*/
public class Order {
private List<OrderItem> orderItems = new ArrayList<OrderItem>();
private final List<OrderItem> orderItems = new ArrayList<OrderItem>();
private int number;
private final int number;
public Order(int number) {
this.number = number;
@@ -44,7 +44,7 @@ public class Order {
public List<OrderItem> getItems() {
return this.orderItems;
}
public String toString() {
return "Order number " + number;
}

View File

@@ -31,7 +31,7 @@ import org.springframework.jmx.export.annotation.ManagedResource;
@ManagedResource
public class Waiter {
private AtomicInteger totalDeliveries = new AtomicInteger();
private final AtomicInteger totalDeliveries = new AtomicInteger();
public Delivery prepareDelivery(List<Drink> drinks) {
totalDeliveries.getAndIncrement();

View File

@@ -30,7 +30,7 @@ import org.springframework.integration.samples.cafe.WaiterMonitor;
*
*/
public class ControlBusMain {
private static Log logger = LogFactory.getLog(ControlBusMain.class);
private static final Log logger = LogFactory.getLog(ControlBusMain.class);
public static void main(String[] args) {

View File

@@ -36,7 +36,7 @@ import org.springframework.stereotype.Component;
@Component
public class Barista {
private static Log logger = LogFactory.getLog(Barista.class);
private static final Log logger = LogFactory.getLog(Barista.class);
private long hotDrinkDelay = 5000;

View File

@@ -33,7 +33,7 @@ import org.springframework.integration.samples.cafe.OrderItem;
public class Barista {
private static Log logger = LogFactory.getLog(Barista.class);
private static final Log logger = LogFactory.getLog(Barista.class);
private long hotDrinkDelay = 5000;

View File

@@ -33,7 +33,7 @@ import org.springframework.integration.samples.loanbroker.domain.LoanRequest;
*/
public class LoanBrokerDemo {
private static Log logger = LogFactory.getLog(LoanBrokerDemo.class);
private static final Log logger = LogFactory.getLog(LoanBrokerDemo.class);
public static void main(String[] args) {
new LoanBrokerDemo().runDemo();

View File

@@ -33,7 +33,7 @@ import org.springframework.integration.samples.loanbroker.domain.LoanRequest;
*/
public class LoanBrokerSharkDetectorDemo {
private static Log logger = LogFactory.getLog(LoanBrokerSharkDetectorDemo.class);
private static final Log logger = LogFactory.getLog(LoanBrokerSharkDetectorDemo.class);
public static void main(String[] args) {
ConfigurableApplicationContext context =

View File

@@ -30,7 +30,7 @@ import org.springframework.integration.samples.loanbroker.domain.LoanRequest;
*/
public class CreditBureauStub {
private static Log logger = LogFactory.getLog(CreditBureauStub.class);
private static final Log logger = LogFactory.getLog(CreditBureauStub.class);
/**
* @param loanRequest the loan request

View File

@@ -36,8 +36,8 @@ import org.apache.ftpserver.usermanager.impl.WritePermission;
*
*/
public class TestUserManager extends AbstractUserManager {
private BaseUser testUser;
private BaseUser anonUser;
private final BaseUser testUser;
private final BaseUser anonUser;
private static final String TEST_USERNAME = "demo";
private static final String TEST_PASSWORD = "demo";

View File

@@ -42,7 +42,7 @@ import org.springframework.messaging.support.GenericMessage;
*/
public class HelloWorldApp {
private static Log logger = LogFactory.getLog(HelloWorldApp.class);
private static final Log logger = LogFactory.getLog(HelloWorldApp.class);
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class);

View File

@@ -27,7 +27,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
*/
public class HttpClientDemo {
private static Log logger = LogFactory.getLog(HttpClientDemo.class);
private static final Log logger = LogFactory.getLog(HttpClientDemo.class);
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(

View File

@@ -27,9 +27,9 @@ import java.util.Map;
public enum Gender {
MALE("M"),FEMALE("F");
private static Map<String, Gender> map;
private String identifier;
private static final Map<String, Gender> map;
private final String identifier;
private Gender(String identifier) {
this.identifier = identifier;
@@ -37,12 +37,12 @@ public enum Gender {
public String getIdentifier() {
return identifier;
}
}
public static Gender getGenderByIdentifier(String identifier) {
return map.get(identifier);
}
static {
map = new HashMap<String, Gender>();
for(Gender gender:EnumSet.allOf(Gender.class)) {

View File

@@ -13,9 +13,9 @@
package org.springframework.integration.samples.jdbc.domain;
public class User {
private String username;
private String password;
private String email;
private final String username;
private final String password;
private final String email;
public User(String username, String password, String email) {
super();

View File

@@ -30,7 +30,7 @@ import org.springframework.messaging.MessagingException;
*
*/
public class GmailInboundImapIdleAdapterTestApp {
private static Log logger = LogFactory.getLog(GmailInboundImapIdleAdapterTestApp.class);
private static final Log logger = LogFactory.getLog(GmailInboundImapIdleAdapterTestApp.class);
public static void main (String[] args) throws Exception {

View File

@@ -32,7 +32,7 @@ import org.springframework.messaging.MessagingException;
*/
public class GmailInboundPop3AdapterTestApp {
private static Log logger = LogFactory.getLog(GmailInboundPop3AdapterTestApp.class);
private static final Log logger = LogFactory.getLog(GmailInboundPop3AdapterTestApp.class);
public static void main (String[] args) throws Exception {
@SuppressWarnings("resource")

View File

@@ -40,10 +40,10 @@ import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
*/
public class BrokerRunning extends TestWatcher {
private static Log logger = LogFactory.getLog(BrokerRunning.class);
private static final Log logger = LogFactory.getLog(BrokerRunning.class);
// Static so that we only test once on failure: speeds up test suite
private static Map<Integer, Boolean> brokerOnline = new HashMap<>();
private static final Map<Integer, Boolean> brokerOnline = new HashMap<>();
private final int port;

View File

@@ -34,7 +34,7 @@ import org.springframework.integration.annotation.ServiceActivator;
*/
@MessageEndpoint
public class EvenLogger {
private static Log logger = LogFactory.getLog(EvenLogger.class);
private static final Log logger = LogFactory.getLog(EvenLogger.class);
@ServiceActivator
public void log(int i) {

View File

@@ -34,7 +34,7 @@ import org.springframework.integration.annotation.ServiceActivator;
*/
@MessageEndpoint
public class OddLogger {
private static Log logger = LogFactory.getLog(OddLogger.class);
private static final Log logger = LogFactory.getLog(OddLogger.class);
@ServiceActivator
public void log(int i) {

View File

@@ -23,9 +23,9 @@ import java.math.BigDecimal;
*/
public class Quote {
private String ticker;
private final String ticker;
private BigDecimal price;
private final BigDecimal price;
public Quote(String ticker, BigDecimal price) {
this.ticker = ticker;

View File

@@ -25,9 +25,9 @@ import org.apache.commons.lang3.builder.ToStringStyle;
*/
public class CustomOrder {
private int number;
private final int number;
private String sender;
private final String sender;
private String message;

View File

@@ -25,16 +25,16 @@ import java.util.Map;
*/
public class Traffic {
private Map<String, String> incidents = new HashMap<String, String>();
private final Map<String, String> incidents = new HashMap<String, String>();
public void addIncident(String title, String description){
incidents.put(title, description);
}
public Map<String, String> getIncidents(){
return incidents;
}
public String toString(){
return "Traffic: {" + incidents.keySet().toString() + "}";
}

View File

@@ -40,8 +40,8 @@ import org.w3c.dom.Node;
* @since SpringOne2GX - 2010, Chicago
*/
public class TrafficHttpConverter implements HttpMessageConverter<Traffic> {
private List<MediaType> supportedMediaTypes = Collections.emptyList();
private final List<MediaType> supportedMediaTypes = Collections.emptyList();
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return Traffic.class.equals(clazz);
}

View File

@@ -47,9 +47,9 @@ public class WeatherMarshaller implements Marshaller, Unmarshaller, Initializing
private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
private Map<String, String> namespacePrefixes = new HashMap<String, String>();
private final Map<String, String> namespacePrefixes = new HashMap<String, String>();
private String xpathPrefix = "/p:GetCityWeatherByZIPResponse/p:GetCityWeatherByZIPResult/";
private static final String XPATH_PREFIX = "/p:GetCityWeatherByZIPResponse/p:GetCityWeatherByZIPResult/";
public Object unmarshal(Source source) throws IOException, XmlMappingException {
@@ -63,16 +63,16 @@ public class WeatherMarshaller implements Marshaller, Unmarshaller, Initializing
throw new MarshallingFailureException("Failed to unmarshal SOAP Response", e);
}
Weather weather = new Weather();
String expression = xpathPrefix + "p:City";
String expression = XPATH_PREFIX + "p:City";
String city = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
weather.setCity(city);
expression = xpathPrefix + "p:State";
expression = XPATH_PREFIX + "p:State";
String state = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
weather.setState(state);
expression = xpathPrefix + "p:Temperature";
expression = XPATH_PREFIX + "p:Temperature";
String temperature = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
weather.setTemperature(temperature);
expression = xpathPrefix + "p:Description";
expression = XPATH_PREFIX + "p:Description";
String description = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
weather.setDescription(description);
return weather;

View File

@@ -41,7 +41,7 @@ import org.springframework.xml.transform.StringSource;
*/
public class InContainerTests {
private static Log logger = LogFactory.getLog(InContainerTests.class);
private static final Log logger = LogFactory.getLog(InContainerTests.class);
private static final String WS_URI = "http://localhost:8080/ws-inbound-gateway/echoservice";
private final WebServiceTemplate template = new WebServiceTemplate();

View File

@@ -25,7 +25,7 @@ import org.w3c.dom.Document;
* @author Gary Russell
*/
public class ExternalResupply {
private static Log logger = LogFactory.getLog(ExternalResupply.class);
private static final Log logger = LogFactory.getLog(ExternalResupply.class);
public void orderResupply(Document resupplyOrder) {
logger.info("Placing resupply order: \n" + XmlUtil.docAsString(resupplyOrder));

View File

@@ -25,7 +25,7 @@ import org.w3c.dom.Document;
* @author Gary Russell
*/
public class WarehouseDispatch {
private static Log logger = LogFactory.getLog(WarehouseDispatch.class);
private static final Log logger = LogFactory.getLog(WarehouseDispatch.class);
public void dispatch(Document orderItem){
logger.info("Warehouse dispatching orderItem: \n" + XmlUtil.docAsString(orderItem));

View File

@@ -68,9 +68,9 @@ public class Application {
}
private AtomicInteger hotDrinkCounter = new AtomicInteger();
private final AtomicInteger hotDrinkCounter = new AtomicInteger();
private AtomicInteger coldDrinkCounter = new AtomicInteger();
private final AtomicInteger coldDrinkCounter = new AtomicInteger();
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {

View File

@@ -59,7 +59,7 @@ import org.springframework.util.concurrent.ListenableFutureCallback;
@DirtiesContext
public class ListenableFutureTest {
private static Log logger = LogFactory.getLog(ListenableFutureTest.class);
private static final Log logger = LogFactory.getLog(ListenableFutureTest.class);
@Autowired
private MathGateway gateway;

View File

@@ -58,7 +58,7 @@ import reactor.core.scheduler.Schedulers;
@DirtiesContext
public class MonoGatewayTests {
private static Log logger = LogFactory.getLog(MonoGatewayTests.class);
private static final Log logger = LogFactory.getLog(MonoGatewayTests.class);
@Autowired
private MathGateway gateway;

View File

@@ -32,11 +32,11 @@ import org.springframework.util.Assert;
*/
public class EmailFragment {
private Object data;
private final Object data;
private String filename;
private final String filename;
private File directory;
private final File directory;
/**
* Constructor.

View File

@@ -44,7 +44,7 @@ import org.springframework.stereotype.Service;
public class DefaultTwitterService implements TwitterService {
/** Holds a collection of polled Twitter messages */
private Map<Long, TwitterMessage> twitterMessages;
private final Map<Long, TwitterMessage> twitterMessages;
@Autowired
private MessageChannel controlBusChannel;

View File

@@ -28,7 +28,7 @@ import org.springframework.util.LinkedMultiValueMap;
*
*/
public class MultipartReceiver {
private static Log logger = LogFactory.getLog(MultipartReceiver.class);
private static final Log logger = LogFactory.getLog(MultipartReceiver.class);
@SuppressWarnings("rawtypes")
public void receive(LinkedMultiValueMap<String, Object> multipartRequest){

View File

@@ -32,13 +32,13 @@ import org.springframework.http.HttpStatus;
*/
public class MultipartClientForHttpOutboundClient {
private static Log logger = LogFactory.getLog(MultipartClientForHttpOutboundClient.class);
private static String resourcePath = "org/springframework/integration/samples/multipart/spring09_logo.png";
private static final Log logger = LogFactory.getLog(MultipartClientForHttpOutboundClient.class);
private static final String RESOURCE_PATH = "org/springframework/integration/samples/multipart/spring09_logo.png";
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"/META-INF/spring/integration/http-outbound-config.xml");
Resource s2logo = new ClassPathResource(resourcePath);
Resource s2logo = new ClassPathResource(RESOURCE_PATH);
Map<String, Object> multipartMap = new HashMap<String, Object>();
multipartMap.put("company", new String[]{"SpringSource", "VMWare"});
multipartMap.put("company-logo", s2logo);

View File

@@ -36,15 +36,15 @@ import org.springframework.web.client.RestTemplate;
*/
public class MultipartRestClient {
private static Log logger = LogFactory.getLog(MultipartRestClient.class);
private static final Log logger = LogFactory.getLog(MultipartRestClient.class);
private static String uri = "http://localhost:8080/multipart-http/inboundAdapter.htm";
private static final String URI = "http://localhost:8080/multipart-http/inboundAdapter.htm";
private static String resourcePath = "org/springframework/integration/samples/multipart/spring09_logo.png";
private static final String RESOURCE_PATH = "org/springframework/integration/samples/multipart/spring09_logo.png";
public static void main(String[] args) throws Exception{
RestTemplate template = new RestTemplate();
Resource s2logo = new ClassPathResource(resourcePath);
Resource s2logo = new ClassPathResource(RESOURCE_PATH);
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("company", "SpringSource");
multipartMap.add("company-logo", s2logo);
@@ -52,8 +52,8 @@ public class MultipartRestClient {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("multipart", "form-data"));
HttpEntity<Object> request = new HttpEntity<Object>(multipartMap, headers);
logger.info("Posting request to: " + uri);
ResponseEntity<?> httpResponse = template.exchange(uri, HttpMethod.POST, request, Object.class);
logger.info("Posting request to: " + URI);
ResponseEntity<?> httpResponse = template.exchange(URI, HttpMethod.POST, request, Object.class);
if (!httpResponse.getStatusCode().equals(HttpStatus.OK)){
logger.error("Problems with the request. Http status: " + httpResponse.getStatusCode());
}

View File

@@ -37,7 +37,8 @@ import org.springframework.stereotype.Service;
@Service("employeeSearchService")
public class EmployeeSearchService {
private static Log logger = LogFactory.getLog(EmployeeSearchService.class);
private static final Log logger = LogFactory.getLog(EmployeeSearchService.class);
/**
* The API <code>getEmployee()</code> looks up the mapped in coming message header's id param
* and fills the return object with the appropriate employee details. The return

View File

@@ -68,7 +68,7 @@ public class RestHttpClientTest {
private HttpMessageConverterExtractor<EmployeeList> responseExtractor;
private static Log logger = LogFactory.getLog(RestHttpClientTest.class);
private static final Log logger = LogFactory.getLog(RestHttpClientTest.class);
@Autowired
private Jaxb2Marshaller marshaller;

View File

@@ -25,7 +25,7 @@ import java.util.Collection;
*
*/
public class CompositeResult extends Result {
private Collection<Result> results = new ArrayList<Result>();
private final Collection<Result> results = new ArrayList<Result>();
public Collection<Result> getResults() {
return results;

View File

@@ -31,10 +31,10 @@ public enum City {
BOSTON(2, "Boston", "02201", "42.636182,-71.651862,42.080413,-70.467446"),
SAN_FRANCISCO(3, "San Francisco", "94102", "38.052886,-123.009856,37.497117,-121.82544");
private Integer id;
private String name;
private String boundingBox;
private String postalCode;
private final Integer id;
private final String name;
private final String boundingBox;
private final String postalCode;
private City(Integer id, String name, String postalCode, String boundingBox) {
this.id = id;