1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.fosstrak.llrp.commander.util;
23
24
25
26
27
28
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
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 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
82
83
84
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
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
142
143 public int getDefaultValue() {
144 return ranges[0].getLowerBound();
145 }
146 }