Guava-Optional(譯)

原文地址
譯者java

Guava Optional Class

Optional 是一個不可變對象,用來包含一個非null對象。Optional使用absent來表達null值。該類提供了不少實用的方法來處理值是否可用,從而避免對null值進行檢查。git

類的聲明

如下是com.google.common.base.Optional<T> 類的聲明:github

@GwtCompatible(serializable=true)
public abstract class Optional<T>
   extends Object
      implements Serializable

類的方法

方法和描述

  1. static <T> Optional<T> absent()編輯器

    建立一個引用缺失的Optional實例。
  2. abstract Set<T> asSet()this

    返回一個不可變的單例的set集合,若是引用存在,則返回一個只包含一個元素的set集合,不然,返回一個空的不可變set集合。
  3. abstract boolean equals(Object object)google

    該equals對象屬於Optional類的方法,比較的規則以下:
    1.若給予的對象不是Optional及其子類,直接返回false
    2.給予的對象是Optional及其子類,則比較改optional中所包含的引用的值。
  4. static <T> Optional<T> fromNullable(T nullableReference)翻譯

    建立一個指定引用的Optional,若引用爲null則表示引用缺失,並返回一個absent()①。
  5. abstract T get()code

    返回包含的實例,該實例必須存在,若是不存在將會拋出java.lang.IllegalStateException異常。
  6. abstract int hashCode()orm

    返回該實例的哈希碼。
  7. abstract boolean isPresent()htm

    若是Optional包含非null引用實例則返回true。
  8. static <T> Optional<T> of(T reference)

    建立一個指定引用的Optional實例,若引用爲null則快速失敗。
  9. abstract Optional<T> or(Optional<? extends T> secondChoice)

    Returns this Optional if it has a value present; secondChoice otherwise.
  10. abstract T or(Supplier<? extends T> supplier)

    Returns the contained instance if it is present; supplier.get() otherwise.
  11. abstract T or(T defaultValue)

    若是包含的實例存在,則返回,若是不存在則返回給予的默認值。
  12. abstract T orNull()

    若是包含的實例存在則返回該實例,若是不存在則返回null。
  13. static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>>optionals)

    Returns the value of each present instance from the supplied optionals,in order, skipping over occurrences of absent().
  14. abstract String toString()

    返回實例的字符串表示,默認實現只有兩種表示方法若Optional中包含的引用缺失則返回optional.absent()不然返回optional.of(引用的值)
  15. abstract <V> Optional<V> transform(Function<? super T,V> function)

    If the instance is present, it is transformed with the given Function; 
    otherwise, absent() is returned.

方法繼承

該類所繼承的方法來自類Object:

java.lang.Object

Optional 類實例

使用任何編輯器建立一下程序:

GuavaTester.java

import com.google.common.base.Optional;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester guavaTester = new GuavaTester();

      Integer value1 =  null;
      Integer value2 =  new Integer(10);
      
      //Optional.fromNullable - allows passed parameter to be null.
      Optional<Integer> a = Optional.fromNullable(value1);
      
      //Optional.of - throws NullPointerException if passed parameter is null
      Optional<Integer> b = Optional.of(value2);        

      System.out.println(guavaTester.sum(a,b));
   }

   public Integer sum(Optional<Integer> a, Optional<Integer> b) {
      //Optional.isPresent - checks the value is present or not
      System.out.println("First parameter is present: " + a.isPresent());

      System.out.println("Second parameter is present: " + b.isPresent());

      //Optional.or - returns the value if present otherwise returns
      //the default value passed.
      Integer value1 = a.or(new Integer(0));    

      //Optional.get - gets the value, value should be present
      Integer value2 = b.get();

      return value1 + value2;
   }    
}

校驗結果

在控制檯使用 javac 命令編譯,編譯結果以下:

C:\Guava>javac GuavaTester.java
Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester
See the result.
First parameter is present: false
Second parameter is present: true
10

注①:請看一下來自Guava的部分代碼

public static Optional fromNullable(Object nullableReference)
{
    return ((Optional) (nullableReference != null ? new Present(nullableReference) : absent()));
}

抽象類Optional只有兩個實現類Present和absent,這兩個類分別表示存在以及缺失狀態。調用fromNullable方法
而且指定的引用爲null的時候,會調用absent方法,來生成Optional對象,實際上與Optional.absent()一致。

說明:有三個方法沒有做解釋,主要是擔憂相關知識不理解,容易作出錯誤的翻譯,望請諒解!

相關文章
相關標籤/搜索