• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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  * Implementation of an {@link Optional} not containing a reference.
30  */
31 @GwtCompatible
32 final class Absent<T> extends Optional<T> {
33   static final Absent<Object> INSTANCE = new Absent<Object>();
34 
35   @SuppressWarnings("unchecked") // implementation is "fully variant"
withType()36   static <T> Optional<T> withType() {
37     return (Optional<T>) INSTANCE;
38   }
39 
Absent()40   private Absent() {}
41 
isPresent()42   @Override public boolean isPresent() {
43     return false;
44   }
45 
get()46   @Override public T get() {
47     throw new IllegalStateException("Optional.get() cannot be called on an absent value");
48   }
49 
or(T defaultValue)50   @Override public T or(T defaultValue) {
51     return checkNotNull(defaultValue, "use Optional.orNull() instead of Optional.or(null)");
52   }
53 
54   @SuppressWarnings("unchecked") // safe covariant cast
or(Optional<? extends T> secondChoice)55   @Override public Optional<T> or(Optional<? extends T> secondChoice) {
56     return (Optional<T>) checkNotNull(secondChoice);
57   }
58 
or(Supplier<? extends T> supplier)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 
orNull()64   @Override @Nullable public T orNull() {
65     return null;
66   }
67 
asSet()68   @Override public Set<T> asSet() {
69     return Collections.emptySet();
70   }
71 
transform(Function<? super T, V> function)72   @Override public <V> Optional<V> transform(Function<? super T, V> function) {
73     checkNotNull(function);
74     return Optional.absent();
75   }
76 
equals(@ullable Object object)77   @Override public boolean equals(@Nullable Object object) {
78     return object == this;
79   }
80 
hashCode()81   @Override public int hashCode() {
82     return 0x598df91c;
83   }
84 
toString()85   @Override public String toString() {
86     return "Optional.absent()";
87   }
88 
readResolve()89   private Object readResolve() {
90     return INSTANCE;
91   }
92 
93   private static final long serialVersionUID = 0;
94 }
95