Coverage Summary for Class: Absent (com.google.common.base)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Absent | 100% (1/1) | 60% (9/15) | 52.9% (9/17) |
1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.base; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtCompatible; 20 import java.util.Collections; 21 import java.util.Set; 22 import javax.annotation.CheckForNull; 23 24 /** Implementation of an {@link Optional} not containing a reference. */ 25 @GwtCompatible 26 @ElementTypesAreNonnullByDefault 27 final class Absent<T> extends Optional<T> { 28 static final Absent<Object> INSTANCE = new Absent<>(); 29 30 @SuppressWarnings("unchecked") // implementation is "fully variant" 31 static <T> Optional<T> withType() { 32 return (Optional<T>) INSTANCE; 33 } 34 35 private Absent() {} 36 37 @Override 38 public boolean isPresent() { 39 return false; 40 } 41 42 @Override 43 public T get() { 44 throw new IllegalStateException("Optional.get() cannot be called on an absent value"); 45 } 46 47 @Override 48 public T or(T defaultValue) { 49 return checkNotNull(defaultValue, "use Optional.orNull() instead of Optional.or(null)"); 50 } 51 52 @SuppressWarnings("unchecked") // safe covariant cast 53 @Override 54 public Optional<T> or(Optional<? extends T> secondChoice) { 55 return (Optional<T>) checkNotNull(secondChoice); 56 } 57 58 @Override 59 public T or(Supplier<? extends T> supplier) { 60 return checkNotNull( 61 supplier.get(), "use Optional.orNull() instead of a Supplier that returns null"); 62 } 63 64 @Override 65 @CheckForNull 66 public T orNull() { 67 return null; 68 } 69 70 @Override 71 public Set<T> asSet() { 72 return Collections.emptySet(); 73 } 74 75 @Override 76 public <V> Optional<V> transform(Function<? super T, V> function) { 77 checkNotNull(function); 78 return Optional.absent(); 79 } 80 81 @Override 82 public boolean equals(@CheckForNull Object object) { 83 return object == this; 84 } 85 86 @Override 87 public int hashCode() { 88 return 0x79a31aac; 89 } 90 91 @Override 92 public String toString() { 93 return "Optional.absent()"; 94 } 95 96 private Object readResolve() { 97 return INSTANCE; 98 } 99 100 private static final long serialVersionUID = 0; 101 }