Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
  
  package base;
  
 import java.util.List;
 import java.util.Map;

Implemented by classes that allow doing a query for one or more data objects.
 
 public abstract class Query<Q extends QueryResult> {

Execute the query using an optional variable number of parameters. Should it not be possible to complete the execution of the query for any reason, the query must throw an IOException. Data must never be altered as a result of making a call to a query object.

Throws:
java.io.IOException Thrown if and only if the command can not be completed.
 
 	public List<Q> run() throws IOException {
 		return execute();
 	}
 
 	protected abstract List<Q> execute() throws IOException;
 
 	public abstract String getJsonParameters();
 
 	public String toJson() {
 		return "{\"q\":\"" + getClass().getSimpleName() + "\",\"p\":{" + getJsonParameters() + "}}";
 	}
 
 	public Query<Q> newWithParameters(Map<StringObjectparametersthrows IOException {
 		throw new IllegalArgumentException(getClass().getSimpleName() + " may not be instantiated with parameters");
 	}

Return the value of the string passed in, or if what was passed in was a string array, return the first element in the string array. Used by newWithParameters method to read strings.

Parameters:
parameterName
o
Returns:
The first string element in object o
See also:
newWithParameters(java.util.Map)
 
 	protected String objectToString(String parameterNameObject o) {
 		if(o == null) {
 			return null;
 		}
 		if(o instanceof String) {
 			return (String)o;
 		}
 		if(o instanceof String[]) {
 			return ((String[])o)[0];
 		}
 		throw new IllegalArgumentException("Parameter " + parameterName + " must be a string.");
 	}

Return an array of strings, assuming the input parameter is either an array of strings, or a single string. Used by newWithParameters method to read string arrays.

Parameters:
parameterName
o
Returns:
String array based on o
See also:
newWithParameters(java.util.Map)
 
 	protected String[] objectToStringArray(String parameterNameObject o) {
 		if(o == null) {
 			return null;
 		}
 		if(o instanceof String) {
 			return new String[]{(String)o};
 		}
 		if(o instanceof String[]) {
 			return (String[])o;
 		}
 		throw new IllegalArgumentException("Parameter " + parameterName + " must be a string array.");
 	}

Used by newWithParameters method to read integer numbers.

Parameters:
parameterName
o
Returns:
Return the first integer found in the object, or the default value if no integer found.
See also:
newWithParameters(java.util.Map)
 
 	protected int objectToInteger(String parameterNameObject oint defaultValue) {
 		if(o == null) {
 			return defaultValue;
 		}
		if(o instanceof String) {
			return Integer.parseInt((String)o);
		}
		if(o instanceof Integer) {
			return (Integer)o;
		}
		throw new IllegalArgumentException("Parameter " + parameterName + " must be an integer.");
	}

Used by newWithParameters method to read long numbers.

Parameters:
parameterName
o
Returns:
Return the first long found in the object, or the default value if no long found.
See also:
newWithParameters(java.util.Map)
	protected long objectToLong(String parameterNameObject olong defaultValue) {
		if(o == null) {
			return defaultValue;
		}
		if(o instanceof String) {
			return Long.parseLong((String)o);
		}
		if(o instanceof Long) {
			return (Long)o;
		}
		if(o instanceof Integer) {
			return (Integer)o;
		}
		throw new IllegalArgumentException("Parameter " + parameterName + " must be a long integer.");
	}
New to GrepCode? Check out our FAQ X