diff --git a/core-java/src/main/java/com/baeldung/generics/Building.java b/core-java/src/main/java/com/baeldung/generics/Building.java index d162c07774..a34dcd3c7e 100644 --- a/core-java/src/main/java/com/baeldung/generics/Building.java +++ b/core-java/src/main/java/com/baeldung/generics/Building.java @@ -4,9 +4,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Building { - private static final Logger LOGGER = LoggerFactory.getLogger(Building.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Building.class); - public void paint() { - LOGGER.info("Painting Building"); - } + public void paint() { + LOGGER.info("Painting Building"); + } } diff --git a/core-java/src/main/java/com/baeldung/generics/Generics.java b/core-java/src/main/java/com/baeldung/generics/Generics.java index 2b83778f64..514ccdae51 100644 --- a/core-java/src/main/java/com/baeldung/generics/Generics.java +++ b/core-java/src/main/java/com/baeldung/generics/Generics.java @@ -15,11 +15,9 @@ public class Generics { // definition of a generic method public static List fromArrayToList(T[] a, List list, Function mapperFunction) { List listWithTypeT = Arrays.stream(a).collect(Collectors.toList()); - return listWithTypeT.stream().map(mapperFunction) - .collect(Collectors.toList()); + return listWithTypeT.stream().map(mapperFunction).collect(Collectors.toList()); } - // example of a generic method that has Number as an upper bound for T public static List fromArrayToListWithUpperBound(T[] a) { return Arrays.stream(a).collect(Collectors.toList()); diff --git a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java index 139633d924..36f7d55675 100644 --- a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java +++ b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java @@ -14,7 +14,7 @@ public class GenericsTest { // testing the generic method with Integer @Test public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() { - Integer[] intArray = {1, 2, 3, 4, 5}; + Integer[] intArray = { 1, 2, 3, 4, 5 }; List list = Generics.fromArrayToList(intArray); assertThat(list, hasItems(intArray)); @@ -23,7 +23,7 @@ public class GenericsTest { // testing the generic method with Integer and String type @Test public void givenArrayOfIntegers_thanListOfStringReturnedOK() { - Integer[] intArray = {1, 2, 3, 4, 5}; + Integer[] intArray = { 1, 2, 3, 4, 5 }; List stringList = new ArrayList<>(); stringList = Generics.fromArrayToList(intArray, stringList, Object::toString); assertThat(stringList, hasItems("1", "2", "3", "4", "5")); @@ -32,7 +32,7 @@ public class GenericsTest { // testing the generic method with String @Test public void givenArrayOfStrings_thanListOfStringsReturnedOK() { - String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"}; + String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" }; List list = Generics.fromArrayToList(stringArray); assertThat(list, hasItems(stringArray)); @@ -43,7 +43,7 @@ public class GenericsTest { // extend Number it will fail to compile @Test public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() { - Integer[] intArray = {1, 2, 3, 4, 5}; + Integer[] intArray = { 1, 2, 3, 4, 5 }; List list = Generics.fromArrayToListWithUpperBound(intArray); assertThat(list, hasItems(intArray)); diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer.java index 5c3d204863..01873b344a 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer.java @@ -34,9 +34,9 @@ public class AsyncEchoServer { ByteBuffer buffer = ByteBuffer.allocate(32); Future readResult = clientChannel.read(buffer); - //do some computation - - readResult.get(); + // do some computation + + readResult.get(); buffer.flip(); String message = new String(buffer.array()).trim(); @@ -45,9 +45,9 @@ public class AsyncEchoServer { } buffer = ByteBuffer.wrap(new String(message).getBytes()); Future writeResult = clientChannel.write(buffer); - - //do some computation - writeResult.get(); + + // do some computation + writeResult.get(); buffer.clear(); } // while() diff --git a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java index 8a7174ae18..663fc4f2ed 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/async/AsyncEchoServer2.java @@ -82,7 +82,6 @@ public class AsyncEchoServer2 { } - public static void main(String[] args) { new AsyncEchoServer2(); } diff --git a/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java b/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java index 05dc3a9a2c..1038043d49 100644 --- a/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java +++ b/core-java/src/test/java/com/baeldung/java8/optional/OptionalTest.java @@ -1,14 +1,17 @@ package com.baeldung.java8.optional; -import com.baeldung.java_8_features.Person; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; import java.util.Optional; -import static org.junit.Assert.*; +import org.junit.Test; + +import com.baeldung.java_8_features.Person; public class OptionalTest { // creating Optional @@ -87,13 +90,9 @@ public class OptionalTest { public void whenOptionalFilterWorks_thenCorrect() { Integer year = 2016; Optional yearOptional = Optional.of(year); - boolean is2016 = yearOptional - .filter(y -> y == 2016) - .isPresent(); + boolean is2016 = yearOptional.filter(y -> y == 2016).isPresent(); assertTrue(is2016); - boolean is2017 = yearOptional - .filter(y -> y == 2017) - .isPresent(); + boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent(); assertFalse(is2017); } @@ -103,8 +102,7 @@ public class OptionalTest { List companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple"); Optional> listOptional = Optional.of(companyNames); - int size = listOptional.map(List::size) - .orElse(0); + int size = listOptional.map(List::size).orElse(0); assertEquals(6, size); } @@ -113,9 +111,7 @@ public class OptionalTest { String name = "baeldung"; Optional nameOptional = Optional.of(name); - int len = nameOptional - .map(String::length) - .orElse(0); + int len = nameOptional.map(String::length).orElse(0); assertEquals(8, len); } @@ -123,14 +119,10 @@ public class OptionalTest { public void givenOptional_whenMapWorksWithFilter_thenCorrect() { String password = " password "; Optional passOpt = Optional.of(password); - boolean correctPassword = passOpt - .filter(pass -> pass.equals("password")) - .isPresent(); + boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent(); assertFalse(correctPassword); - correctPassword = passOpt.map(String::trim) - .filter(pass -> pass.equals("password")) - .isPresent(); + correctPassword = passOpt.map(String::trim).filter(pass -> pass.equals("password")).isPresent(); assertTrue(correctPassword); } @@ -140,17 +132,12 @@ public class OptionalTest { Person person = new Person("john", 26); Optional personOptional = Optional.of(person); - Optional> nameOptionalWrapper = personOptional - .map(Person::getName); - Optional nameOptional = nameOptionalWrapper - .orElseThrow(IllegalArgumentException::new); - String name1 = nameOptional - .orElseThrow(IllegalArgumentException::new); + Optional> nameOptionalWrapper = personOptional.map(Person::getName); + Optional nameOptional = nameOptionalWrapper.orElseThrow(IllegalArgumentException::new); + String name1 = nameOptional.orElseThrow(IllegalArgumentException::new); assertEquals("john", name1); - String name = personOptional - .flatMap(Person::getName) - .orElseThrow(IllegalArgumentException::new); + String name = personOptional.flatMap(Person::getName).orElseThrow(IllegalArgumentException::new); assertEquals("john", name); } @@ -160,10 +147,7 @@ public class OptionalTest { person.setPassword("password"); Optional personOptional = Optional.of(person); - String password = personOptional - .flatMap(Person::getPassword) - .filter(cleanPass -> cleanPass.equals("password")) - .orElseThrow(IllegalArgumentException::new); + String password = personOptional.flatMap(Person::getPassword).filter(cleanPass -> cleanPass.equals("password")).orElseThrow(IllegalArgumentException::new); assertEquals("password", password); } @@ -171,8 +155,7 @@ public class OptionalTest { @Test public void whenOrElseWorks_thenCorrect() { String nullName = null; - String name = Optional.ofNullable(nullName) - .orElse("john"); + String name = Optional.ofNullable(nullName).orElse("john"); assertEquals("john", name); } @@ -180,8 +163,7 @@ public class OptionalTest { @Test public void whenOrElseGetWorks_thenCorrect() { String nullName = null; - String name = Optional.ofNullable(nullName) - .orElseGet(() -> "john"); + String name = Optional.ofNullable(nullName).orElseGet(() -> "john"); assertEquals("john", name); } @@ -190,13 +172,11 @@ public class OptionalTest { public void whenOrElseGetAndOrElseOverlap_thenCorrect() { String text = null; System.out.println("Using orElseGet:"); - String defaultText = Optional.ofNullable(text) - .orElseGet(this::getMyDefault); + String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault); assertEquals("Default Value", defaultText); System.out.println("Using orElse:"); - defaultText = Optional.ofNullable(text) - .orElse(getMyDefault()); + defaultText = Optional.ofNullable(text).orElse(getMyDefault()); assertEquals("Default Value", defaultText); } @@ -204,13 +184,11 @@ public class OptionalTest { public void whenOrElseGetAndOrElseDiffer_thenCorrect() { String text = "Text present"; System.out.println("Using orElseGet:"); - String defaultText = Optional.ofNullable(text) - .orElseGet(this::getMyDefault); + String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault); assertEquals("Text present", defaultText); System.out.println("Using orElse:"); - defaultText = Optional.ofNullable(text) - .orElse(getMyDefault()); + defaultText = Optional.ofNullable(text).orElse(getMyDefault()); assertEquals("Text present", defaultText); } @@ -218,8 +196,7 @@ public class OptionalTest { @Test(expected = IllegalArgumentException.class) public void whenOrElseThrowWorks_thenCorrect() { String nullName = null; - String name = Optional.ofNullable(nullName) - .orElseThrow(IllegalArgumentException::new); + String name = Optional.ofNullable(nullName).orElseThrow(IllegalArgumentException::new); } public String getMyDefault() { diff --git a/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java index 7efc2ab9de..78f79f646b 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/JoinSplitCollectionsUnitTest.java @@ -13,20 +13,18 @@ public class JoinSplitCollectionsUnitTest { @Test public void whenJoiningTwoArrays_thenJoined() { - String[] animals1 = new String[]{"Dog", "Cat"}; - String[] animals2 = new String[]{"Bird", "Cow"}; - String[] result = Stream.concat(Arrays.stream(animals1), Arrays.stream(animals2)) - .toArray(String[]::new); + String[] animals1 = new String[] { "Dog", "Cat" }; + String[] animals2 = new String[] { "Bird", "Cow" }; + String[] result = Stream.concat(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" }); } @Test public void whenJoiningTwoCollections_thenJoined() { Collection collection1 = Arrays.asList(7, 8, 9); Collection collection2 = Arrays.asList(10, 11, 12); - Collection result = Stream.concat(collection1.stream(), collection2.stream()) - .collect(Collectors.toList()); + Collection result = Stream.concat(collection1.stream(), collection2.stream()).collect(Collectors.toList()); assertTrue(result.equals(Arrays.asList(7, 8, 9, 10, 11, 12))); } @@ -35,18 +33,15 @@ public class JoinSplitCollectionsUnitTest { public void whenJoiningTwoCollectionsWithFilter_thenJoined() { Collection collection1 = Arrays.asList(7, 8, 11); Collection collection2 = Arrays.asList(9, 12, 10); - Collection result = Stream.concat(collection1.stream(), collection2.stream()) - .filter(next -> next <= 10) - .collect(Collectors.toList()); + Collection result = Stream.concat(collection1.stream(), collection2.stream()).filter(next -> next <= 10).collect(Collectors.toList()); assertTrue(result.equals(Arrays.asList(7, 8, 9, 10))); } @Test public void whenConvertArrayToString_thenConverted() { - String[] colors = new String[]{"Red", "Blue", "Green", "Yellow"}; - String result = Arrays.stream(colors) - .collect(Collectors.joining(", ")); + String[] colors = new String[] { "Red", "Blue", "Green", "Yellow" }; + String result = Arrays.stream(colors).collect(Collectors.joining(", ")); assertEquals(result, "Red, Blue, Green, Yellow"); } @@ -54,8 +49,7 @@ public class JoinSplitCollectionsUnitTest { @Test public void whenConvertCollectionToString_thenConverted() { Collection directions = Arrays.asList("Left", "Right", "Top", "Bottom"); - String result = directions.stream() - .collect(Collectors.joining(", ")); + String result = directions.stream().collect(Collectors.joining(", ")); assertEquals(result, "Left, Right, Top, Bottom"); } @@ -67,9 +61,7 @@ public class JoinSplitCollectionsUnitTest { users.put(2, "Paul Smith"); users.put(3, "Susan Anderson"); - String result = users.entrySet().stream() - .map(entry -> entry.getKey() + " = " + entry.getValue()) - .collect(Collectors.joining(", ")); + String result = users.entrySet().stream().map(entry -> entry.getKey() + " = " + entry.getValue()).collect(Collectors.joining(", ")); assertEquals(result, "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"); } @@ -80,10 +72,7 @@ public class JoinSplitCollectionsUnitTest { nested.add(Arrays.asList("Left", "Right", "Top", "Bottom")); nested.add(Arrays.asList("Red", "Blue", "Green", "Yellow")); - String result = nested.stream() - .map(nextList -> nextList.stream() - .collect(Collectors.joining("-"))) - .collect(Collectors.joining("; ")); + String result = nested.stream().map(nextList -> nextList.stream().collect(Collectors.joining("-"))).collect(Collectors.joining("; ")); assertEquals(result, "Left-Right-Top-Bottom; Red-Blue-Green-Yellow"); } @@ -91,9 +80,7 @@ public class JoinSplitCollectionsUnitTest { @Test public void whenConvertCollectionToStringAndSkipNull_thenConverted() { Collection fruits = Arrays.asList("Apple", "Orange", null, "Grape"); - String result = fruits.stream() - .filter(Objects::nonNull) - .collect(Collectors.joining(", ")); + String result = fruits.stream().filter(Objects::nonNull).collect(Collectors.joining(", ")); assertEquals(result, "Apple, Orange, Grape"); } @@ -121,9 +108,8 @@ public class JoinSplitCollectionsUnitTest { @Test public void whenSplitArrayByWordLength_thenConverted() { - String[] words = new String[]{"bye", "cold", "it", "and", "my", "word"}; - Map> result = Arrays.stream(words) - .collect(Collectors.groupingBy(String::length)); + String[] words = new String[] { "bye", "cold", "it", "and", "my", "word" }; + Map> result = Arrays.stream(words).collect(Collectors.groupingBy(String::length)); assertTrue(result.get(2).equals(Arrays.asList("it", "my"))); assertTrue(result.get(3).equals(Arrays.asList("bye", "and"))); @@ -135,7 +121,7 @@ public class JoinSplitCollectionsUnitTest { String colors = "Red, Blue, Green, Yellow"; String[] result = colors.split(", "); - assertArrayEquals(result, new String[]{"Red", "Blue", "Green", "Yellow"}); + assertArrayEquals(result, new String[] { "Red", "Blue", "Green", "Yellow" }); } @Test @@ -150,9 +136,7 @@ public class JoinSplitCollectionsUnitTest { public void whenConvertStringToMap_thenConverted() { String users = "1 = John Doe, 2 = Paul Smith, 3 = Susan Anderson"; - Map result = Arrays.stream(users.split(", ")) - .map(next -> next.split(" = ")) - .collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); + Map result = Arrays.stream(users.split(", ")).map(next -> next.split(" = ")).collect(Collectors.toMap(entry -> Integer.parseInt(entry[0]), entry -> entry[1])); assertEquals(result.get(1), "John Doe"); assertEquals(result.get(2), "Paul Smith"); @@ -163,10 +147,7 @@ public class JoinSplitCollectionsUnitTest { public void whenConvertCollectionToStringMultipleSeparators_thenConverted() { String fruits = "Apple. , Orange, Grape. Lemon"; - Collection result = Arrays.stream(fruits.split("[,|.]")) - .map(String::trim) - .filter(next -> !next.isEmpty()) - .collect(Collectors.toList()); + Collection result = Arrays.stream(fruits.split("[,|.]")).map(String::trim).filter(next -> !next.isEmpty()).collect(Collectors.toList()); assertTrue(result.equals(Arrays.asList("Apple", "Orange", "Grape", "Lemon"))); }