1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.policy.predicate; |
11 | |
|
12 | |
import static org.openpermis.policy.bean.basic.BasicUtilities.equalLists; |
13 | |
|
14 | |
import java.util.Arrays; |
15 | |
import java.util.List; |
16 | |
import java.util.Map; |
17 | |
|
18 | |
import org.openpermis.policy.TimeStamp; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class ValueSet { |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
private static final long serialVersionUID = -120774600735285360L; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private final List<Value<?>> values; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 2 | public ValueSet (Value<?>... values) { |
50 | 2 | if (values.length < 1) { |
51 | 0 | throw new IllegalArgumentException("Set operator needs at least one argument."); |
52 | |
} |
53 | 2 | this.values = Arrays.asList(values); |
54 | 2 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 80 | public ValueSet (List<Value<?>> values) { |
62 | 80 | if (values.size() < 1) { |
63 | 0 | throw new IllegalArgumentException("Set operator needs at least one argument."); |
64 | |
} |
65 | 80 | this.values = values; |
66 | 80 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public List<Value<?>> getValues () { |
76 | 78 | return this.values; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@SuppressWarnings("unchecked") |
83 | |
private boolean containsElement ( |
84 | |
Value<?> value, TimeStamp timeStamp, Map<String, ?> arguments |
85 | |
) { |
86 | |
Object op1; |
87 | |
Object op2; |
88 | |
Comparable op1Value; |
89 | |
Comparable op2Value; |
90 | |
|
91 | 18 | op1 = value.valueOf(timeStamp, arguments); |
92 | 18 | op1Value = (Comparable) op1; |
93 | 41 | for (int i = 0; i < this.values.size(); i++) { |
94 | 33 | op2 = this.values.get(i).valueOf(timeStamp, arguments); |
95 | |
|
96 | 33 | if (op1.getClass() == op2.getClass()) { |
97 | 28 | op2Value = (Comparable) op2; |
98 | 28 | if (op1Value.compareTo(op2Value) == 0) { |
99 | 10 | return true; |
100 | |
} |
101 | |
} |
102 | |
} |
103 | 8 | return false; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public boolean containsAnyElementOfSet ( |
111 | |
ValueSet otherSet, TimeStamp timeStamp, Map<String, ?> arguments) { |
112 | 6 | for (int i = 0; i < otherSet.getValues().size(); i++) { |
113 | 5 | if (this.containsElement(otherSet.getValues().get(i), timeStamp, arguments)) { |
114 | 2 | return true; |
115 | |
} |
116 | |
} |
117 | 1 | return false; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public boolean containsAllElementOfSet ( |
125 | |
ValueSet otherSet, TimeStamp timeStamp, Map<String, ?> arguments) { |
126 | 18 | for (int i = 0; i < otherSet.getValues().size(); i++) { |
127 | 13 | if (!this.containsElement(otherSet.getValues().get(i), timeStamp, arguments)) { |
128 | 5 | return false; |
129 | |
} |
130 | |
} |
131 | 5 | return true; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public final boolean equals (Object obj) { |
140 | 12 | if (obj == null) { |
141 | 0 | return false; |
142 | |
} |
143 | 12 | if (obj == this) { |
144 | 0 | return true; |
145 | |
} |
146 | 12 | if (obj instanceof ValueSet) { |
147 | 12 | final ValueSet other = (ValueSet) obj; |
148 | 12 | if (!equalLists(getValues(), other.getValues())) { |
149 | 0 | return false; |
150 | |
} |
151 | 12 | return true; |
152 | |
} |
153 | 0 | return false; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public final int hashCode () { |
160 | 0 | int hashCode = ValueSet.class.hashCode(); |
161 | 0 | for (Value<?> value : getValues()) { |
162 | 0 | hashCode *= value.hashCode(); |
163 | |
} |
164 | 0 | return hashCode; |
165 | |
} |
166 | |
|
167 | |
} |