Valkyr Posted February 17, 2016 Share Posted February 17, 2016 (edited) Wrote this a while back, basically a replacement for EntityAPI IQuery.java import com.zenscripting.api.script.engine.Context; import java.util.Comparator; import java.util.List; import java.util.function.Predicate; /** * Created by Valkyr on 17/12/2015. */ /** * Query * * @param <T> Type of query * @param <R> Result table type * @param <Q> Query type */ public interface IQuery<T, R extends IQueryResult<T>> { /** * Returns the API context * * @return context */ Context getContext(); /** * Returns the list of T to be queried. * * @return List of T */ List<T> load(); /** * Returns the results from filtering items using the * provided predicate. * * @param predicate a predicate to filter the query * @return this query */ IQuery<T, R> filter(Predicate<T> predicate); /** * Sorts results according to the comparator * * @return this query */ IQuery<T, R> sort(Comparator<T> comparator); /** * Removes non-matching items * * @param subType * @return this query, modified for subclass */ <ST extends T> Query<ST, QueryResult<ST>> bySubClass(Class<ST> subType); /** * Returns the results of the query * * @return result */ R result(); } IQueryBuilder.java import org.osbot.rs07.script.Script; /** * Created by Valkyr on 17/12/2015. */ /** * Query Builder * Builds a query using given parameters * * @param <T> Object to use * @param <R> QueryResult type to use */ public interface IQueryBuilder<T, R extends IQueryResult<T>, Q extends IQuery<T, R>> { /** * Returns the API context * * @return context */ Script getScript(); /** * Returns a new Query of type T * * @return new query */ Q newQuery(); } IQueryResult.java import java.util.Comparator; import java.util.List; /** * Created by Valkyr on 17/12/2015. */ /** * Query Result * * @param <T> Type of query */ public interface IQueryResult<T> { /** * Returns the first item in the results table * * @return first found item */ T first(); /** * Returns the last item in the results table * * @return last found item */ T last(); /** * Returns the item at the provided index * * @param index * @return found item at index */ T get(int index); /** * Returns all items in the results table * * @return last found item */ List<T> all(); /** * Determines whether or not the results table is empty. * * @return results table is empty */ boolean isEmpty(); } Query.java import org.osbot.rs07.script.Script; import java.util.Comparator; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; /** * Created by Valkyr on 17/12/2015. */ public abstract class Query<T, R extends QueryResult<T>> implements IQuery<T, R> { private final Script script; private List<T> raw; public Query(Script script) { this.script = script; raw = load(); } public final Script getScript() { return script; } @Override public final Query<T, R> filter(Predicate<T> predicate) { if (raw != null && raw.size() > 0) { raw.removeAll(raw.stream().filter(t -> !predicate.test(t)).collect(Collectors.toList())); } return getThis(); } @Override public final Query<T, R> sort(Comparator<T> comparator) { raw = raw.stream().sorted(comparator).collect(Collectors.toList()); return getThis(); } @Override public final <ST extends T> Query<ST, QueryResult<ST>> bySubClass(Class<ST> subType) { if (raw != null && raw.size() > 0) { filter(t -> t != null && subType.isAssignableFrom(t.getClass())); } return (Query<ST, QueryResult<ST>>) getThis(); } protected final List<T> getRaw() { return raw; } protected final <SQ extends Query<T, R>> SQ getThis() { return (SQ) this; } public abstract R result(); } QueryBuilder.java import org.osbot.rs07.script.Script; /** * Created by Valkyr on 17/12/2015. */ public abstract class QueryBuilder<T, R extends IQueryResult<T>, Q extends IQuery<T, R>> implements IQueryBuilder<T, R, Q> { private final Script script; public QueryBuilder(Script context) { this.script = context; } public final Script getScript() { return script; } } QueryResult.java import java.util.List; /** * Created by Valkyr on 17/12/2015. */ public abstract class QueryResult<T> implements IQueryResult<T> { private final List<T> results; public QueryResult(List<T> results) { this.results = results; } @Override public T first() { if (!isEmpty()) return results.get(0); return null; } @Override public T last() { if (!isEmpty()) return results.get(results.size()); return null; } @Override public T get(int index) { if (!isEmpty()) return results.get(index); return null; } @Override public List<T> all() { if (!isEmpty()) return results; return null; } @Override public boolean isEmpty() { return results.size() < 1; } } Edited February 17, 2016 by Valkyr 3 Quote Link to comment Share on other sites More sharing options...
Gold Scripts Posted February 17, 2016 Share Posted February 17, 2016 Nice, looks pretty good Quote Link to comment Share on other sites More sharing options...
Aeonx Posted February 19, 2016 Share Posted February 19, 2016 Thanks, really helpful. Quote Link to comment Share on other sites More sharing options...