cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1419094 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/client/ test/java/org/apache/cxf/jaxrs/ext/search/client/
Date Sun, 09 Dec 2012 19:00:51 GMT
Author: sergeyb
Date: Sun Dec  9 19:00:50 2012
New Revision: 1419094

URL: http://svn.apache.org/viewvc?rev=1419094&view=rev
Log:
Few updates to SearchConditionBuilder to make it simpler to build some comlpex expressions

Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/CompleteCondition.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/Property.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilderTest.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/CompleteCondition.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/CompleteCondition.java?rev=1419094&r1=1419093&r2=1419094&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/CompleteCondition.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/CompleteCondition.java
Sun Dec  9 19:00:50 2012
@@ -34,6 +34,10 @@ public interface CompleteCondition {
     /** shortcut for or().is() */
     Property or(String name);
     
+    /** Wraps the current expression in ()*/
+    CompleteCondition wrap();
+    
+    
     /** Finalize condition construction and build search condition query. */
     String query();
 }

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java?rev=1419094&r1=1419093&r2=1419094&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
Sun Dec  9 19:00:50 2012
@@ -86,6 +86,7 @@ public class FiqlSearchConditionBuilder 
         private Builder parent;
         private DateFormat df;
         private boolean timeZoneSupported;
+        private String currentCompositeOp;
 
         public Builder(Map<String, String> properties) {
             parent = null;
@@ -128,22 +129,31 @@ public class FiqlSearchConditionBuilder 
             return condition(FiqlParser.LT, toString(date));
         }
 
-        public CompleteCondition equalTo(String literalOrPattern) {
-            return condition(FiqlParser.EQ, literalOrPattern);
+        public CompleteCondition equalTo(String value, String...moreValues) {
+            return condition(FiqlParser.EQ, value, (Object[])moreValues);
         }
 
-        public CompleteCondition equalTo(double number) {
-            return condition(FiqlParser.EQ, number);
+        public CompleteCondition equalTo(Double number, Double... moreValues) {
+            return condition(FiqlParser.EQ, number, (Object[])moreValues);
         }
         
-        public CompleteCondition equalTo(long number) {
-            return condition(FiqlParser.EQ, number);
+        public CompleteCondition equalTo(Long number, Long... moreValues) {
+            return condition(FiqlParser.EQ, number, (Object[])moreValues);
+        }
+        
+        public CompleteCondition equalTo(Integer number, Integer... moreValues) {
+            return condition(FiqlParser.EQ, number, (Object[])moreValues);
+        }
+
+        public CompleteCondition equalTo(Date date, Date... moreValues) {
+            return condition(FiqlParser.EQ, date, (Object[])moreValues);
         }
 
-        public CompleteCondition equalTo(Date date) {
-            return condition(FiqlParser.EQ, toString(date));
+        public CompleteCondition equalTo(Duration distanceFromNow, Duration... moreValues)
{
+            return condition(FiqlParser.EQ, distanceFromNow, (Object[])moreValues);
         }
 
+        
         public CompleteCondition greaterOrEqualTo(double number) {
             return condition(FiqlParser.GE, number);
         }
@@ -224,10 +234,6 @@ public class FiqlSearchConditionBuilder 
             return condition(FiqlParser.LT, distanceFromNow);
         }
 
-        public CompleteCondition equalTo(Duration distanceFromNow) {
-            return condition(FiqlParser.EQ, distanceFromNow);
-        }
-
         public CompleteCondition notAfter(Duration distanceFromNow) {
             return condition(FiqlParser.LE, distanceFromNow);
         }
@@ -240,12 +246,29 @@ public class FiqlSearchConditionBuilder 
             return condition(FiqlParser.NEQ, distanceFromNow);
         }
 
-        protected CompleteCondition condition(String operator, Object value) {
-            result += operator + value;
+        protected CompleteCondition condition(String operator, Object value, Object...moreValues)
{
+            String name = result;
+            result += operator + toString(value);
+            if (moreValues != null && moreValues.length > 0) {
+                for (Object next : moreValues) {
+                    result += "," + name + operator + toString(next);
+                }
+                currentCompositeOp = FiqlParser.OR;
+            }
             return this;
         }
         
         public PartialCondition and() {
+            if (currentCompositeOp == FiqlParser.OR 
+                || parent != null && parent.currentCompositeOp == FiqlParser.OR)
{
+                if (parent != null) {
+                    parent.result = "(" + parent.result;
+                    result += ")";
+                } else {
+                    wrap();
+                }
+                currentCompositeOp = FiqlParser.AND;
+            }
             result += FiqlParser.AND;
             return this;
         }
@@ -255,6 +278,16 @@ public class FiqlSearchConditionBuilder 
         }
 
         public PartialCondition or() {
+            if (currentCompositeOp == FiqlParser.AND
+                || parent != null && parent.currentCompositeOp == FiqlParser.AND)
{
+                if (parent != null) {
+                    parent.result = "(" + parent.result;
+                    result += ")";
+                } else {
+                    wrap();
+                }
+                currentCompositeOp = FiqlParser.OR;
+            }
             result += FiqlParser.OR;
             return this;
         }
@@ -263,6 +296,12 @@ public class FiqlSearchConditionBuilder 
             return or().is(name);
         }
 
+        public CompleteCondition wrap() {
+            result = "(" + result + ")";
+            this.currentCompositeOp = null;
+            return this;
+        }
+        
         public CompleteCondition and(CompleteCondition c1, CompleteCondition c2, CompleteCondition...
cn) {
             result += "(" + ((Builder)c1).buildPartial(this) + FiqlParser.AND
                       + ((Builder)c2).buildPartial(this);
@@ -289,14 +328,21 @@ public class FiqlSearchConditionBuilder 
             return b;
         }
 
-        private String toString(Date date) {
-            String s = df.format(date);
-            if (timeZoneSupported) {
-                // zone in XML is "+01:00" in Java is "+0100"; adding semicolon
-                int len = s.length();
-                return s.substring(0, len - 2) + ":" + s.substring(len - 2, len);
+        private String toString(Object value) {
+            if (value == null) {
+                return null;
+            }
+            if (value.getClass() == Date.class) {
+                String s = df.format((Date)value);
+                if (timeZoneSupported) {
+                    // zone in XML is "+01:00" in Java is "+0100"; adding semicolon
+                    int len = s.length();
+                    return s.substring(0, len - 2) + ":" + s.substring(len - 2, len);
+                } else {
+                    return s;
+                }
             } else {
-                return s;
+                return value.toString();
             }
         }
     }

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/Property.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/Property.java?rev=1419094&r1=1419093&r2=1419094&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/Property.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/Property.java
Sun Dec  9 19:00:50 2012
@@ -27,19 +27,22 @@ import javax.xml.datatype.Duration;
  */
 public interface Property {
     /** Is textual property equal to given literal or matching given pattern? */
-    CompleteCondition equalTo(String literalOrPattern);
+    CompleteCondition equalTo(String value, String ...moreValues);
 
     /** Is numeric property equal to given double number? */
-    CompleteCondition equalTo(double number);
+    CompleteCondition equalTo(Double number, Double... moreValues);
 
     /** Is numeric property equal to given long number? */
-    CompleteCondition equalTo(long number);
+    CompleteCondition equalTo(Long number, Long... moreValues);
+    
+    /** Is numeric property equal to given long number? */
+    CompleteCondition equalTo(Integer number, Integer... moreValues);
     
     /** Is date property same as given date? */
-    CompleteCondition equalTo(Date date);
+    CompleteCondition equalTo(Date date, Date... moreValues);
 
     /** Is date property same as date distant from now by given period of time? */
-    CompleteCondition equalTo(Duration distanceFromNow);
+    CompleteCondition equalTo(Duration distanceFromNow, Duration... moreValues);
 
     /** Is textual property different than given literal or not matching given pattern? */
     CompleteCondition notEqualTo(String literalOrPattern);

Modified: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilderTest.java?rev=1419094&r1=1419093&r2=1419094&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilderTest.java
Sun Dec  9 19:00:50 2012
@@ -297,12 +297,46 @@ public class FiqlSearchConditionBuilderT
 
     @Test
     public void testComplex2() {
-        String ret = b.is("foo").equalTo(123.4).or().is("foo").equalTo("null").and().or(
+        String ret = b.is("foo").equalTo(123L).or().is("foo").equalTo("null").and().or(
             b.is("bar").equalTo("asadf*"), 
             b.is("baz").lessThan(20).and().or(
-                b.is("sub1").equalTo(0),
-                b.is("sub2").equalTo(0))).query();
+                b.is("sub1").equalTo(0L),
+                b.is("sub2").equalTo(0L))).query();
         
-        assertEquals("foo==123.4,foo==null;(bar==asadf*,baz=lt=20;(sub1==0,sub2==0))", ret);
+        assertEquals("foo==123,foo==null;(bar==asadf*,baz=lt=20;(sub1==0,sub2==0))", ret);
+    }
+    
+    @Test
+    public void testOrAndImplicitWrap() {
+        String ret = b.is("foo").equalTo(1, 2).and("bar").equalTo("baz").query();
+        
+        assertEquals("(foo==1,foo==2);bar==baz", ret);
+    }
+    
+    @Test
+    public void testMultipleOrShortcut() {
+        // alternative to
+        // b.is("foo").equalTo(123.4).or().is("foo").equalTo("137.8")
+        String ret = b.is("foo").equalTo(123.4, 137.8).query();
+        
+        assertEquals("foo==123.4,foo==137.8", ret);
+    }
+    
+    @Test
+    public void testMultipleOrShortcutWithAnd() {
+        // alternative to
+        // b.is("foo").equalTo(123.4).or().is("foo").equalTo("137.8")
+        String ret = b.is("foo").equalTo(123.4, 137.8).and("bar").equalTo("baz").query();
+        
+        assertEquals("(foo==123.4,foo==137.8);bar==baz", ret);
+    }
+    
+    @Test
+    public void testMultipleOrShortcutWithAnd2() {
+        // alternative to
+        // b.is("foo").equalTo(123.4).or().is("foo").equalTo("137.8")
+        String ret = b.is("foo").equalTo(123.4, 137.8).or("n").equalTo("n1").and("bar").equalTo("baz").query();
+        
+        assertEquals("(foo==123.4,foo==137.8,n==n1);bar==baz", ret);
     }
 }



Mime
View raw message