Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Hallgren2009-12-21 15:53:41 +0000
committerThomas Hallgren2009-12-21 15:53:41 +0000
commitf669d20c4b764018aca26e2f9685a0efc3fd939e (patch)
tree05cf44425d577fbd372ae3e8dc41aabb5252c0e2 /bundles/org.eclipse.equinox.p2.ql
parente19bcd774063f2b43556b77375b35c57dedb6395 (diff)
downloadrt.equinox.p2-f669d20c4b764018aca26e2f9685a0efc3fd939e.tar.gz
rt.equinox.p2-f669d20c4b764018aca26e2f9685a0efc3fd939e.tar.xz
rt.equinox.p2-f669d20c4b764018aca26e2f9685a0efc3fd939e.zip
298023: Simplify the IQueryResult
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.ql')
-rw-r--r--bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/QueryContext.java4
-rw-r--r--bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/Expression.java4
-rw-r--r--bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/WrappedIQuery.java25
-rw-r--r--bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLContextQuery.java12
-rw-r--r--bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLMatchQuery.java11
-rw-r--r--bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QueryResult.java78
6 files changed, 93 insertions, 41 deletions
diff --git a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/QueryContext.java b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/QueryContext.java
index 39e3d5984..3d7b5b703 100644
--- a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/QueryContext.java
+++ b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/QueryContext.java
@@ -23,7 +23,7 @@ public class QueryContext implements IQueryContext {
final IRepeatableIterator repeatable = RepeatableIterator.create(iterator);
this.queryable = new IQueryable() {
public IQueryResult query(IQuery query, IProgressMonitor monitor) {
- return query.perform(repeatable.getCopy(), new Collector());
+ return query.perform(repeatable.getCopy());
}
};
}
@@ -45,7 +45,7 @@ public class QueryContext implements IQueryContext {
public Iterator iterator() {
final Iterator[] iteratorCatcher = new Iterator[1];
queryable.query(new ContextQuery() {
- public Collector perform(Iterator iterator, Collector result) {
+ public IQueryResult perform(Iterator iterator) {
iteratorCatcher[0] = iterator;
return null;
}
diff --git a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/Expression.java b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/Expression.java
index 7992dda5b..46f542964 100644
--- a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/Expression.java
+++ b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/Expression.java
@@ -34,7 +34,7 @@ abstract class Expression implements IExpression, IParserConstants {
if (provider instanceof Set)
return (Set) provider;
if (provider instanceof Collector)
- return (Set) ((Collector) provider).toCollection();
+ return ((Collector) provider).unmodifiableSet();
}
if (provider instanceof Collection)
@@ -44,7 +44,7 @@ abstract class Expression implements IExpression, IParserConstants {
if (val instanceof Set)
return (Set) val;
if (val instanceof Collector)
- return (Set) ((Collector) val).toCollection();
+ return ((Collector) val).unmodifiableSet();
}
}
diff --git a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/WrappedIQuery.java b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/WrappedIQuery.java
index 88fbbdddb..282fe452c 100644
--- a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/WrappedIQuery.java
+++ b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/internal/p2/ql/expression/WrappedIQuery.java
@@ -11,16 +11,14 @@
package org.eclipse.equinox.internal.p2.ql.expression;
import java.util.Iterator;
-import org.eclipse.equinox.internal.provisional.p2.metadata.query.Collector;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.IMatchQuery;
import org.eclipse.equinox.p2.metadata.query.IQuery;
import org.eclipse.equinox.p2.ql.IEvaluationContext;
-import org.eclipse.equinox.p2.ql.QLContextQuery;
final class WrappedIQuery extends Function {
public WrappedIQuery(Expression[] operands) {
- super(assertLength(operands, 1, 3, KEYWORD_IQUERY));
+ super(assertLength(operands, 1, 2, KEYWORD_IQUERY));
assertNotBoolean(operands[0], "parameter"); //$NON-NLS-1$
assertNotCollection(operands[0], "parameter"); //$NON-NLS-1$
}
@@ -29,9 +27,6 @@ final class WrappedIQuery extends Function {
Object query = operands[0].evaluate(context);
if (query instanceof IMatchQuery) {
- if (operands.length > 2)
- throw new IllegalArgumentException("iquery third argument cannot be combined with a match query"); //$NON-NLS-1$
-
Object value = null;
if (operands.length > 1)
value = operands[1].evaluate(context);
@@ -43,29 +38,13 @@ final class WrappedIQuery extends Function {
if (!(query instanceof IQuery))
throw new IllegalArgumentException("iquery first argument must be an IQuery instance"); //$NON-NLS-1$
- Collector collector = null;
- if (operands.length == 3) {
- Object cobj = operands[2].evaluate(context);
- if (cobj instanceof Collector)
- collector = (Collector) cobj;
- else if (cobj == null)
- collector = new Collector();
- else
- throw new IllegalArgumentException("iquery third argument must be a collector"); //$NON-NLS-1$
- }
-
Iterator iterator = null;
if (operands.length > 1)
iterator = operands[1].evaluateAsIterator(context);
else
iterator = Variable.EVERYTHING.evaluateAsIterator(context);
- if (collector == null) {
- if (query instanceof QLContextQuery)
- return ((QLContextQuery) query).evaluate(iterator);
- collector = new Collector();
- }
- return ((IQuery) query).perform(iterator, collector);
+ return ((IQuery) query).perform(iterator);
}
String getOperator() {
diff --git a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLContextQuery.java b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLContextQuery.java
index 9d97f823b..9e3b765d8 100644
--- a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLContextQuery.java
+++ b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLContextQuery.java
@@ -11,8 +11,8 @@
package org.eclipse.equinox.p2.ql;
import java.util.Iterator;
-import org.eclipse.equinox.internal.provisional.p2.metadata.query.Collector;
import org.eclipse.equinox.p2.metadata.IVersionedId;
+import org.eclipse.equinox.p2.metadata.query.IQueryResult;
/**
* An IQuery 'context query' implementation that is based on the p2 query language.
@@ -87,14 +87,8 @@ public class QLContextQuery extends QLQuery {
this(IVersionedId.class, expression, new Object[] {param1, param2, param3});
}
- public Collector perform(Iterator iterator, Collector collector) {
- iterator = evaluate(iterator);
- while (iterator.hasNext()) {
- Object nxt = iterator.next();
- if (!collector.accept(nxt))
- break;
- }
- return collector;
+ public IQueryResult perform(Iterator iterator) {
+ return new QueryResult(evaluate(iterator));
}
public Iterator evaluate(Iterator iterator) {
diff --git a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLMatchQuery.java b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLMatchQuery.java
index c639b6789..b43dc2f7f 100644
--- a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLMatchQuery.java
+++ b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QLMatchQuery.java
@@ -10,10 +10,11 @@
*******************************************************************************/
package org.eclipse.equinox.p2.ql;
+import java.util.ArrayList;
import java.util.Iterator;
-import org.eclipse.equinox.internal.provisional.p2.metadata.query.Collector;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.IMatchQuery;
import org.eclipse.equinox.p2.metadata.IVersionedId;
+import org.eclipse.equinox.p2.metadata.query.IQueryResult;
/**
* An IQuery implementation that is based on the p2 query language.
@@ -105,7 +106,7 @@ public class QLMatchQuery extends QLQuery implements IMatchQuery {
//
}
- public Collector perform(Iterator iterator, Collector collector) {
+ public IQueryResult perform(Iterator iterator) {
if (expression.needsTranslations()) {
IQueryContext queryContext = QL.newQueryContext(iterator);
context = expression.createContext(parameters, queryContext.getTranslationSupport(getLocale()));
@@ -114,13 +115,13 @@ public class QLMatchQuery extends QLQuery implements IMatchQuery {
prePerform();
try {
+ ArrayList result = new ArrayList();
while (iterator.hasNext()) {
Object candidate = iterator.next();
if (isMatch(candidate))
- if (!collector.accept(candidate))
- break;
+ result.add(candidate);
}
- return collector;
+ return new QueryResult(result);
} finally {
postPerform();
}
diff --git a/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QueryResult.java b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QueryResult.java
new file mode 100644
index 000000000..d634d405f
--- /dev/null
+++ b/bundles/org.eclipse.equinox.p2.ql/src/org/eclipse/equinox/p2/ql/QueryResult.java
@@ -0,0 +1,78 @@
+package org.eclipse.equinox.p2.ql;
+
+import java.lang.reflect.Array;
+import java.util.*;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.equinox.internal.p2.ql.IRepeatableIterator;
+import org.eclipse.equinox.internal.p2.ql.RepeatableIterator;
+import org.eclipse.equinox.internal.provisional.p2.metadata.query.Collector;
+import org.eclipse.equinox.p2.metadata.query.IQuery;
+import org.eclipse.equinox.p2.metadata.query.IQueryResult;
+
+class QueryResult implements IQueryResult {
+
+ private final IRepeatableIterator iterator;
+
+ QueryResult(Iterator iterator) {
+ this.iterator = RepeatableIterator.create(iterator);
+ }
+
+ QueryResult(Collection collection) {
+ this.iterator = RepeatableIterator.create(collection);
+ }
+
+ public boolean isEmpty() {
+ return !iterator.hasNext();
+ }
+
+ public Iterator iterator() {
+ return iterator.getCopy();
+ }
+
+ public Object[] toArray(Class clazz) {
+ Object provider = iterator.getIteratorProvider();
+ if (provider.getClass().isArray())
+ return (Object[]) provider;
+
+ if (provider instanceof Collector)
+ return ((Collector) provider).toArray(clazz);
+
+ Collection c = (Collection) provider;
+ return c.toArray((Object[]) Array.newInstance(clazz, c.size()));
+ }
+
+ public Set toSet() {
+ Object provider = iterator.getIteratorProvider();
+ if (provider.getClass().isArray()) {
+ Object[] elems = (Object[]) provider;
+ int idx = elems.length;
+ HashSet copy = new HashSet(idx);
+ while (--idx >= 0)
+ copy.add(elems[idx]);
+ return copy;
+ }
+ if (provider instanceof Collector)
+ return ((Collector) provider).toSet();
+ if (provider instanceof Map)
+ return new HashSet(((Map) provider).entrySet());
+ return new HashSet((Collection) provider);
+ }
+
+ public IQueryResult query(IQuery query, IProgressMonitor monitor) {
+ return query.perform(iterator());
+ }
+
+ public Set unmodifiableSet() {
+ Object provider = iterator.getIteratorProvider();
+ if (provider instanceof Collector)
+ return ((Collector) provider).unmodifiableSet();
+
+ if (provider instanceof Set)
+ return Collections.unmodifiableSet((Set) provider);
+
+ if (provider instanceof Map)
+ return Collections.unmodifiableSet(((Map) provider).entrySet());
+
+ return toSet();
+ }
+}

Back to the top