1 | package edu.stanford.rt.credential; |
---|
2 | import edu.stanford.rt.datatype.*; |
---|
3 | /** |
---|
4 | * @author Ninghui Li, Sandra Qiu<br> |
---|
5 | * |
---|
6 | * This class handles <code>Principal</code> element, which consists of |
---|
7 | * IntegerValue or StringValue. |
---|
8 | */ |
---|
9 | public class DataValuePrincipal extends Principal |
---|
10 | { |
---|
11 | /** |
---|
12 | * the principal value, which could be an integer or a string. |
---|
13 | */ |
---|
14 | private DataValue value; |
---|
15 | |
---|
16 | /** |
---|
17 | * Constructor for DataValuePrincipal. |
---|
18 | * |
---|
19 | * Contruct a Principal object using a DataValue object. |
---|
20 | * DomainSpecException is thrown if value passed in is not an instance of |
---|
21 | * IntegerValue, StringValue, or KeyValue. |
---|
22 | * <br> |
---|
23 | * <code>IllegalArgumentException</code> is thrown if value |
---|
24 | * is not of <code>IntegerValue</code> or <code>StringValue</code> type. |
---|
25 | */ |
---|
26 | public DataValuePrincipal(DataValue value) |
---|
27 | { |
---|
28 | if (!(value instanceof IntegerValue) |
---|
29 | && !(value instanceof StringValue)) |
---|
30 | throw new IllegalArgumentException("Illegal argument"); |
---|
31 | |
---|
32 | this.value = value; |
---|
33 | } |
---|
34 | /** |
---|
35 | * Method getValue. |
---|
36 | * @return DataValue |
---|
37 | */ |
---|
38 | public DataValue getValue() |
---|
39 | { |
---|
40 | return value; |
---|
41 | } |
---|
42 | /* (non-Javadoc) |
---|
43 | * @see java.lang.Object#toString() |
---|
44 | */ |
---|
45 | public String toString() |
---|
46 | { |
---|
47 | return value.toString(); |
---|
48 | } |
---|
49 | /* (non-Javadoc) |
---|
50 | * @see java.lang.Object#hashCode() |
---|
51 | */ |
---|
52 | public int hashCode() |
---|
53 | { |
---|
54 | return value.hashCode(); |
---|
55 | } |
---|
56 | /* (non-Javadoc) |
---|
57 | * @see java.lang.Object#equals(Object) |
---|
58 | */ |
---|
59 | public boolean equals(Object value) |
---|
60 | { |
---|
61 | if (!(value instanceof IntegerValue) |
---|
62 | && !(value instanceof StringValue)) |
---|
63 | return false; |
---|
64 | |
---|
65 | if (value instanceof IntegerValue) |
---|
66 | { |
---|
67 | if (!(value instanceof IntegerValue)) |
---|
68 | return false; |
---|
69 | else |
---|
70 | return this.hashCode() == value.hashCode(); |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | if (!(value instanceof StringValue)) |
---|
75 | return false; |
---|
76 | else |
---|
77 | return this.hashCode() == value.hashCode(); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /* (non-Javadoc) |
---|
82 | * @see edu.stanford.rt.credential.PrincipalExpression#toString(String) |
---|
83 | */ |
---|
84 | public String toString(String indent) |
---|
85 | { |
---|
86 | String thisIndent = indent + " "; |
---|
87 | return thisIndent + "DataValuePrincipal: " + value.toString(); |
---|
88 | } |
---|
89 | } |
---|