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