Skip to content
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit c999b10

Browse files
author
Nicolai Parlog
committed
Experiment with Intersection Types
1 parent 7446eed commit c999b10

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ You can read more from me on [codefx.org](http://codefx.org) ([Java 10 tag](http
1212
* download [OpenJDK 10](http://jdk.java.net/10/) (not Oracle JDK!) and unpack it
1313
* to use with Maven simply set `target` and `release` to `10` (see [`pom.xml`](pom.xml#L13-L14))
1414
* to use with IntelliJ:
15-
* install [2017.3 EAP](https://www.jetbrains.com/idea/nextversion/)
15+
* install [2017.3 EAP](https://www.jetbrains.com/idea/nextversion/) or later
1616
* go to an occurrence of `var`, hit _ALT + Enter_, and "Enable support for beta java version"
1717
* in project and module settings, make sure that language level _X_ is selected
1818

1919
## Language Changes
2020

2121
* [local-variable type inference with `var`](src/main/java/org/codefx/demo/java10/lang/var/VariableTypeInference.java) ([blog post](http://blog.codefx.org/java/java-10-var-type-inference/), [video](https://www.youtube.com/watch?v=Le1DbpRZdRQ), [JEP 286](http://openjdk.java.net/jeps/286))
22+
* experiments with [intersection types](src/main/java/org/codefx/demo/java10/lang/var/IntersectionTypes.java) ([blog post](http://blog.codefx.org/java/intersection-types-var))
2223

2324
## JVM Capabilities
2425

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.codefx.demo.java10.lang.var;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.util.Iterator;
6+
import java.util.Optional;
7+
import java.util.Random;
8+
import java.util.Scanner;
9+
import java.util.function.Predicate;
10+
import java.util.stream.Stream;
11+
import java.util.stream.StreamSupport;
12+
13+
import static java.util.stream.StreamSupport.stream;
14+
15+
public class IntersectionTypes {
16+
17+
public static void main(String[] args) throws Exception {
18+
boolean empty = new Random().nextBoolean();
19+
doItWithGenerics(empty);
20+
doItWithVar(empty);
21+
}
22+
23+
private static <T extends Closeable & Iterator<String>> void doItWithGenerics(boolean empty) throws IOException {
24+
T elements = createCloseableIterator(empty);
25+
Optional<String> dollarWord = firstMatch(elements, s -> s.startsWith("$"));
26+
System.out.println(dollarWord.orElse("none"));
27+
}
28+
29+
private static void doItWithVar(boolean empty) throws IOException {
30+
var scanner = createCloseableIterator(empty);
31+
Optional<String> dollarWord = firstMatch(scanner, s -> s.startsWith("$"));
32+
System.out.println(dollarWord.orElse("none"));
33+
}
34+
35+
@SuppressWarnings("unchecked")
36+
private static <T extends Closeable & Iterator<String>> T createCloseableIterator(boolean empty) {
37+
if (empty)
38+
return (T) new Empty();
39+
else
40+
return (T) new Scanner(System.in);
41+
}
42+
43+
private static <E, T extends Closeable & Iterator<E>> Optional<E> firstMatch(
44+
T elements, Predicate<? super E> condition)
45+
throws IOException {
46+
try (elements) {
47+
return stream(elements)
48+
.filter(condition)
49+
.findAny();
50+
}
51+
}
52+
53+
private static <E> Stream<E> stream(Iterator<E> elements) {
54+
return StreamSupport.stream(((Iterable<E>) () -> elements).spliterator(), false);
55+
}
56+
57+
static class Empty<E> implements Closeable, Iterator<E> {
58+
59+
@Override
60+
public void close() throws IOException {
61+
62+
}
63+
64+
@Override
65+
public boolean hasNext() {
66+
return false;
67+
}
68+
69+
@Override
70+
public E next() {
71+
return null;
72+
}
73+
}
74+
75+
}

0 commit comments

Comments
 (0)