Empty.java (998B)
1 import java.util.function.BiFunction; 2 import java.util.function.Function; 3 import java.util.function.Predicate; 4 5 public class Empty<V> implements Cons<V> { 6 7 @Override 8 public V first() { 9 throw new UnsupportedOperationException(); 10 } 11 12 @Override 13 public Cons<V> rest() { 14 throw new UnsupportedOperationException(); 15 } 16 17 @Override 18 public <W> Cons<W> map(Function<V, W> mapper) { 19 return new Empty<>(); 20 } 21 22 @Override 23 public Cons<V> filter(Predicate<V> predicate) { 24 return new Empty<>(); 25 } 26 27 @Override 28 public <W> W foldl(BiFunction<V, W, W> f, W init) { 29 return init; 30 } 31 32 @Override 33 public <W> W foldr(BiFunction<V, W, W> f, W init) { 34 return init; 35 } 36 37 @Override 38 public boolean equals(Object obj) { 39 return obj instanceof Empty; 40 } 41 42 @Override 43 public boolean empty() { 44 return true; 45 } 46 47 @Override 48 public String toString() { 49 return "empty"; 50 } 51 }