Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
And |
|
| 3.3333333333333335;3.333 |
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 | * And operation on predicates. | |
19 | * @since 0.1.0 | |
20 | */ | |
21 | public class And extends AbstractPredicate<Predicate> { | |
22 | ||
23 | //---- State | |
24 | ||
25 | /** | |
26 | * @since 0.1.0 | |
27 | */ | |
28 | private static final long serialVersionUID = 2594689375624382737L; | |
29 | ||
30 | //---- Constructors | |
31 | ||
32 | /** | |
33 | * Creates an and node, at least two arguments are needed. | |
34 | * @param predicates {@link Predicate}s. | |
35 | * @since 0.1.0 | |
36 | */ | |
37 | public And (Predicate... predicates) { | |
38 | 57 | super(predicates); |
39 | 57 | if (predicates.length < 2) { |
40 | 2 | throw new IllegalArgumentException("And operator needs at least two arguments."); |
41 | } | |
42 | 55 | } |
43 | ||
44 | //---- AbstractPredicate | |
45 | ||
46 | /** | |
47 | * @since 0.1.0 | |
48 | */ | |
49 | public boolean isValid () { | |
50 | 27 | for (Predicate predicate : getOperands()) { |
51 | 138 | if (!predicate.isValid()) { |
52 | 0 | return false; |
53 | } | |
54 | } | |
55 | 27 | return true; |
56 | } | |
57 | ||
58 | /** | |
59 | * @since 0.1.0 | |
60 | */ | |
61 | public boolean matches (TimeStamp timeStamp, Map<String, ?> arguments) { | |
62 | 17 | for (int i = 0; i < getOperandCount(); i++) { |
63 | 13 | if (!getOperand(i).matches(timeStamp, arguments)) { |
64 | 4 | return false; |
65 | } | |
66 | } | |
67 | 4 | return true; |
68 | } | |
69 | ||
70 | /** | |
71 | * @since 0.3.0 | |
72 | */ | |
73 | public boolean isMatchable (Map<String, Class<?>> arguments) { | |
74 | 30 | for (int i = 0; i < getOperandCount(); i++) { |
75 | 25 | if (getOperand(i) instanceof Present && |
76 | !((Present) getOperand(i)).isMatchable(arguments) | |
77 | ) { | |
78 | // Cancel. | |
79 | 4 | return true; |
80 | } | |
81 | 21 | if (!getOperand(i).isMatchable(arguments)) { |
82 | 7 | return false; |
83 | } | |
84 | } | |
85 | 5 | return true; |
86 | } | |
87 | ||
88 | /** | |
89 | * @since 0.1.0 | |
90 | */ | |
91 | public boolean comparablePredicate (Predicate predicate) { | |
92 | 12 | return predicate instanceof And; |
93 | } | |
94 | ||
95 | /** | |
96 | * @since 0.1.0 | |
97 | */ | |
98 | protected int partHashCode () { | |
99 | 2 | return And.class.hashCode(); |
100 | } | |
101 | ||
102 | } |