package com.xplus.commons.guava.basic;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.base.Optional;

/**
 * 使用和避免null：null是模棱两可的，会引起令人困惑的错误，有些时候它让人很不舒服。<br>
 * 很多Guava工具类用快速失败拒绝null值，而不是盲目地接受。
 * 
 * @author huzexiong
 *
 */
public class A_1_1_OptionalTest {

  @Test
  public void test() {
    Optional<Integer> possible = Optional.of(5);
    Assert.assertEquals("相同", true, possible.isPresent());
    Assert.assertEquals("相同", true, 5 == possible.get());
    Assert.assertEquals("相同", true, 5 == possible.or(3));
    Assert.assertEquals("相同", true, possible.asSet().size() == 1);
    Optional<Integer[]> possibles = Optional.of(new Integer[] {
        1, 2, 3
    });
    Assert.assertEquals("相同", true, possibles.asSet().size() == 1);
  }
  
  @Test
  public void test2() {
    Optional<Integer> possible = Optional.absent();
    Assert.assertEquals("相同", false, possible.isPresent());
    Assert.assertEquals("相同", true, null == possible.orNull());
    Assert.assertEquals("相同", true, 3 == possible.or(3));
    Assert.assertEquals("相同", true, Optional.absent().equals(Optional.fromNullable(null)));
  }
  
  @Test
  public void test3() {
    Optional<Integer> possible = Optional.of(8);
    Assert.assertEquals("相同", true, possible.isPresent());
    Assert.assertEquals("相同", false, null == possible.orNull());
    Assert.assertEquals("相同", true, 8 == possible.or(3));
    Assert.assertEquals("相同", true, Optional.absent().equals(Optional.fromNullable(null)));
  }
  
}

@@ -83,24 +83,68 @@
 it.Test;%0D%0A%0D%0A
+import com.google.common.base.MoreObjects;%0D%0A
 import com.g
@@ -172,16 +172,16 @@
 ional;%0D%0A
-
 %0D%0A/**%0D%0A 
@@ -751,21 +751,16 @@
  1, 2, 3
-%0D%0A   
  %7D);%0D%0A  
@@ -823,36 +823,34 @@
 () == 1);%0D%0A  %7D%0D%0A
-  
 %0D%0A
+
   @Test%0D%0A  publi
@@ -1203,20 +1203,18 @@
 ;%0D%0A  %7D%0D%0A
-  
 %0D%0A
+
   @Test%0D
@@ -1572,15 +1572,296 @@
 ;%0D%0A  %7D%0D%0A
-  
+%0D%0A  @Test%0D%0A  public void test4() %7B%0D%0A    Assert.assertEquals(%22%E7%9B%B8%E5%90%8C%22, true, 5 == MoreObjects.firstNonNull(null, 5));%0D%0A    System.out.println(%0D%0A        MoreObjects.toStringHelper(this) //%0D%0A        .omitNullValues() //%0D%0A        .add(%22x%22, 1).add(%22y%22, %22foo%22) //%0D%0A        .toString());%0D%0A  %7D%0D%0A
 %0D%0A%7D%0D%0A

Add MoreObjects.