Code cleanup (#1461)
* Code cleanup * Fix flux tests * Fix checkstyle errors * Fix compile error
This commit is contained in:
@ -1,93 +1,93 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.IntStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* DateFormatCallable converts string dates to a date format using SimpleDateFormat. The date format
|
||||
* and the date value will be passed to the Callable by the constructor. The constructor creates a
|
||||
* instance of SimpleDateFormat and stores it in a ThreadLocal class variable. For the complete
|
||||
* description of the example see {@link App}.
|
||||
*
|
||||
* <p>You can comment out the code marked with //TLTL and comment in the code marked //NTLNTL. Then
|
||||
* you can see what will happen if you do not use the ThreadLocal. For details see the description
|
||||
* of {@link App}
|
||||
*
|
||||
* @author Thomas Bauer, 2017
|
||||
*/
|
||||
public class DateFormatCallable implements Callable<Result> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatCallable.class);
|
||||
// class variables (members)
|
||||
private ThreadLocal<DateFormat> df; //TLTL
|
||||
// private DateFormat df; //NTLNTL
|
||||
|
||||
private String dateValue; // for dateValue Thread Local not needed
|
||||
|
||||
|
||||
/**
|
||||
* The date format and the date value are passed to the constructor.
|
||||
*
|
||||
* @param inDateFormat string date format string, e.g. "dd/MM/yyyy"
|
||||
* @param inDateValue string date value, e.g. "21/06/2016"
|
||||
*/
|
||||
public DateFormatCallable(String inDateFormat, String inDateValue) {
|
||||
final var idf = inDateFormat; //TLTL
|
||||
this.df = ThreadLocal.withInitial(() -> { //TLTL
|
||||
return new SimpleDateFormat(idf); //TLTL
|
||||
}); //TLTL
|
||||
// this.df = new SimpleDateFormat(inDateFormat); //NTLNTL
|
||||
this.dateValue = inDateValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result call() {
|
||||
LOGGER.info(Thread.currentThread() + " started executing...");
|
||||
var result = new Result();
|
||||
|
||||
// Convert date value to date 5 times
|
||||
IntStream.rangeClosed(1, 5).forEach(i -> {
|
||||
try {
|
||||
// this is the statement where it is important to have the
|
||||
// instance of SimpleDateFormat locally
|
||||
// Create the date value and store it in dateList
|
||||
result.getDateList().add(this.df.get().parse(this.dateValue)); //TLTL
|
||||
// result.getDateList().add(this.df.parse(this.dateValue)); //NTLNTL
|
||||
} catch (Exception e) {
|
||||
// write the Exception to a list and continue work
|
||||
result.getExceptionList().add(e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
LOGGER.info(Thread.currentThread() + " finished processing part of the thread");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.IntStream;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* DateFormatCallable converts string dates to a date format using SimpleDateFormat. The date format
|
||||
* and the date value will be passed to the Callable by the constructor. The constructor creates a
|
||||
* instance of SimpleDateFormat and stores it in a ThreadLocal class variable. For the complete
|
||||
* description of the example see {@link App}.
|
||||
*
|
||||
* <p>You can comment out the code marked with //TLTL and comment in the code marked //NTLNTL. Then
|
||||
* you can see what will happen if you do not use the ThreadLocal. For details see the description
|
||||
* of {@link App}
|
||||
*
|
||||
* @author Thomas Bauer, 2017
|
||||
*/
|
||||
public class DateFormatCallable implements Callable<Result> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatCallable.class);
|
||||
// class variables (members)
|
||||
private final ThreadLocal<DateFormat> df; //TLTL
|
||||
// private DateFormat df; //NTLNTL
|
||||
|
||||
private final String dateValue; // for dateValue Thread Local not needed
|
||||
|
||||
|
||||
/**
|
||||
* The date format and the date value are passed to the constructor.
|
||||
*
|
||||
* @param inDateFormat string date format string, e.g. "dd/MM/yyyy"
|
||||
* @param inDateValue string date value, e.g. "21/06/2016"
|
||||
*/
|
||||
public DateFormatCallable(String inDateFormat, String inDateValue) {
|
||||
final var idf = inDateFormat; //TLTL
|
||||
this.df = ThreadLocal.withInitial(() -> { //TLTL
|
||||
return new SimpleDateFormat(idf); //TLTL
|
||||
}); //TLTL
|
||||
// this.df = new SimpleDateFormat(inDateFormat); //NTLNTL
|
||||
this.dateValue = inDateValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result call() {
|
||||
LOGGER.info(Thread.currentThread() + " started executing...");
|
||||
var result = new Result();
|
||||
|
||||
// Convert date value to date 5 times
|
||||
IntStream.rangeClosed(1, 5).forEach(i -> {
|
||||
try {
|
||||
// this is the statement where it is important to have the
|
||||
// instance of SimpleDateFormat locally
|
||||
// Create the date value and store it in dateList
|
||||
result.getDateList().add(this.df.get().parse(this.dateValue)); //TLTL
|
||||
// result.getDateList().add(this.df.parse(this.dateValue)); //NTLNTL
|
||||
} catch (Exception e) {
|
||||
// write the Exception to a list and continue work
|
||||
result.getExceptionList().add(e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
LOGGER.info(Thread.currentThread() + " finished processing part of the thread");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,65 +1,65 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Fiducia IT AG, All rights reserved. Use is subject to license terms.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Result object that will be returned by the Callable {@link DateFormatCallable} used in {@link
|
||||
* App}.
|
||||
*
|
||||
* @author Thomas Bauer, 2017
|
||||
*/
|
||||
public class Result {
|
||||
// A list to collect the date values created in one thread
|
||||
private List<Date> dateList = new ArrayList<>();
|
||||
|
||||
// A list to collect Exceptions thrown in one threads (should be none in
|
||||
// this example)
|
||||
private List<String> exceptionList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Get list of date values collected within a thread execution.
|
||||
*
|
||||
* @return List of date values collected within an thread execution
|
||||
*/
|
||||
public List<Date> getDateList() {
|
||||
return dateList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of exceptions thrown within a thread execution.
|
||||
*
|
||||
* @return List of exceptions thrown within an thread execution
|
||||
*/
|
||||
public List<String> getExceptionList() {
|
||||
return exceptionList;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Fiducia IT AG, All rights reserved. Use is subject to license terms.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Result object that will be returned by the Callable {@link DateFormatCallable} used in {@link
|
||||
* App}.
|
||||
*
|
||||
* @author Thomas Bauer, 2017
|
||||
*/
|
||||
public class Result {
|
||||
// A list to collect the date values created in one thread
|
||||
private final List<Date> dateList = new ArrayList<>();
|
||||
|
||||
// A list to collect Exceptions thrown in one threads (should be none in
|
||||
// this example)
|
||||
private final List<String> exceptionList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Get list of date values collected within a thread execution.
|
||||
*
|
||||
* @return List of date values collected within an thread execution
|
||||
*/
|
||||
public List<Date> getDateList() {
|
||||
return dateList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of exceptions thrown within a thread execution.
|
||||
*
|
||||
* @return List of exceptions thrown within an thread execution
|
||||
*/
|
||||
public List<String> getExceptionList() {
|
||||
return exceptionList;
|
||||
}
|
||||
}
|
||||
|
@ -66,18 +66,18 @@ public class DateFormatCallableTest {
|
||||
/**
|
||||
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
private int expectedCounterDateValues = 5;
|
||||
private final int expectedCounterDateValues = 5;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
*/
|
||||
private int expectedCounterExceptions = 0;
|
||||
private final int expectedCounterExceptions = 0;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the date values created by the run of
|
||||
* DateFormatRunnalbe
|
||||
*/
|
||||
private List<String> expectedDateValues =
|
||||
private final List<String> expectedDateValues =
|
||||
List.of("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
|
||||
/**
|
||||
|
@ -54,18 +54,18 @@ public class DateFormatCallableTestIncorrectDateFormat {
|
||||
/**
|
||||
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
private int expectedCounterDateValues = 0;
|
||||
private final int expectedCounterDateValues = 0;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
*/
|
||||
private int expectedCounterExceptions = 5;
|
||||
private final int expectedCounterExceptions = 5;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the exceptions created by the run of
|
||||
* DateFormatRunnalbe
|
||||
*/
|
||||
private List<String> expectedExceptions = List.of(
|
||||
private final List<String> expectedExceptions = List.of(
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
|
@ -55,7 +55,7 @@ public class DateFormatCallableTestMultiThread {
|
||||
* Result object given back by DateFormatCallable, one for each thread -- Array with converted
|
||||
* date values -- Array with thrown exceptions
|
||||
*/
|
||||
private static Result[] result = new Result[4];
|
||||
private static final Result[] result = new Result[4];
|
||||
|
||||
/**
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup()
|
||||
@ -66,22 +66,22 @@ public class DateFormatCallableTestMultiThread {
|
||||
/* nothing needed here */
|
||||
}
|
||||
|
||||
private static List<String>[] createdDateValues = new StringArrayList[4];
|
||||
private static final List<String>[] createdDateValues = new StringArrayList[4];
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by each thread
|
||||
*/
|
||||
private int expectedCounterDateValues = 5;
|
||||
private final int expectedCounterDateValues = 5;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by each thread
|
||||
*/
|
||||
private int expectedCounterExceptions = 0;
|
||||
private final int expectedCounterExceptions = 0;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the date values created by each thread
|
||||
*/
|
||||
private List<String> expectedDateValues =
|
||||
private final List<String> expectedDateValues =
|
||||
List.of("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user