Last active
August 29, 2015 14:03
-
-
Save JensRantil/3063132784a0945dc6f0 to your computer and use it in GitHub Desktop.
Nullcheck abstraction ideas.
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
public class NullcheckAbstractionsExamples { | |
public void Example1( ) { | |
NullablePair pair; | |
pair = Nullables.pair(myObject1.getProperty(), myObject2.getProperty()); | |
if (pair.anyNull()) | |
return pair.compare(); | |
pair = Nullables.pair(myObject1.getProperty().getSubProperty(), myObject2.getProperty().getSubProperty()); | |
if (pair.anyNull()) | |
return pair.compare(); | |
return ComparisonChain.start().compare(myObject1.getProperty().getSubProperty().value(), | |
myObject2.getProperty().getSubProperty().value()); | |
} | |
public void Example2a() { | |
ComparisonChain chain = ComparisonChain.start(); | |
try { | |
chain = chain.compareNull(myObject1.getProperty(), myObject2.getProperty()); | |
chain = chain.compareNull(myObject1.getProperty().getSubProperty(), myObject2.getProperty().getSubProperty()); | |
} except (ComparisonChain.NullComparisonFallbackException e) { | |
return e.compare(); | |
} | |
return chain.compare(myObject1.getProperty().getSubProperty().value(), | |
myObject2.getProperty().getSubProperty().value()).result(); | |
} | |
public void Example2b() { | |
ComparisonChain chain = ComparisonChain.start(); | |
try { | |
chain = chain.compareNull(myObject1.getProperty(), myObject2.getProperty()); | |
chain = chain.compare(myObject1.getProperty().name(), myObject2.getProperty().name()); | |
chain = chain.compareNull(myObject1.getProperty().getSubProperty(), myObject2.getProperty().getSubProperty()); | |
chain = chain.compare(myObject1.getProperty().getSubProperty().value(), | |
myObject2.getProperty().getSubProperty().value()) | |
} except (ComparisonChain.NullComparisonFallbackException e) { | |
return e.compare(); | |
} | |
return chain.result() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment