Skip to content

Commit 1474c71

Browse files
committed
java-8 optional and inheritance
1 parent b63e8f5 commit 1474c71

File tree

7 files changed

+124
-5
lines changed

7 files changed

+124
-5
lines changed

pom.xml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2-
<modelVersion>4.0.0</modelVersion>
3-
<groupId>com.test</groupId>
4-
<artifactId>java-8</artifactId>
5-
<version>0.0.1-SNAPSHOT</version>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.test</groupId>
5+
<artifactId>java-8</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<dependencies>
8+
<dependency>
9+
<groupId>junit</groupId>
10+
<artifactId>junit</artifactId>
11+
<version>4.12</version>
12+
</dependency>
13+
<dependency>
14+
<groupId>org.hamcrest</groupId>
15+
<artifactId>hamcrest-core</artifactId>
16+
<version>1.3</version>
17+
<scope>test</scope>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.hamcrest</groupId>
21+
<artifactId>hamcrest-library</artifactId>
22+
<version>1.3</version>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
626
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.test.inheritance;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public interface ABC<T, D, K, V> {
7+
8+
default List<String> getSrc(DEF def, XYZ xyz) {
9+
final List<String> defaultList = new ArrayList<>();
10+
defaultList.add("default");
11+
defaultList.add("another-default");
12+
return defaultList;
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.test.inheritance;
2+
3+
import java.util.List;
4+
5+
public class ABCImpl implements ABC<String, Integer, String, Integer> {
6+
7+
public List<String> getSrcImpl(DEF def, XYZ xyz) {
8+
final List<String> list = getSrc(def, xyz);
9+
list.add("implementation");
10+
return list;
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.test.inheritance;
2+
3+
public class DEF {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.test.inheritance;
2+
3+
public class XYZ {
4+
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.test.optional;
2+
3+
import java.util.Optional;
4+
5+
public class OptionalExample {
6+
7+
public static void main(String[] args) {
8+
9+
Optional<String> emptyOptional = Optional.empty();
10+
System.out.println(emptyOptional);
11+
12+
String val = emptyOptional.orElse("default-value");
13+
System.out.println(val);
14+
15+
Optional<String> optional = Optional.of("hello");
16+
System.out.println(optional.get() + " Or Else :: "
17+
+ optional.orElse("hello-world"));
18+
19+
optional.ifPresent( System.out::println );
20+
21+
22+
Optional<String> optionalOfNullable = Optional.ofNullable(null);
23+
System.out.println(optionalOfNullable.isPresent());
24+
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.test.inheritance;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.empty;
5+
import static org.hamcrest.Matchers.is;
6+
import static org.hamcrest.Matchers.not;
7+
import static org.hamcrest.Matchers.contains;
8+
import java.util.Collection;
9+
import java.util.List;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class ABCImplTest {
15+
16+
private ABCImpl abcImpl;
17+
18+
@Before
19+
public void setup() {
20+
abcImpl = new ABCImpl();
21+
}
22+
23+
@Test
24+
public void testGetSrc() throws Exception {
25+
List<String> result = abcImpl.getSrc(new DEF(), new XYZ());
26+
assertThat((Collection<String>) result, is(not(empty())));
27+
assertThat(result, contains("default", "another-default"));
28+
}
29+
30+
31+
@Test
32+
public void testABCImplGetSrc() throws Exception {
33+
List<String> result = abcImpl.getSrcImpl(new DEF(), new XYZ());
34+
assertThat((Collection<String>) result, is(not(empty())));
35+
assertThat(result, contains("default", "another-default", "implementation"));
36+
}
37+
}

0 commit comments

Comments
 (0)