/
RangePredicateImpl.java
167 lines (147 loc) · 4.59 KB
/
RangePredicateImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
* Copyright (c) 2016, 2020 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.datastore.internal.model.query;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.datastore.client.DatamodelMappingException;
import org.eclipse.kapua.service.datastore.internal.schema.SchemaUtil;
import org.eclipse.kapua.service.datastore.model.query.RangePredicate;
import org.eclipse.kapua.service.datastore.model.query.StorableField;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Implementation of query predicate for matching range values
*
* @since 1.0.0
*/
public class RangePredicateImpl implements RangePredicate {
protected String field;
protected Object minValue;
protected Object maxValue;
private <V extends Comparable<V>> void checkRange(Class<V> clazz) throws KapuaException {
if (minValue == null || maxValue == null) {
return;
}
V min = clazz.cast(minValue);
V max = clazz.cast(maxValue);
if (min.compareTo(max) > 0) {
throw KapuaException.internalError("Min value must not be graeter than max value");
}
}
/**
* Default constructor
*/
public RangePredicateImpl() {
}
public <V extends Comparable<V>> RangePredicateImpl(StorableField field, V minValue, V maxValue) {
this(field.field(), minValue, maxValue);
}
/**
* Construct a range predicate given the field and the values
*
* @param field
* @param minValue
* @param maxValue
*/
public <V extends Comparable<V>> RangePredicateImpl(String field, V minValue, V maxValue) {
this.field = field;
this.minValue = minValue;
this.maxValue = maxValue;
}
@Override
public String getField() {
return field;
}
public RangePredicate setField(StorableField field) {
return setField(field.field());
}
/**
* Get the field
*
* @param field
* @return
*/
public RangePredicate setField(String field) {
this.field = field;
return this;
}
@Override
public Object getMinValue() {
return minValue;
}
@Override
public <V extends Comparable<V>> V getMinValue(Class<V> clazz) {
return clazz.cast(minValue);
}
/**
* Set the minimum value (typed)
*
* @param clazz
* @param minValue
* @return
* @throws KapuaException
*/
public <V extends Comparable<V>> RangePredicate setMinValue(Class<V> clazz, V minValue) throws KapuaException {
this.minValue = minValue;
checkRange(clazz);
return this;
}
@Override
public Object getMaxValue() {
return maxValue;
}
@Override
public <V extends Comparable<V>> V getMaxValue(Class<V> clazz) {
return clazz.cast(maxValue);
}
/**
* Set the maximum value (typed)
*
* @param clazz
* @param maxValue
* @return
* @throws KapuaException
*/
public <V extends Comparable<V>> RangePredicate setMaxValue(Class<V> clazz, V maxValue) throws KapuaException {
this.maxValue = maxValue;
checkRange(clazz);
return this;
}
/**
* <pre>
* {
* "query": {
* "range" : {
* "temperature" : {
* "gte" : 10,
* "lte" : 20,
* }
* }
* }
* }
* </pre>
*
* @throws DatamodelMappingException
*/
public ObjectNode toSerializedMap() throws DatamodelMappingException {
ObjectNode rootNode = SchemaUtil.getObjectNode();
ObjectNode valuesNode = SchemaUtil.getObjectNode();
if (maxValue != null) {
SchemaUtil.appendField(valuesNode, PredicateConstants.LTE_KEY, maxValue);
}
if (minValue != null) {
SchemaUtil.appendField(valuesNode, PredicateConstants.GTE_KEY, minValue);
}
ObjectNode termNode = SchemaUtil.getObjectNode();
termNode.set(field, valuesNode);
rootNode.set(PredicateConstants.RANGE_KEY, termNode);
return rootNode;
}
}