summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Pitschke2012-06-30 14:58:45 (EDT)
committer Steve Pitschke2012-06-30 14:58:45 (EDT)
commit415a65c6af65e233c13da16693a84df9c08c18b8 (patch)
tree3bab9f70ffb0066d681c9fe4c1687ea1eb94d0c1
parente1f1ff1390b001226ec6e73d8c74cfb5cc8101c0 (diff)
downloadorg.eclipse.lyo.core-415a65c6af65e233c13da16693a84df9c08c18b8.zip
org.eclipse.lyo.core-415a65c6af65e233c13da16693a84df9c08c18b8.tar.gz
org.eclipse.lyo.core-415a65c6af65e233c13da16693a84df9c08c18b8.tar.bz2
Bug 383469: Implement toString() methods on where termsrefs/changes/61/6561/1
Change-Id: I861f0bf11e051531fa326cab5f53c049bde467df Signed-off-by: Steve Pitschke <pitschke@us.ibm.com>
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/WhereClause.java4
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ComparisonTermInvocationHandler.java12
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/CompoundTermInvocationHandler.java36
-rw-r--r--org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/InTermInvocationHandler.java32
-rw-r--r--org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java69
5 files changed, 78 insertions, 75 deletions
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/WhereClause.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/WhereClause.java
index 4901e06..1e7bd93 100644
--- a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/WhereClause.java
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/WhereClause.java
@@ -17,8 +17,8 @@
package org.eclipse.lyo.core.query;
/**
- * Top-level {@link ComparisonTerm} from olsc.where clause
+ * Top-level {@link CompoundTerm} from olsc.where clause
*/
-public interface WhereClause
+public interface WhereClause extends CompoundTerm
{
}
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ComparisonTermInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ComparisonTermInvocationHandler.java
index e0e125f..f40c551 100644
--- a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ComparisonTermInvocationHandler.java
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/ComparisonTermInvocationHandler.java
@@ -86,7 +86,10 @@ class ComparisonTermInvocationHandler extends SimpleTermInvocationHandler
return operator;
}
- if (! methodName.equals("operand")) {
+ boolean isOperand = methodName.equals("operand");
+
+ if (! isOperand &&
+ ! methodName.equals("toString")) {
return super.invoke(proxy, method, args);
}
@@ -98,7 +101,12 @@ class ComparisonTermInvocationHandler extends SimpleTermInvocationHandler
prefixMap);
}
- return operand;
+ if (isOperand) {
+ return operand;
+ }
+
+ return ((ComparisonTerm)proxy).property().toString() +
+ operator.toString() + operand.toString();
}
static Value
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/CompoundTermInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/CompoundTermInvocationHandler.java
index f25a2a4..a5ae452 100644
--- a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/CompoundTermInvocationHandler.java
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/CompoundTermInvocationHandler.java
@@ -62,7 +62,11 @@ public class CompoundTermInvocationHandler extends SimpleTermInvocationHandler
Object[] args
) throws Throwable
{
- if (! method.getName().equals("children")) {
+ String methodName = method.getName();
+ boolean isChildren = methodName.equals("children");
+
+ if (! isChildren &&
+ ! methodName.equals("toString")) {
return super.invoke(proxy, method, args);
}
@@ -115,7 +119,35 @@ public class CompoundTermInvocationHandler extends SimpleTermInvocationHandler
children = Collections.unmodifiableList(children);
- return children;
+ if (isChildren) {
+ return children;
+ }
+
+ StringBuffer buffer = new StringBuffer();
+
+ if (! isTopLevel) {
+ buffer.append(((CompoundTerm)proxy).property().toString());
+ buffer.append('{');
+ }
+
+ boolean first = true;
+
+ for (SimpleTerm term : children) {
+
+ if (first) {
+ first = false;
+ } else {
+ buffer.append(" and ");
+ }
+
+ buffer.append(term.toString());
+ }
+
+ if (! isTopLevel) {
+ buffer.append('}');
+ }
+
+ return buffer.toString();
}
private final CommonTree tree;
diff --git a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/InTermInvocationHandler.java b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/InTermInvocationHandler.java
index 50ff157..33659e1 100644
--- a/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/InTermInvocationHandler.java
+++ b/org.eclipse.lyo.core.query/src/main/java/org/eclipse/lyo/core/query/impl/InTermInvocationHandler.java
@@ -52,7 +52,11 @@ class InTermInvocationHandler extends SimpleTermInvocationHandler
Object[] args
) throws Throwable
{
- if (! method.getName().equals("values")) {
+ String methodName = method.getName();
+ boolean isValues = methodName.equals("values");
+
+ if (! isValues &&
+ ! methodName.equals("toString")) {
return super.invoke(proxy, method, args);
}
@@ -77,7 +81,31 @@ class InTermInvocationHandler extends SimpleTermInvocationHandler
values = Collections.unmodifiableList(values);
}
- return values;
+ if (isValues) {
+ return values;
+ }
+
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append(((InTerm)proxy).property().toString());
+ buffer.append(" in [");
+
+ boolean first = true;
+
+ for (Value value : values) {
+
+ if (first) {
+ first = false;
+ } else {
+ buffer.append(',');
+ }
+
+ buffer.append(value.toString());
+ }
+
+ buffer.append(']');
+
+ return buffer.toString();
}
private List<Value> values = null;
diff --git a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java
index 835aa54..e1832b2 100644
--- a/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java
+++ b/org.eclipse.lyo.core.query/src/test/java/org/eclipse/lyo/core/query/test/BasicWhereTest.java
@@ -67,18 +67,14 @@ public class BasicWhereTest
OslcWhereParser.oslc_where_return resultTree =
parser.oslc_where();
- CompoundTerm whereClause = (CompoundTerm)
+ WhereClause whereClause = (WhereClause)
Proxy.newProxyInstance(CompoundTerm.class.getClassLoader(),
new Class<?>[] { CompoundTerm.class, WhereClause.class },
new CompoundTermInvocationHandler(
(CommonTree)resultTree.getTree(), true,
prefixMap));
- System.out.println("Is where: " + (whereClause instanceof WhereClause));
-
- dumpCompound(whereClause);
-
- System.out.println();
+ System.out.println("Is where: " + whereClause);
} catch (RecognitionException e) {
// TODO Auto-generated catch block
@@ -86,65 +82,4 @@ public class BasicWhereTest
}
}
}
-
- private static void
- dumpCompound(CompoundTerm compoundTerm)
- {
- List<SimpleTerm> children = compoundTerm.children();
- boolean first = true;
-
- for (SimpleTerm term : children) {
-
- if (first) {
- first = false;
- } else {
- System.out.print(" and ");
- }
-
- System.out.print(term.property().toString());
-
- switch (term.type()) {
- case COMPARISON:
- {
- ComparisonTerm compTerm = (ComparisonTerm)term;
-
- System.out.print(compTerm.operator().toString());
- System.out.print(compTerm.operand().toString());
- }
- break;
- case IN_TERM:
- {
- System.out.print(" in [");
-
- InTerm inTerm = (InTerm)term;
- boolean firstValue = true;
-
- for (Value value : inTerm.values()) {
-
- if (firstValue) {
- firstValue = false;
- } else {
- System.out.print(',');
- }
-
- System.out.print(value.toString());
- }
-
- System.out.print(']');
- }
- break;
- case NESTED:
- {
- System.out.print('{');
-
- dumpCompound((CompoundTerm)term);
-
- System.out.print('}');
- }
- break;
- default:
- throw new IllegalStateException("unknown simple term type");
- }
- }
- }
}