Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ValueSetRelationalPredicate |
|
| 3.4285714285714284;3.429 | ||||
ValueSetRelationalPredicate$1 |
|
| 3.4285714285714284;3.429 | ||||
ValueSetRelationalPredicate$SetRelation |
|
| 3.4285714285714284;3.429 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.policy.predicate; | |
11 | ||
12 | import java.util.Map; | |
13 | ||
14 | import org.openpermis.policy.Predicate; | |
15 | import org.openpermis.policy.TimeStamp; | |
16 | ||
17 | /** | |
18 | * A relational predicate. | |
19 | * @since 0.3.0 | |
20 | */ | |
21 | public class ValueSetRelationalPredicate extends AbstractPredicate<ValueSet> { | |
22 | ||
23 | //---- Static | |
24 | ||
25 | /** | |
26 | * @since 0.3.0 | |
27 | */ | |
28 | 6 | public static enum SetRelation { |
29 | /** | |
30 | * @since 0.3.0 | |
31 | */ | |
32 | 1 | Subset, |
33 | ||
34 | /** | |
35 | * @since 0.3.0 | |
36 | */ | |
37 | 1 | Superset, |
38 | ||
39 | /** | |
40 | * @since 0.3.0 | |
41 | */ | |
42 | 1 | NonNullIntersection |
43 | } | |
44 | ||
45 | /** | |
46 | * @since 0.3.0 | |
47 | */ | |
48 | private static final long serialVersionUID = 5176218191733835322L; | |
49 | ||
50 | //---- State | |
51 | ||
52 | /** | |
53 | * @since 0.3.0 | |
54 | */ | |
55 | private SetRelation relation; | |
56 | ||
57 | //---- Constructors | |
58 | ||
59 | /** | |
60 | * Creates a relational predicate. | |
61 | * @param relation a {@link SetRelation}. | |
62 | * @param first the first comparable {@link Value}. | |
63 | * @param second the second comparable {@link Value}. | |
64 | * @since 0.3.0 | |
65 | */ | |
66 | public ValueSetRelationalPredicate (SetRelation relation, ValueSet first, ValueSet second) { | |
67 | 44 | super(first, second); |
68 | 44 | this.relation = relation; |
69 | 44 | } |
70 | ||
71 | //---- Methods | |
72 | ||
73 | /** | |
74 | * Returns the relation of this predicate. | |
75 | * @return the relation of this predicate. | |
76 | * @since 0.3.0 | |
77 | */ | |
78 | public SetRelation getRelation () { | |
79 | 40 | return this.relation; |
80 | } | |
81 | ||
82 | //---- AbstractPredicate | |
83 | ||
84 | /** | |
85 | * @since 0.3.0 | |
86 | */ | |
87 | public boolean isValid () { | |
88 | 53 | return true; |
89 | } | |
90 | ||
91 | /** | |
92 | * @since 0.3.0 | |
93 | */ | |
94 | public boolean matches (TimeStamp timeStamp, Map<String, ?> arguments) { | |
95 | 13 | if (!isValid()) { |
96 | 0 | throw new IllegalStateException("Illegal predicate."); |
97 | } | |
98 | 13 | ValueSet set1 = getOperand(0); |
99 | 13 | ValueSet set2 = getOperand(1); |
100 | 13 | switch (getRelation()) { |
101 | case Subset: | |
102 | 7 | return set2.containsAllElementOfSet(set1, timeStamp, arguments); |
103 | case Superset: | |
104 | 3 | return set1.containsAllElementOfSet(set2, timeStamp, arguments); |
105 | case NonNullIntersection: | |
106 | 3 | return set1.containsAnyElementOfSet(set2, timeStamp, arguments); |
107 | default: | |
108 | 0 | throw new IllegalStateException("Should never happen."); |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * @since 0.3.0 | |
114 | */ | |
115 | public boolean isMatchable (Map<String, Class<?>> arguments) { | |
116 | 4 | for (Value<?> value : getOperand(0).getValues()) { |
117 | 8 | if (!value.isMatchable(arguments)) { |
118 | 2 | return false; |
119 | } | |
120 | } | |
121 | 2 | for (Value<?> value : getOperand(1).getValues()) { |
122 | 4 | if (!value.isMatchable(arguments)) { |
123 | 1 | return false; |
124 | } | |
125 | } | |
126 | 1 | return true; |
127 | } | |
128 | ||
129 | ||
130 | /** | |
131 | * @since 0.3.0 | |
132 | */ | |
133 | public boolean comparablePredicate (Predicate predicate) { | |
134 | 12 | if (!(predicate instanceof ValueSetRelationalPredicate)) { |
135 | 0 | return false; |
136 | } | |
137 | 12 | final ValueSetRelationalPredicate other = (ValueSetRelationalPredicate) predicate; |
138 | 12 | return getRelation().equals(other.getRelation()); |
139 | } | |
140 | ||
141 | /** | |
142 | * @since 0.3.0 | |
143 | */ | |
144 | protected int partHashCode () { | |
145 | 0 | return ValueSetRelationalPredicate.class.hashCode(); |
146 | } | |
147 | ||
148 | } |