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 org.whispersystems.libsignal.util.guava; 18 19 import static org.whispersystems.libsignal.util.guava.Preconditions.checkNotNull; 20 21 22 23 import java.util.Collections; 24 import java.util.Set; 25 26 27 /** 28 * Implementation of an {@link Optional} not containing a reference. 29 */ 30 31 final class Absent extends Optional<Object> { 32 static final Absent INSTANCE = new Absent(); 33 isPresent()34 @Override public boolean isPresent() { 35 return false; 36 } 37 get()38 @Override public Object get() { 39 throw new IllegalStateException("value is absent"); 40 } 41 or(Object defaultValue)42 @Override public Object or(Object defaultValue) { 43 return checkNotNull(defaultValue, "use orNull() instead of or(null)"); 44 } 45 46 @SuppressWarnings("unchecked") // safe covariant cast or(Optional<?> secondChoice)47 @Override public Optional<Object> or(Optional<?> secondChoice) { 48 return (Optional) checkNotNull(secondChoice); 49 } 50 or(Supplier<?> supplier)51 @Override public Object or(Supplier<?> supplier) { 52 return checkNotNull(supplier.get(), 53 "use orNull() instead of a Supplier that returns null"); 54 } 55 orNull()56 @Override public Object orNull() { 57 return null; 58 } 59 asSet()60 @Override public Set<Object> asSet() { 61 return Collections.emptySet(); 62 } 63 64 @Override transform(Function<? super Object, V> function)65 public <V> Optional<V> transform(Function<? super Object, V> function) { 66 checkNotNull(function); 67 return Optional.absent(); 68 } 69 equals(Object object)70 @Override public boolean equals(Object object) { 71 return object == this; 72 } 73 hashCode()74 @Override public int hashCode() { 75 return 0x598df91c; 76 } 77 toString()78 @Override public String toString() { 79 return "Optional.absent()"; 80 } 81 readResolve()82 private Object readResolve() { 83 return INSTANCE; 84 } 85 86 private static final long serialVersionUID = 0; 87 } 88