Skip to content

Instantly share code, notes, and snippets.

@yamen
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamen/8744593 to your computer and use it in GitHub Desktop.
Save yamen/8744593 to your computer and use it in GitHub Desktop.
XRefService with LookupResult
package au.com.sixtree.common.services.xref;
import java.util.ArrayList;
import java.util.Collection;
import au.com.sixtree.common.services.xref.exceptions.UnexpectedResultsException;
public class LookupResult {
private ArrayList<String> _result;
public LookupResult() {
_result = new ArrayList<String>();
}
public LookupResult(Collection<String> from) {
_result = new ArrayList<String>(from);
}
public Collection<String> all() {
return _result;
}
public String firstOrDefault(String defaultValue) {
if (_result.size() > 0) {
return _result.get(0);
} else {
return defaultValue;
}
}
public String first() throws UnexpectedResultsException {
if (_result.size() == 0) throw new UnexpectedResultsException(UnexpectedResultsException.AT_LEAST_ONE, _result.size());
return _result.get(0);
}
public String single() throws UnexpectedResultsException {
if (_result.size() > 1) throw new UnexpectedResultsException(UnexpectedResultsException.EXACTLY_ONE, _result.size());
return first();
}
public String singleOrDefault(String defaultValue) throws UnexpectedResultsException {
if (_result.size() > 1) throw new UnexpectedResultsException(UnexpectedResultsException.AT_MOST_ONE, _result.size());
return firstOrDefault(defaultValue);
}
public Collection<String> many() throws UnexpectedResultsException {
if (_result.size() == 0) throw new UnexpectedResultsException(UnexpectedResultsException.AT_LEAST_ONE, _result.size());
return _result;
}
public Collection<String> manyOrDefault(String defaultValue) {
if (_result.size() == 0) {
_result = new ArrayList<String>();
_result.add(defaultValue);
}
return _result;
}
}
package au.com.sixtree.common.services.xref.exceptions;
public class UnexpectedResultsException extends XRefException {
public static final String AT_LEAST_ONE = "at least one";
public static final String EXACTLY_ONE = "exactly one";
public static final String AT_MOST_ONE = "at most one";
public UnexpectedResultsException(String message) {
super(message);
}
public UnexpectedResultsException(String expected, int actual) {
this(String.format("Expected %s result returned, got %i", expected, actual));
}
}
package au.com.sixtree.common.services.xref.exceptions;
public class XRefException extends Exception {
public XRefException(String message) {
super(message);
}
}
package au.com.sixtree.common.services.xref;
public interface XRefService {
public LookupResult lookup(String entity, String sourceValue,
String sourceAttribute, String sourceParticipant,
String targetAttribute, String targetParticipant);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment