1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.base;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.annotations.GwtCompatible;
22
23 import java.util.Collections;
24 import java.util.Set;
25
26 import javax.annotation.Nullable;
27
28
29
30
31 @GwtCompatible
32 final class Absent<T> extends Optional<T> {
33 static final Absent<Object> INSTANCE = new Absent<Object>();
34
35 @SuppressWarnings("unchecked")
36 static <T> Optional<T> withType() {
37 return (Optional<T>) INSTANCE;
38 }
39
40 private Absent() {}
41
42 @Override public boolean isPresent() {
43 return false;
44 }
45
46 @Override public T get() {
47 throw new IllegalStateException("Optional.get() cannot be called on an absent value");
48 }
49
50 @Override public T or(T defaultValue) {
51 return checkNotNull(defaultValue, "use Optional.orNull() instead of Optional.or(null)");
52 }
53
54 @SuppressWarnings("unchecked")
55 @Override public Optional<T> or(Optional<? extends T> secondChoice) {
56 return (Optional<T>) checkNotNull(secondChoice);
57 }
58
59 @Override public T or(Supplier<? extends T> supplier) {
60 return checkNotNull(supplier.get(),
61 "use Optional.orNull() instead of a Supplier that returns null");
62 }
63
64 @Override @Nullable public T orNull() {
65 return null;
66 }
67
68 @Override public Set<T> asSet() {
69 return Collections.emptySet();
70 }
71
72 @Override public <V> Optional<V> transform(Function<? super T, V> function) {
73 checkNotNull(function);
74 return Optional.absent();
75 }
76
77 @Override public boolean equals(@Nullable Object object) {
78 return object == this;
79 }
80
81 @Override public int hashCode() {
82 return 0x598df91c;
83 }
84
85 @Override public String toString() {
86 return "Optional.absent()";
87 }
88
89 private Object readResolve() {
90 return INSTANCE;
91 }
92
93 private static final long serialVersionUID = 0;
94 }