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 double range. 020 */ 021public class DoubleRangeQuery extends AbstractFieldQuery 022{ 023 024 /** The lower end of the double range. */ 025 protected double _lower; 026 /** The upper end of the double range. */ 027 protected double _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 double 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 DoubleRangeQuery(String fieldPath, double lower, double upper) 040 { 041 this(fieldPath, lower, upper, true, true); 042 } 043 044 /** 045 * Build a double 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 DoubleRangeQuery(String fieldPath, double lower, double 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("_d:") 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 long temp; 082 temp = Double.doubleToLongBits(_lower); 083 result = prime * result + (int) (temp ^ (temp >>> 32)); 084 temp = Double.doubleToLongBits(_upper); 085 result = prime * result + (int) (temp ^ (temp >>> 32)); 086 return result; 087 } 088 089 @Override 090 public boolean equals(Object obj) 091 { 092 if (this == obj) 093 { 094 return true; 095 } 096 if (!super.equals(obj)) 097 { 098 return false; 099 } 100 if (getClass() != obj.getClass()) 101 { 102 return false; 103 } 104 DoubleRangeQuery other = (DoubleRangeQuery) obj; 105 if (_includeLower != other._includeLower) 106 { 107 return false; 108 } 109 if (_includeUpper != other._includeUpper) 110 { 111 return false; 112 } 113 if (Double.doubleToLongBits(_lower) != Double.doubleToLongBits(other._lower)) 114 { 115 return false; 116 } 117 if (Double.doubleToLongBits(_upper) != Double.doubleToLongBits(other._upper)) 118 { 119 return false; 120 } 121 return true; 122 } 123}