maven fix and small formatting work

This commit is contained in:
eugenp 2016-12-17 15:24:36 +02:00
parent b4ac6c6ec5
commit d66d9fb2c0
11 changed files with 449 additions and 481 deletions

View File

@ -106,6 +106,13 @@
<!-- test scoped --> <!-- test scoped -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -254,8 +261,7 @@
<configuration> <configuration>
<shadedArtifactAttached>true</shadedArtifactAttached> <shadedArtifactAttached>true</shadedArtifactAttached>
<transformers> <transformers>
<transformer <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass> <mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
</transformer> </transformer>
</transformers> </transformers>

View File

@ -5,8 +5,7 @@ public class SimulatedAnnealing {
private static Travel travel = new Travel(10); private static Travel travel = new Travel(10);
public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) { public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) {
System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: " + numberOfIterations + " and colling rate: " + coolingRate);
+ numberOfIterations + " and colling rate: " + coolingRate);
double t = startingTemperature; double t = startingTemperature;
travel.generateInitialTravel(); travel.generateInitialTravel();
double bestDistance = travel.getDistance(); double bestDistance = travel.getDistance();

View File

@ -22,7 +22,6 @@ public class EncoderDecoderUnitTest {
private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253"; private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253"; private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253";
private String encodeValue(String value) { private String encodeValue(String value) {
String encoded = null; String encoded = null;
try { try {
@ -59,9 +58,7 @@ public class EncoderDecoderUnitTest {
requestParams.put("key2", "value@!$2"); requestParams.put("key2", "value@!$2");
requestParams.put("key3", "value%3"); requestParams.put("key3", "value%3");
String encodedURL = requestParams.keySet().stream() String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", ""));
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
.collect(joining("&", "http://www.baeldung.com?", ""));
Assert.assertThat(testUrl, is(encodedURL)); Assert.assertThat(testUrl, is(encodedURL));
} }
@ -103,12 +100,9 @@ public class EncoderDecoderUnitTest {
String path = "path+1"; String path = "path+1";
String encodedURL = requestParams.keySet().stream() String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
.collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
Assert.assertThat(testUrlWithPath, is(encodedURL)); Assert.assertThat(testUrlWithPath, is(encodedURL));
} }
} }

View File

@ -17,9 +17,7 @@ public class Java8CollectionCleanupUnitTest {
@Test @Test
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() { public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null); final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
final List<Integer> listWithoutNulls = list.parallelStream() final List<Integer> listWithoutNulls = list.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
.filter(Objects::nonNull)
.collect(Collectors.toList());
assertThat(listWithoutNulls, hasSize(3)); assertThat(listWithoutNulls, hasSize(3));
} }
@ -27,9 +25,7 @@ public class Java8CollectionCleanupUnitTest {
@Test @Test
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() { public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null); final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
final List<Integer> listWithoutNulls = list.stream() final List<Integer> listWithoutNulls = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
.filter(Objects::nonNull)
.collect(Collectors.toList());
assertThat(listWithoutNulls, hasSize(3)); assertThat(listWithoutNulls, hasSize(3));
} }
@ -45,9 +41,7 @@ public class Java8CollectionCleanupUnitTest {
@Test @Test
public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() { public void givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
final List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3); final List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream() final List<Integer> listWithoutDuplicates = listWithDuplicates.parallelStream().distinct().collect(Collectors.toList());
.distinct()
.collect(Collectors.toList());
assertThat(listWithoutDuplicates, hasSize(3)); assertThat(listWithoutDuplicates, hasSize(3));
} }

View File

@ -93,6 +93,7 @@ public class OptionalTest {
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent(); boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
assertFalse(is2017); assertFalse(is2017);
} }
@Test @Test
public void whenFiltersWithoutOptional_thenCorrect() { public void whenFiltersWithoutOptional_thenCorrect() {
assertTrue(priceIsInRange1(new Modem(10.0))); assertTrue(priceIsInRange1(new Modem(10.0)));
@ -121,12 +122,9 @@ public class OptionalTest {
} }
public boolean priceIsInRange2(Modem modem2) { public boolean priceIsInRange2(Modem modem2) {
return Optional.ofNullable(modem2) return Optional.ofNullable(modem2).map(Modem::getPrice).filter(p -> p >= 10).filter(p -> p <= 15).isPresent();
.map(Modem::getPrice)
.filter(p -> p >= 10)
.filter(p -> p <= 15)
.isPresent();
} }
// Transforming Value With map() // Transforming Value With map()
@Test @Test
public void givenOptional_whenMapWorks_thenCorrect() { public void givenOptional_whenMapWorks_thenCorrect() {

View File

@ -38,20 +38,17 @@ public class GrepWithUnix4JTest {
int expectedLineCount = 178687; int expectedLineCount = 178687;
// grep -v "NINETEEN" dictionary.txt // grep -v "NINETEEN" dictionary.txt
List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep). List<Line> lines = grep(Options.v, "NINETEEN", fileToGrep).toLineList();
toLineList();
assertEquals(expectedLineCount, lines.size()); assertEquals(expectedLineCount, lines.size());
} }
@Test @Test
public void whenGrepWithRegex_thenCorrect() { public void whenGrepWithRegex_thenCorrect() {
int expectedLineCount = 151; int expectedLineCount = 151;
// grep -c ".*?NINE.*?" dictionary.txt // grep -c ".*?NINE.*?" dictionary.txt
String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep). String patternCount = grep(Options.c, ".*?NINE.*?", fileToGrep).cut(fields, ":", 1).toStringResult();
cut(fields, ":", 1).toStringResult();
assertEquals(expectedLineCount, Integer.parseInt(patternCount)); assertEquals(expectedLineCount, Integer.parseInt(patternCount));
} }

View File

@ -15,8 +15,7 @@ public class JoinSplitCollectionsUnitTest {
public void whenJoiningTwoArrays_thenJoined() { public void whenJoiningTwoArrays_thenJoined() {
String[] animals1 = new String[] { "Dog", "Cat" }; String[] animals1 = new String[] { "Dog", "Cat" };
String[] animals2 = new String[] { "Bird", "Cow" }; String[] animals2 = new String[] { "Bird", "Cow" };
String[] result = Stream.concat( String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new);
assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" });
} }
@ -25,9 +24,7 @@ public class JoinSplitCollectionsUnitTest {
public void whenJoiningTwoCollections_thenJoined() { public void whenJoiningTwoCollections_thenJoined() {
Collection<String> collection1 = Arrays.asList("Dog", "Cat"); Collection<String> collection1 = Arrays.asList("Dog", "Cat");
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose"); Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
Collection<String> result = Stream.concat( Collection<String> result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList());
collection1.stream(), collection2.stream())
.collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose"))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow", "Moose")));
} }
@ -36,10 +33,7 @@ public class JoinSplitCollectionsUnitTest {
public void whenJoiningTwoCollectionsWithFilter_thenJoined() { public void whenJoiningTwoCollectionsWithFilter_thenJoined() {
Collection<String> collection1 = Arrays.asList("Dog", "Cat"); Collection<String> collection1 = Arrays.asList("Dog", "Cat");
Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose"); Collection<String> collection2 = Arrays.asList("Bird", "Cow", "Moose");
Collection<String> result = Stream.concat( Collection<String> result = Stream.concat(collection1.stream(), collection2.stream()).filter(e -> e.length() == 3).collect(Collectors.toList());
collection1.stream(), collection2.stream())
.filter(e -> e.length() == 3)
.collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow"))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Cow")));
} }
@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest {
animals.put(2, "Cat"); animals.put(2, "Cat");
animals.put(3, "Cow"); animals.put(3, "Cow");
String result = animals.entrySet().stream() String result = animals.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", "));
.map(entry -> entry.getKey() + " = " + entry.getValue())
.collect(Collectors.joining(", "));
assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow"); assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow");
} }
@ -80,10 +72,7 @@ public class JoinSplitCollectionsUnitTest {
nested.add(Arrays.asList("Dog", "Cat")); nested.add(Arrays.asList("Dog", "Cat"));
nested.add(Arrays.asList("Cow", "Pig")); nested.add(Arrays.asList("Cow", "Pig"));
String result = nested.stream().map( String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; "));
nextList -> nextList.stream()
.collect(Collectors.joining("-")))
.collect(Collectors.joining("; "));
assertEquals(result, "Dog-Cat; Cow-Pig"); assertEquals(result, "Dog-Cat; Cow-Pig");
} }
@ -91,17 +80,14 @@ public class JoinSplitCollectionsUnitTest {
@Test @Test
public void whenConvertCollectionToStringAndSkipNull_thenConverted() { public void whenConvertCollectionToStringAndSkipNull_thenConverted() {
Collection<String> animals = Arrays.asList("Dog", "Cat", null, "Moose"); Collection<String> animals = Arrays.asList("Dog", "Cat", null, "Moose");
String result = animals.stream() String result = animals.stream().filter(Objects::nonNull).collect(Collectors.joining(", "));
.filter(Objects::nonNull)
.collect(Collectors.joining(", "));
assertEquals(result, "Dog, Cat, Moose"); assertEquals(result, "Dog, Cat, Moose");
} }
@Test @Test
public void whenSplitCollectionHalf_thenConverted() { public void whenSplitCollectionHalf_thenConverted() {
Collection<String> animals = Arrays.asList( Collection<String> animals = Arrays.asList("Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
"Dog", "Cat", "Cow", "Bird", "Moose", "Pig");
Collection<String> result1 = new ArrayList<>(); Collection<String> result1 = new ArrayList<>();
Collection<String> result2 = new ArrayList<>(); Collection<String> result2 = new ArrayList<>();
AtomicInteger count = new AtomicInteger(); AtomicInteger count = new AtomicInteger();
@ -123,8 +109,7 @@ public class JoinSplitCollectionsUnitTest {
@Test @Test
public void whenSplitArrayByWordLength_thenConverted() { public void whenSplitArrayByWordLength_thenConverted() {
String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose" }; String[] animals = new String[] { "Dog", "Cat", "Bird", "Cow", "Pig", "Moose" };
Map<Integer, List<String>> result = Arrays.stream(animals) Map<Integer, List<String>> result = Arrays.stream(animals).collect(Collectors.groupingBy(String::length));
.collect(Collectors.groupingBy(String::length));
assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig"))); assertTrue(result.get(3).equals(Arrays.asList("Dog", "Cat", "Cow", "Pig")));
assertTrue(result.get(4).equals(Arrays.asList("Bird"))); assertTrue(result.get(4).equals(Arrays.asList("Bird")));
@ -151,9 +136,7 @@ public class JoinSplitCollectionsUnitTest {
public void whenConvertStringToMap_thenConverted() { public void whenConvertStringToMap_thenConverted() {
String animals = "1 = Dog, 2 = Cat, 3 = Bird"; String animals = "1 = Dog, 2 = Cat, 3 = Bird";
Map<Integer, String> result = Arrays.stream( Map<Integer, String> result = Arrays.stream(animals.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
animals.split(", ")).map(next -> next.split(" = "))
.collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1]));
assertEquals(result.get(1), "Dog"); assertEquals(result.get(1), "Dog");
assertEquals(result.get(2), "Cat"); assertEquals(result.get(2), "Cat");
@ -164,10 +147,7 @@ public class JoinSplitCollectionsUnitTest {
public void whenConvertCollectionToStringMultipleSeparators_thenConverted() { public void whenConvertCollectionToStringMultipleSeparators_thenConverted() {
String animals = "Dog. , Cat, Bird. Cow"; String animals = "Dog. , Cat, Bird. Cow";
Collection<String> result = Arrays.stream(animals.split("[,|.]")) Collection<String> result = Arrays.stream(animals.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList());
.map(String::trim)
.filter(next -> !next.isEmpty())
.collect(Collectors.toList());
assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow"))); assertTrue(result.equals(Arrays.asList("Dog", "Cat", "Bird", "Cow")));
} }