Coverage Summary for Class: Present (com.google.common.base)

Class Class, % Method, % Line, %
Present 100% (1/1) 66.7% (8/12) 68.2% (15/22)


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