minor formatting work
This commit is contained in:
@@ -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<Integer> 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<String> 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<String> 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<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
||||
|
||||
assertThat(list, hasItems(intArray));
|
||||
|
||||
@@ -34,9 +34,9 @@ public class AsyncEchoServer {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(32);
|
||||
Future<Integer> 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<Integer> writeResult = clientChannel.write(buffer);
|
||||
|
||||
//do some computation
|
||||
writeResult.get();
|
||||
|
||||
// do some computation
|
||||
writeResult.get();
|
||||
buffer.clear();
|
||||
|
||||
} // while()
|
||||
|
||||
@@ -82,7 +82,6 @@ public class AsyncEchoServer2 {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
new AsyncEchoServer2();
|
||||
}
|
||||
|
||||
@@ -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<Integer> 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<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
|
||||
Optional<List<String>> 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<String> 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<String> 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<Person> personOptional = Optional.of(person);
|
||||
|
||||
Optional<Optional<String>> nameOptionalWrapper = personOptional
|
||||
.map(Person::getName);
|
||||
Optional<String> nameOptional = nameOptionalWrapper
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
String name1 = nameOptional
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(Person::getName);
|
||||
Optional<String> 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<Person> 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() {
|
||||
|
||||
Reference in New Issue
Block a user