1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.policy.predicate; |
11 | |
|
12 | |
import java.util.Map; |
13 | |
|
14 | |
import org.openpermis.policy.TimeStamp; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class Argument<T> implements Value<T> { |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
private String name; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
private Class<?> type; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 125 | public Argument (String name, Class<T> type) { |
42 | 125 | if (name == null || type == null) { |
43 | 2 | throw new IllegalArgumentException("Name or type is null."); |
44 | |
} |
45 | 123 | if (name.equals("")) { |
46 | 1 | throw new IllegalArgumentException("Name is empty."); |
47 | |
} |
48 | 122 | this.name = name; |
49 | 122 | this.type = type; |
50 | 122 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public Class<?> getType () { |
58 | 51 | return this.type; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public String getName () { |
67 | 93 | return this.name; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
@SuppressWarnings("unchecked") |
76 | |
public T valueOf (TimeStamp timeStamp, Map<String, ?> arguments) { |
77 | 38 | final Object result = arguments.get(getName()); |
78 | 38 | if (result != null) { |
79 | 38 | return (T) result; |
80 | |
} |
81 | 0 | throw new IllegalStateException("Runtime evaluation error."); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public boolean isMatchable (Map<String, Class<?>> arguments) { |
88 | 5 | final Object result = arguments.get(getName()); |
89 | 5 | if (result != null) { |
90 | 3 | return true; |
91 | |
} |
92 | 2 | return false; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public final boolean equals (Object obj) { |
101 | 16 | if (obj == null) { |
102 | 0 | return false; |
103 | |
} |
104 | 16 | if (obj == this) { |
105 | 0 | return true; |
106 | |
} |
107 | 16 | if (obj instanceof Argument) { |
108 | 16 | final Argument<?> other = (Argument<?>) obj; |
109 | 16 | if (getName().equals(other.getName())) { |
110 | 16 | return true; |
111 | |
} |
112 | |
} |
113 | 0 | return false; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public int hashCode () { |
120 | 0 | return Argument.class.hashCode() * getName().hashCode(); |
121 | |
} |
122 | |
|
123 | |
} |