Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.cxf.jaxrs.ext.search.client;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 
Builds a FIQL search condition.

Examples:

 SearchConditionBuilder b = SearchConditionBuilder.instance("fiql");
 b.is("price").equalTo(123.5).query();
 // gives "price==123.5"
 b.is("price").greaterThan(30).and().is("price").lessThan(50).query();
 // gives "price=gt=30.0;price=lt=50.0"
 
For very complex junctions nested "and"/"or" are allowed (breaking a bit fluency of interface) and looks like the following example:
 SearchConditionBuilder b = SearchConditionBuilder.instance("fiql");
 b.is("price").lessThan(100).and().or(
     b.is("title").equalTo("The lord*"),
     b.is("author").equalTo("R.R.Tolkien")).query();
 // gives "price=lt=100.0;(title==The lord*,author==R.R.Tolkien)"
 
 
 
     protected Map<StringStringproperties;
 
     public FiqlSearchConditionBuilder() {
         this(Collections.<StringStringemptyMap());
     }
 
     public FiqlSearchConditionBuilder(Map<StringStringproperties) {
         this. = properties;
     }
 
     protected Builder newBuilderInstance() {
         return new Builder();
     }
     
     public String query() {
         return "";
     }
 
     public Property is(String property) {
         return newBuilderInstance().is(property);
     }
 
                                  CompleteCondition... cn) {
         return newBuilderInstance().and(c1c2cn);
     }
     
     public CompleteCondition and(List<CompleteConditionconditions) {
         return newBuilderInstance().and(conditions);
     }
     
     public CompleteCondition or(List<CompleteConditionconditions) {
         return newBuilderInstance().or(conditions);
     }
 
         return newBuilderInstance().or(c1c2cn);
     }
 
     protected static class Builder implements PropertyCompleteConditionPartialCondition {
        protected String result = "";
        protected Builder parent;
        protected DateFormat df;
        protected boolean timeZoneSupported;
        protected String currentCompositeOp;
        public Builder(Map<StringStringproperties) {
             = null;
             = SearchUtils.getDateFormat(properties);
             = SearchUtils.isTimeZoneSupported(properties.);
        }
        public Builder(Builder parent) {
            this. = parent;
             = parent.getDateFormat();
             = parent.isTimeZoneSupported();
        }
        public String query() {
            return buildPartial(null);
        }
        protected DateFormat getDateFormat() {
            return ;
        }
        protected boolean isTimeZoneSupported() {
            return ;
        }
        // builds from parent but not further then exclude builder
        protected String buildPartial(Builder exclude) {
            if ( != null && !.equals(exclude)) {
                return .buildPartial(exclude) + ;
            } else {
                return ;
            }
        }
        public CompleteCondition after(Date date) {
            return condition(.toString(date));
        }
        public CompleteCondition before(Date date) {
            return condition(.toString(date));
        }
        public CompleteCondition comparesTo(ConditionType typeString value) {
            
            return condition(toFiqlPrimitiveCondition(type), value);
        }
        
        public CompleteCondition comparesTo(ConditionType typeDouble value) {
            
            return condition(toFiqlPrimitiveCondition(type), value);
        }
        
        public CompleteCondition comparesTo(ConditionType typeInteger value) {
            
            return condition(toFiqlPrimitiveCondition(type), value);
        }
        
        public CompleteCondition comparesTo(ConditionType typeLong value) {
            
            return condition(toFiqlPrimitiveCondition(type), value);
        }
        
        public CompleteCondition comparesTo(ConditionType typeDate value) {
            
            return condition(toFiqlPrimitiveCondition(type), value);
        }
        
        public CompleteCondition comparesTo(ConditionType typeDuration value) {
            
            return condition(toFiqlPrimitiveCondition(type), value);
        }
        
        public CompleteCondition equalTo(String valueString...moreValues) {
            return condition(.value, (Object[])moreValues);
        }
        public CompleteCondition equalTo(Double numberDouble... moreValues) {
            return condition(.number, (Object[])moreValues);
        }
        
        public CompleteCondition equalTo(Long numberLong... moreValues) {
            return condition(.number, (Object[])moreValues);
        }
        
        public CompleteCondition equalTo(Integer numberInteger... moreValues) {
            return condition(.number, (Object[])moreValues);
        }
        public CompleteCondition equalTo(Date dateDate... moreValues) {
            return condition(.date, (Object[])moreValues);
        }
        public CompleteCondition equalTo(Duration distanceFromNowDuration... moreValues) {
            return condition(.distanceFromNow, (Object[])moreValues);
        }
        
        public CompleteCondition greaterOrEqualTo(Double number) {
            return condition(.number);
        }
        
        public CompleteCondition greaterOrEqualTo(Long number) {
            return condition(.number);
        }
        
        public CompleteCondition greaterOrEqualTo(Integer number) {
            return condition(.number);
        }
        public CompleteCondition greaterThan(Double number) {
            return condition(.number);
        }
        
        public CompleteCondition greaterThan(Long number) {
            return condition(.number);
        }
        
        public CompleteCondition greaterThan(Integer number) {
            return condition(.number);
        }
        public CompleteCondition lessOrEqualTo(Double number) {
            return condition(.number);
        }
        
        public CompleteCondition lessOrEqualTo(Long number) {
            return condition(.number);
        }
        
        public CompleteCondition lessOrEqualTo(Integer number) {
            return condition(.number);
        }
        public CompleteCondition lessThan(Double number) {
            return condition(.number);
        }
        
        public CompleteCondition lessThan(Long number) {
            return condition(.number);
        }
        
        public CompleteCondition lessThan(Integer number) {
            return condition(.number);
        }
        public CompleteCondition lexicalAfter(String literal) {
            return condition(.literal);
        }
        public CompleteCondition lexicalBefore(String literal) {
            return condition(.literal);
        }
        public CompleteCondition lexicalNotAfter(String literal) {
            return condition(.literal);
        }
        public CompleteCondition lexicalNotBefore(String literal) {
            return condition(.literal);
        }
        public CompleteCondition notAfter(Date date) {
            return condition(.toString(date));
        }
        public CompleteCondition notBefore(Date date) {
            return condition(.toString(date));
        }
        public CompleteCondition notEqualTo(String literalOrPattern) {
            return condition(.literalOrPattern);
        }
        public CompleteCondition notEqualTo(Double number) {
            return condition(.number);
        }
        
        public CompleteCondition notEqualTo(Long number) {
            return condition(.number);
        }
        
        public CompleteCondition notEqualTo(Integer number) {
            return condition(.number);
        }
        public CompleteCondition notEqualTo(Date date) {
            return condition(.toString(date));
        }
        public CompleteCondition after(Duration distanceFromNow) {
            return condition(.distanceFromNow);
        }
        public CompleteCondition before(Duration distanceFromNow) {
            return condition(.distanceFromNow);
        }
        public CompleteCondition notAfter(Duration distanceFromNow) {
            return condition(.distanceFromNow);
        }
        public CompleteCondition notBefore(Duration distanceFromNow) {
            return condition(.distanceFromNow);
        }
        public CompleteCondition notEqualTo(Duration distanceFromNow) {
            return condition(.distanceFromNow);
        }
        protected CompleteCondition condition(String operatorObject valueObject...moreValues) {
            String name = ;
             += operator + toString(value);
            if (moreValues != null && moreValues.length > 0) {
                for (Object next : moreValues) {
                     += "," + name + operator + toString(next);
                }
                 = .;
            }
            return this;
        }
        
        public PartialCondition and() {
            if ( == . 
                ||  != null && . == .) {
                if ( != null) {
                    . = "(" + .;
                     += ")";
                } else {
                    wrap();
                }
                 = .;
            }
             += .;
            return this;
        }
        
        public Property and(String name) {
            return and().is(name);
        }
        public PartialCondition or() {
            if ( == .
                ||  != null && . == .) {
                if ( != null) {
                    . = "(" + .;
                     += ")";
                } else {
                    wrap();
                }
                 = .;
            }
             += .;
            return this;
        }
        
        public Property or(String name) {
            return or().is(name);
        }
        public CompleteCondition wrap() {
             = "(" +  + ")";
            this. = null;
            return this;
        }
        
        public CompleteCondition and(CompleteCondition c1CompleteCondition c2CompleteCondition... cn) {
             += "(" + ((Builder)c1).buildPartial(this) + .
                      + ((Builder)c2).buildPartial(this);
            for (CompleteCondition c : cn) {
                 += . + ((Builder)c).buildPartial(this);
            }
             += ")";
            return this;
        }
        public CompleteCondition or(CompleteCondition c1CompleteCondition c2CompleteCondition... cn) {
             += "(" + ((Builder)c1).buildPartial(this) + .
                      + ((Builder)c2).buildPartial(this);
            for (CompleteCondition c : cn) {
                 += . + ((Builder)c).buildPartial(this);
            }
             += ")";
            return this;
        }
        
        public CompleteCondition and(List<CompleteConditionconditions) {
            return conditionsList(.conditions);
        }
        
        public CompleteCondition or(List<CompleteConditionconditions) {
            return conditionsList(.conditions);
        }
        
        protected CompleteCondition conditionsList(String opList<CompleteConditionconditions) {
            if (conditions.size() == 1) {
                 += ((Builder)conditions.get(0)).buildPartial(this);
            } else {
                 += "(";
                
                for (Iterator<CompleteConditionit = conditions.iterator(); it.hasNext();) {
                     += ((Builder)it.next()).buildPartial(this);
                    if (it.hasNext()) {
                         += op;
                    }
                }
                 += ")";
            }
            
            return this;
        }
        
        public Property is(String property) {
            Builder b = new Builder(this);
            b.result = property;
            return b;
        }
        protected String toString(Object value) {
            if (value == null) {
                return null;
            }
            if (value.getClass() == Date.class) {
                String s = .format((Date)value);
                if () {
                    // 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 value.toString();
            }
        }
        
        protected String toFiqlPrimitiveCondition(ConditionType type) {
            String fiqlType = ..get(type);
            if (fiqlType == null) {
                throw new IllegalArgumentException("Only primitive condition types are supported");
            }
            return fiqlType;
        }
    }
New to GrepCode? Check out our FAQ X