Home | History | Annotate | Download | only in base
      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} containing a reference.
     30  */
     31 @GwtCompatible
     32 final class Present<T> extends Optional<T> {
     33   private final T reference;
     34 
     35   Present(T reference) {
     36     this.reference = reference;
     37   }
     38 
     39   @Override public boolean isPresent() {
     40     return true;
     41   }
     42 
     43   @Override public T get() {
     44     return reference;
     45   }
     46 
     47   @Override public T or(T defaultValue) {
     48     checkNotNull(defaultValue, "use Optional.orNull() instead of Optional.or(null)");
     49     return reference;
     50   }
     51 
     52   @Override public Optional<T> or(Optional<? extends T> secondChoice) {
     53     checkNotNull(secondChoice);
     54     return this;
     55   }
     56 
     57   @Override public T or(Supplier<? extends T> supplier) {
     58     checkNotNull(supplier);
     59     return reference;
     60   }
     61 
     62   @Override public T orNull() {
     63     return reference;
     64   }
     65 
     66   @Override public Set<T> asSet() {
     67     return Collections.singleton(reference);
     68   }
     69 
     70   @Override public <V> Optional<V> transform(Function<? super T, V> function) {
     71     return new Present<V>(checkNotNull(function.apply(reference),
     72         "the Function passed to Optional.transform() must not return null."));
     73   }
     74 
     75   @Override public boolean equals(@Nullable Object object) {
     76     if (object instanceof Present) {
     77       Present<?> other = (Present<?>) object;
     78       return reference.equals(other.reference);
     79     }
     80     return false;
     81   }
     82 
     83   @Override public int hashCode() {
     84     return 0x598df91c + reference.hashCode();
     85   }
     86 
     87   @Override public String toString() {
     88     return "Optional.of(" + reference + ")";
     89   }
     90 
     91   private static final long serialVersionUID = 0;
     92 }
     93