merged formatting changes and removed while loops in asynchronous client (#870)

* made changes to java reflection

* removed redundant method makeSound in Animal abstract class

* added project for play-framework article

* added project for regex

* changed regex project from own model to core-java

* added project for routing in play

* made changes to regex project

* refactored code for REST API with Play project

* refactored student store indexing to zero base

* added unit tests, removed bad names

* added NIO Selector project under core-java module

* requested changes made

* added project for nio2

* standardized exception based tests

* fixed exception based tests

* removed redundant files

* added network interface project

* used UUID other than timestamps

* fixed network interface tests

* removed filetest change

* made changes to NIO2 FileTest names

* added project for asyncronous channel apis

* added project for NIO2 advanced filesystems APIS

* merge conflicts

* merged changes to asyncfiletest with future get API

* removed while loops from async client and server

* added project for java8 optional

* fixed merge conflicts in spring-core
This commit is contained in:
Egima profile 2016-12-01 11:55:20 +03:00 committed by Grzegorz Piwowarek
parent ac89a21ee2
commit aa68501341
12 changed files with 270 additions and 21 deletions

View File

@ -53,4 +53,4 @@ public class FileSearchExample implements FileVisitor<Path> {
Files.walkFileTree(startingDir, crawler);
}
}
}

View File

@ -27,4 +27,4 @@ public class FileVisitorImpl implements FileVisitor<Path> {
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return null;
}
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.java_8_features;
import java.util.Optional;
public class Person {
private String name;
private int age;
private String password;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Optional<String> getName() {
return Optional.ofNullable(name);
}
public void setName(String name) {
this.name = name;
}
public Optional<Integer> getAge() {
return Optional.ofNullable(age);
}
public void setAge(int age) {
this.age = age;
}
public void setPassword(String password) {
this.password = password;
}
public Optional<String> getPassword() {
return Optional.ofNullable(password);
}
}

View File

@ -45,14 +45,13 @@ public class AsyncEchoClient {
ByteBuffer buffer = ByteBuffer.wrap(byteMsg);
Future<Integer> writeResult = client.write(buffer);
while (!writeResult.isDone()) {
// do nothing
}
//run some code
writeResult.get();
buffer.flip();
Future<Integer> readResult = client.read(buffer);
while (!readResult.isDone()) {
}
//run some code
readResult.get();
String echo = new String(buffer.array()).trim();
buffer.clear();
return echo;

View File

@ -34,9 +34,9 @@ public class AsyncEchoServer {
ByteBuffer buffer = ByteBuffer.allocate(32);
Future<Integer> readResult = clientChannel.read(buffer);
while (!readResult.isDone()) {
// do nothing
}
//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);
while (!writeResult.isDone()) {
// do nothing
}
//do some computation
writeResult.get();
buffer.clear();
} // while()
@ -77,4 +77,4 @@ public class AsyncEchoServer {
return builder.start();
}
}
}

View File

@ -95,4 +95,4 @@ public class AsyncEchoServer2 {
return builder.start();
}
}
}

View File

@ -123,4 +123,4 @@ public class AsyncFileTest {
buffer.clear();
return fileContent;
}
}
}

View File

@ -66,4 +66,4 @@ public class BasicAttribsTest {
boolean isOther = basicAttribs.isOther();
assertFalse(isOther);
}
}
}

View File

@ -0,0 +1,211 @@
package com.baeldung.java8.optional;
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 com.baeldung.java_8_features.Person;
import org.junit.Test;
public class OptionalTest {
// creating Optional
@Test
public void whenCreatesEmptyOptional_thenCorrect() {
Optional<String> empty = Optional.empty();
assertFalse(empty.isPresent());
}
@Test
public void givenNonNull_whenCreatesNonNullable_thenCorrect() {
String name = "baeldung";
Optional.of(name);
}
@Test(expected = NullPointerException.class)
public void givenNull_whenThrowsErrorOnCreate_thenCorrect() {
String name = null;
Optional<String> opt = Optional.of(name);
}
@Test
public void givenNonNull_whenCreatesOptional_thenCorrect() {
String name = "baeldung";
Optional<String> opt = Optional.of(name);
assertEquals("Optional[baeldung]", opt.toString());
}
@Test
public void givenNonNull_whenCreatesNullable_thenCorrect() {
String name = "baeldung";
Optional<String> opt = Optional.ofNullable(name);
assertEquals("Optional[baeldung]", opt.toString());
}
@Test
public void givenNull_whenCreatesNullable_thenCorrect() {
String name = null;
Optional<String> opt = Optional.ofNullable(name);
assertEquals("Optional.empty", opt.toString());
}
// Checking Value With isPresent()
@Test
public void givenOptional_whenIsPresentWorks_thenCorrect() {
Optional<String> opt = Optional.of("Baeldung");
assertTrue(opt.isPresent());
opt = Optional.ofNullable(null);
assertFalse(opt.isPresent());
}
// Condition Action With ifPresent()
@Test
public void givenOptional_whenIfPresentWorks_thenCorrect() {
Optional<String> opt = Optional.of("baeldung");
opt.ifPresent(name -> System.out.println(name.length()));
opt.ifPresent(String::length);
}
// returning Value With get()
@Test
public void givenOptional_whenGetsValue_thenCorrect() {
Optional<String> opt = Optional.of("baeldung");
String name = opt.get();
assertEquals("baeldung", name);
}
@Test(expected = NoSuchElementException.class)
public void givenOptionalWithNull_whenGetThrowsException_thenCorrect() {
Optional<String> opt = Optional.ofNullable(null);
String name = opt.get();
}
// Conditional Return With filter()
@Test
public void whenOptionalFilterWorks_thenCorrect() {
Integer year = 2016;
Optional<Integer> yearOptional = Optional.of(year);
boolean is2016 = yearOptional.filter(y -> y == 2016).isPresent();
assertTrue(is2016);
boolean is2017 = yearOptional.filter(y -> y == 2017).isPresent();
assertFalse(is2017);
}
// Transforming Value With map()
@Test
public void givenOptional_whenMapWorks_thenCorrect() {
List<String> companyNames = Arrays.asList("paypal", "oracle", "", "microsoft", "", "apple");
Optional<List<String>> listOptional = Optional.of(companyNames);
int size = listOptional.map(list -> list.size()).get();
assertEquals(6, size);
}
@Test
public void givenOptional_whenMapWorks_thenCorrect2() {
String name = "baeldung";
Optional<String> nameOptional = Optional.of(name);
int len = nameOptional.map(s -> s.length()).get();
assertEquals(8, len);
}
@Test
public void givenOptional_whenMapWorksWithFilter_thenCorrect() {
String password = " password ";
Optional<String> passOpt = Optional.of(password);
boolean correctPassword = passOpt.filter(pass -> pass.equals("password")).isPresent();
assertFalse(correctPassword);
correctPassword = passOpt.map(pass -> pass.trim()).filter(pass -> pass.equals("password")).isPresent();
assertTrue(correctPassword);
}
// Transforming Value With flatMap()
@Test
public void givenOptional_whenFlatMapWorks_thenCorrect2() {
Person person = new Person("john", 26);
Optional<Person> personOptional = Optional.of(person);
Optional<Optional<String>> nameOptionalWrapper = personOptional.map(p -> p.getName());
Optional<String> nameOptional = nameOptionalWrapper.get();
String name1 = nameOptional.get();
assertEquals("john", name1);
String name = personOptional.flatMap(p -> p.getName()).get();
assertEquals("john", name);
}
@Test
public void givenOptional_whenFlatMapWorksWithFilter_thenCorrect() {
Person person = new Person("john", 26);
person.setPassword("password");
Optional<Person> personOptional = Optional.of(person);
String password = personOptional.flatMap(p -> p.getPassword()).filter(cleanPass -> cleanPass.equals("password")).get();
assertEquals("password", password);
}
// Default Value With orElse
@Test
public void whenOrElseWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElse("john");
assertEquals("john", name);
}
// Default Value With orElseGet
@Test
public void whenOrElseGetWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElseGet(() -> "john");
assertEquals("john", name);
name = Optional.ofNullable(nullName).orElseGet(() -> {
return "doe";
});
assertEquals("doe", name);
}
@Test
public void whenOrElseGetAndOrElseOverlap_thenCorrect() {
String text = null;
System.out.println("Using orElseGet:");
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
assertEquals("Default Value", defaultText);
System.out.println("Using orElse:");
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
assertEquals("Default Value", defaultText);
}
@Test
public void whenOrElseGetAndOrElseDiffer_thenCorrect() {
String text = "Text present";
System.out.println("Using orElseGet:");
String defaultText = Optional.ofNullable(text).orElseGet(this::getMyDefault);
assertEquals("Text present", defaultText);
System.out.println("Using orElse:");
defaultText = Optional.ofNullable(text).orElse(getMyDefault());
assertEquals("Text present", defaultText);
}
// Exceptions With orElseThrow
@Test(expected = IllegalArgumentException.class)
public void whenOrElseThrowWorks_thenCorrect() {
String nullName = null;
String name = Optional.ofNullable(nullName).orElseThrow(IllegalArgumentException::new);
}
public String getMyDefault() {
System.out.println("Getting default value...");
return "Default Value";
}
}

View File

@ -13,4 +13,4 @@ public class FactoryBeanAppConfig {
factory.setToolId(2);
return factory;
}
}
}

View File

@ -26,4 +26,4 @@ public class FactoryBeanJavaConfigTest {
assertThat(tool.getId(), equalTo(2));
assertThat(toolFactory.getFactoryId(), equalTo(7070));
}
}
}

View File

@ -25,4 +25,4 @@ public class FactoryBeanXmlConfigTest {
assertThat(tool.getId(), equalTo(1));
assertThat(toolFactory.getFactoryId(), equalTo(9090));
}
}
}