001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.search.query;
017
018/**
019 * Represents a {@link Query} testing a long range.
020 */
021public class LongRangeQuery extends AbstractFieldQuery
022{
023    
024    /** The lower end of the long range. */
025    protected long _lower;
026    /** The upper end of the long range. */
027    protected long _upper;
028    /** True to include the lower end, false to exclude it. */
029    protected boolean _includeLower;
030    /** True to include the upper end, false to exclude it. */
031    protected boolean _includeUpper;
032    
033    /**
034     * Build a long range query.
035     * @param fieldPath The field path.
036     * @param lower The lower end of the range.
037     * @param upper The upper end of the range.
038     */
039    public LongRangeQuery(String fieldPath, long lower, long upper)
040    {
041        this(fieldPath, lower, upper, true, true);
042    }
043    
044    /**
045     * Build a long range query.
046     * @param fieldPath The field path.
047     * @param lower The lower end of the range.
048     * @param upper The upper end of the range.
049     * @param includelower Whether to include the lower end or not.
050     * @param includeTo Whether to include the upper end or not.
051     */
052    public LongRangeQuery(String fieldPath, long lower, long upper, boolean includelower, boolean includeTo)
053    {
054        super(fieldPath);
055        _lower = lower;
056        _upper = upper;
057        _includeLower = includelower;
058        _includeUpper = includeTo;
059    }
060    
061    @Override
062    public String build() throws QuerySyntaxException
063    {
064        StringBuilder query = new StringBuilder();
065        
066        query.append(_fieldPath).append("_l:")
067             .append(_includeLower ? '[' : '{')
068             .append(_lower).append(" TO ").append(_upper)
069             .append(_includeUpper ? ']' : '}');
070        
071        return query.toString();
072    }
073
074    @Override
075    public int hashCode()
076    {
077        final int prime = 31;
078        int result = super.hashCode();
079        result = prime * result + (_includeLower ? 1231 : 1237);
080        result = prime * result + (_includeUpper ? 1231 : 1237);
081        result = prime * result + (int) (_lower ^ (_lower >>> 32));
082        result = prime * result + (int) (_upper ^ (_upper >>> 32));
083        return result;
084    }
085
086    @Override
087    public boolean equals(Object obj)
088    {
089        if (this == obj)
090        {
091            return true;
092        }
093        if (!super.equals(obj))
094        {
095            return false;
096        }
097        if (getClass() != obj.getClass())
098        {
099            return false;
100        }
101        LongRangeQuery other = (LongRangeQuery) obj;
102        if (_includeLower != other._includeLower)
103        {
104            return false;
105        }
106        if (_includeUpper != other._includeUpper)
107        {
108            return false;
109        }
110        if (_lower != other._lower)
111        {
112            return false;
113        }
114        if (_upper != other._upper)
115        {
116            return false;
117        }
118        return true;
119    }
120}