001/*
002 *  Copyright 2014 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 double field.
020 */
021public class DoubleQuery extends AbstractFieldQuery
022{
023    /** The operator. */
024    protected Operator _operator;
025    /** The boolean value to test. */
026    protected Double _value;
027    
028    /**
029     * Build a DoubleQuery testing the existence of the field.
030     * @param fieldPath the field path
031     */
032    public DoubleQuery(String fieldPath)
033    {
034        this(fieldPath, Operator.EXISTS, null);
035    }
036    
037    /**
038     * Build a DoubleQuery.
039     * @param fieldPath the field's path
040     * @param value the value.
041     */
042    public DoubleQuery(String fieldPath, Double value)
043    {
044        this(fieldPath, Operator.EQ, value);
045    }
046    
047    /**
048     * Build a DoubleQuery.
049     * @param fieldPath the field's path
050     * @param op the operator.
051     * @param value the value.
052     */
053    public DoubleQuery(String fieldPath, Operator op, Double value)
054    {
055        super(fieldPath);
056        _operator = op;
057        _value = value;
058    }
059    
060    /**
061     * Get the operator.
062     * @return the operator.
063     */
064    public Operator getOperator()
065    {
066        return _operator;
067    }
068    
069    /**
070     * Get the value.
071     * @return the value.
072     */
073    public double getValue()
074    {
075        return _value;
076    }
077    
078    @Override
079    public String build() throws QuerySyntaxException
080    {
081        return QueryHelper.getStandardQuery(_fieldPath + "_d", _operator, _value);
082    }
083
084    @Override
085    public int hashCode()
086    {
087        final int prime = 31;
088        int result = super.hashCode();
089        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
090        result = prime * result + ((_value == null) ? 0 : _value.hashCode());
091        return result;
092    }
093
094    @Override
095    public boolean equals(Object obj)
096    {
097        if (this == obj)
098        {
099            return true;
100        }
101        if (!super.equals(obj))
102        {
103            return false;
104        }
105        if (getClass() != obj.getClass())
106        {
107            return false;
108        }
109        DoubleQuery other = (DoubleQuery) obj;
110        if (_operator != other._operator)
111        {
112            return false;
113        }
114        if (_value == null)
115        {
116            if (other._value != null)
117            {
118                return false;
119            }
120        }
121        else if (!_value.equals(other._value))
122        {
123            return false;
124        }
125        return true;
126    }
127}