1 /*
2 *
3 * Fosstrak LLRP Commander (www.fosstrak.org)
4 *
5 * Copyright (C) 2008 ETH Zurich
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>
19 *
20 */
21
22 package org.fosstrak.llrp.commander.util;
23
24 /**
25 * This class represents an LLRP range constraint. It specifies that a certain numeric field value
26 * must lie within some ranges.
27 *
28 * @author Ulrich Etter, ETHZ
29 *
30 */
31 public class LLRPRangeConstraint {
32
33 private String messageOrParameterName;
34 private String fieldName;
35 private Range[] ranges;
36
37 private String preconditionedEnumerationName;
38 private String preconditionedEnumerationValue;
39
40 /**
41 * Creates a new range constraint.
42 * <br/><br/>
43 * <b>Example:</b> <br/>
44 * To create a constraint that specifies that the field <code>DurationPeriod</code> of the
45 * parameter <code>RFSurveySpecStopTrigger</code> must be greater than 0 if the enumeration
46 * <code>StopTriggerType</code> has value <code>Duration</code>, use the following code: <br/>
47 * <code>
48 * new LLRPRangeConstraint(
49 * "RFSurveySpecStopTrigger",
50 * "DurationPeriod",
51 * new Range[] {
52 * new Range(1, Integer.MAX_VALUE)
53 * },
54 * "StopTriggerType",
55 * "Duration");
56 * </code>
57 *
58 * @param messageOrParameterName the name of the message/parameter this constraint is defined for
59 * @param fieldName the name of the field this constraint is defined for
60 * @param ranges an array of ranges; for a value to satisfy a constraint it must lie in
61 * one of this ranges
62 * @param preconditionedEnumerationName the name of the enumeration on which this constraint
63 * is dependent; use <code>null</code> if this constraint does not depend on any enumeration
64 * @param preconditionedEnumerationValue the value of the enumeration on which this constraint
65 * is dependent; use <code>null</code> if this constraint does not depend on any enumeration
66 */
67 public LLRPRangeConstraint(
68 String messageOrParameterName,
69 String fieldName,
70 Range[] ranges,
71 String preconditionedEnumerationName,
72 String preconditionedEnumerationValue){
73 this.messageOrParameterName = messageOrParameterName;
74 this.fieldName = fieldName;
75 this.ranges = ranges;
76 this.preconditionedEnumerationName = preconditionedEnumerationName;
77 this.preconditionedEnumerationValue = preconditionedEnumerationValue;
78 }
79
80 /**
81 * Checks whether this constraint is satisfied by the given field value.
82 *
83 * @param fieldValue the field value to validate
84 * @return <code>true</code> if the constraint is satisfied, and <code>false</code> otherwise
85 */
86 public boolean isSatisfied(int fieldValue){
87 boolean result = false;
88 for (int i = 0; i < ranges.length; i++){
89 result = result || (ranges[i].getLowerBound() <= fieldValue && fieldValue <= ranges[i].getUpperBound());
90 }
91 return result;
92 }
93
94 /**
95 * @return the error message associated with this constraint
96 */
97 public String getErrorMessage(){
98 String result = fieldName + " must be ";
99 for (int i = 0; i < ranges.length; i++){
100 if (ranges[i].getLowerBound() == Integer.MIN_VALUE){
101 result = result + "smaller than " + (ranges[i].getUpperBound() + 1);
102 }
103 else if (ranges[i].getUpperBound() == Integer.MAX_VALUE){
104 result = result + "greater than " + (ranges[i].getLowerBound() - 1);
105 }
106 else if (ranges[i].getLowerBound() == ranges[i].getUpperBound()){
107 result = result + ranges[i].getLowerBound();
108 }
109 else {
110 result = result + "between " + ranges[i].getLowerBound() + " and " + ranges[i].getUpperBound();
111 }
112
113 if (i < (ranges.length - 1)){
114 result = result + " or ";
115 }
116 }
117 if (preconditionedEnumerationName != null){
118 result = result + " when " + preconditionedEnumerationName + " = " + preconditionedEnumerationValue;
119 }
120 result = result + ".";
121 return result;
122 }
123
124 public String getMessageOrParameterName() {
125 return messageOrParameterName;
126 }
127
128 public String getFieldName() {
129 return fieldName;
130 }
131
132 public String getPreconditionedEnumerationName() {
133 return preconditionedEnumerationName;
134 }
135
136 public String getPreconditionedEnumerationValue() {
137 return preconditionedEnumerationValue;
138 }
139
140 /**
141 * @return a default value for the field this constraint is specified for
142 */
143 public int getDefaultValue() {
144 return ranges[0].getLowerBound();
145 }
146 }