Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Constant |
|
| 2.2857142857142856;2.286 |
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.TimeStamp; | |
15 | ||
16 | ||
17 | /** | |
18 | * A constant value. | |
19 | * @param <T> constant value type. | |
20 | * @since 0.1.0 | |
21 | */ | |
22 | public class Constant<T> implements Value<T> { | |
23 | ||
24 | //---- State | |
25 | ||
26 | /** | |
27 | * @since 0.1.0 | |
28 | */ | |
29 | private T value; | |
30 | ||
31 | //---- Constructors | |
32 | ||
33 | /** | |
34 | * Creates a constant. | |
35 | */ | |
36 | 192 | public Constant (T value) { |
37 | 192 | if (value == null) { |
38 | 1 | throw new IllegalArgumentException("Value is null"); |
39 | } | |
40 | 191 | this.value = value; |
41 | 191 | } |
42 | ||
43 | //---- Methods | |
44 | ||
45 | /** | |
46 | * @since 0.1.0 | |
47 | */ | |
48 | public Class<?> getType () { | |
49 | 345 | return getValue().getClass(); |
50 | } | |
51 | ||
52 | /** | |
53 | * Returns the value. | |
54 | * @return the value. | |
55 | * @since 0.1.0 | |
56 | */ | |
57 | public T getValue () { | |
58 | 474 | return this.value; |
59 | } | |
60 | ||
61 | //---- Value | |
62 | ||
63 | /** | |
64 | * @since 0.1.0 | |
65 | */ | |
66 | public T valueOf (TimeStamp timeStamp, Map<String, ?> arguments) { | |
67 | 62 | return getValue(); |
68 | } | |
69 | ||
70 | /** | |
71 | * @since 0.3.0 | |
72 | */ | |
73 | public boolean isMatchable (Map<String, Class<?>> arguments) { | |
74 | 7 | return true; |
75 | } | |
76 | ||
77 | //---- Object | |
78 | ||
79 | /** | |
80 | * @since 0.1.0 | |
81 | */ | |
82 | public final boolean equals (Object obj) { | |
83 | 26 | if (obj == null) { |
84 | 0 | return false; |
85 | } | |
86 | 26 | if (obj == this) { |
87 | 4 | return true; |
88 | } | |
89 | 22 | if (obj instanceof Constant) { |
90 | 22 | final Constant<?> other = (Constant<?>) obj; |
91 | 22 | if (getValue().equals(other.getValue())) { |
92 | 21 | return true; |
93 | } | |
94 | } | |
95 | 1 | return false; |
96 | } | |
97 | ||
98 | /** | |
99 | * @since 0.1.0 | |
100 | */ | |
101 | public int hashCode () { | |
102 | 2 | return Constant.class.hashCode() * getValue().hashCode(); |
103 | } | |
104 | ||
105 | } |