Created
November 17, 2016 09:54
-
-
Save pablisco/5a78ec1932b2127eeeb650fc3ae7ba32 to your computer and use it in GitHub Desktop.
Object Matcher
This file contains hidden or 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 ObjectMatcher<T> { | |
private SparseArrayCompat<T> values = new SparseArrayCompat<>(); | |
public MatchCandidate matchWith(T value) { | |
return new MatchCandidate(value); | |
} | |
public T find(Object... keys) { | |
return values.get(Arrays.hashCode(keys)); | |
} | |
public class MatchCandidate { | |
private T value; | |
private MatchCandidate(T value) { | |
this.value = value; | |
} | |
public ObjectMatcher<T> when(Object... keys) { | |
values.put(Arrays.hashCode(keys), value); | |
return ObjectMatcher.this; | |
} | |
} | |
} |
This file contains hidden or 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
ObjectMatcher<String> matcher = new ObjectMatcher<String>() | |
.matchWith("Awesome item").when(GreatEnum.ConditionA, AwesomeService.KEY_SOMETHING); | |
String result = matcher.find(GreatEnum.ConditionA, AwesomeService.KEY_SOMETHING); | |
assert result.equals("Awesome item"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment