forked from projekt-opal/licenses
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Attribute.java
192 lines (170 loc) · 3.87 KB
/
Attribute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.dice_research.opal.licenses;
import java.text.ParseException;
import java.util.Objects;
/**
* Abstract attribute to extend.
*
* @see https://www.w3.org/TR/odrl-model/
* @see https://www.w3.org/TR/odrl-vocab/
* @see https://w3c.github.io/odrl/bp/
*
* @author Adrian Wilke
*/
public abstract class Attribute extends MetaAttribute {
/**
* Gets boolean representation of binary value.
*
* @throws ParseException if not 0 or 1
*/
public static final boolean binaryToBoolean(int binary) throws ParseException {
if (binary == 0) {
return false;
} else if (binary == 1) {
return true;
} else {
throw new ParseException(Integer.toString(binary), 0);
}
}
/**
* Gets binary representation of boolean value.
*/
public static final int booleanToBinary(boolean bool) {
if (bool == false) {
return 0;
} else {
return 1;
}
}
private String uri = null;
private Boolean value = null;
/**
* Compares {@link Attribute} instances by URI.
*/
public int compareTo(Attribute o) {
return getUri().compareTo(o.getUri());
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
// Only obj is null
return false;
} else if (obj == this) {
// Same instance
return true;
} else if (!(this.getClass().isInstance(obj))) {
// Classes and subclasses
return false;
} else {
// URI based
return hashCode() == obj.hashCode();
}
}
/**
* Maps attribute value to binary representation.
*
* @throws NullPointerException if not set
*/
public int getBinaryValue() throws NullPointerException {
if (getValue() == null) {
throw new NullPointerException();
} else {
return booleanToBinary(getValue());
}
}
/**
* Gets internal value for computation. (E.g. values of permissions are
* inverted.)
*
* @throws NullPointerException if not set
*/
public Boolean getInternalValue() throws NullPointerException {
if (value == null) {
throw new NullPointerException();
}
return invertForComputation() ? !value : value;
}
/**
* Gets short representation of attribute.
*/
public abstract String getShortForm();
/**
* Gets type of attribute.
*/
public abstract String getType();
/**
* Gets URI identifying this attribute.
*
* @throws NullPointerException if not set
*/
public String getUri() throws NullPointerException {
if (uri == null) {
throw new NullPointerException();
}
return uri;
}
/**
* Gets value of this attribute.
*
* @throws NullPointerException if not set
*/
public Boolean getValue() throws NullPointerException {
if (value == null) {
throw new NullPointerException();
}
return value;
}
@Override
public int hashCode() {
return Objects.hash(uri);
}
/**
* Checks, if the value is not null.
*/
public boolean hasValue() throws NullPointerException {
return (value != null);
}
/**
* Gets type of attribute.
*/
public abstract boolean invertForComputation();
/**
* Sets URI identifying this attribute.
*
* @throws NullPointerException if given URI is null
*/
public Attribute setUri(String uri) {
if (uri == null) {
throw new NullPointerException();
}
this.uri = uri;
return this;
}
/**
* Sets value of this attribute.
*/
public Attribute setValue(boolean value) {
this.value = value;
return this;
}
/**
* Parses binary value and returns instance.
*
* @throws ParseException if not 0 or 1
*/
public Attribute setValue(int binary) throws ParseException {
setValue(binaryToBoolean(binary));
return this;
}
/**
* Sets value of by parsing the given internal value.
*/
public Attribute setValueByInternal(boolean internalValue) {
this.value = invertForComputation() ? !internalValue : internalValue;
return this;
}
@Override
public String toString() {
return getUri() + (value == null ? "" : "=" + value) + " (" + (isMetaAttribute() ? "Meta-" : "") + getType()
+ ")";
}
}