Created
November 25, 2016 07:00
-
-
Save rohitvvv/bf7b16153bdbde771e1836985863f206 to your computer and use it in GitHub Desktop.
Guava Null Treatmenat and use of Optional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package GauvaDemo; | |
import static com.google.common.base.Preconditions.*; | |
import com.google.common.base.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Just a snippet on Optional and usage of Preconditions in Guava. | |
* | |
*/ | |
public class App { | |
public static void main(String[] args) { | |
Optional<String> returned = methodThatReturnsSomething(new ArrayList()); | |
if(!returned.isPresent()) | |
System.out.println("Rohit Not found"); | |
List<String> list = new ArrayList<String>(); | |
list.add("Rohit"); | |
returned = methodThatReturnsSomething(list); | |
if(returned.isPresent()) | |
System.out.println("Rohit Found"); | |
foo(null); | |
} | |
static Optional<String> methodThatReturnsSomething(List<String> list){ | |
if(list.contains("Rohit")) | |
return Optional.of("Rohit"); | |
else | |
return Optional.absent(); | |
} | |
static int foo(Integer bar){ | |
checkNotNull(bar); | |
return bar; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment