1
2
3
4
5
6
7
8
9
10 package ch.panter.li.bi.asn;
11
12 import ch.panter.li.bi.util.ArgumentTool;
13 import ch.panter.li.bi.util.HashCodeTool;
14 import ch.panter.li.bi.util.CompareTool;
15 import ch.panter.li.bi.asn.model.AsnType;
16 import ch.panter.li.bi.asn.model.AsnConstraint;
17 import ch.panter.li.bi.asn.api.ValidationInfoImpl;
18 import ch.panter.li.bi.asn.api.ValidationException;
19
20
21 public abstract class AsnValueBase implements AsnValue
22 {
23
24 protected AsnValueBase( final AsnType type )
25 {
26 this.type = ArgumentTool.nonNullArgument( type );
27 }
28
29 protected AsnValueBase( final AsnValueBase copy )
30 {
31 this.type = copy.type;
32 this.tag = copy.tag;
33 }
34
35 protected static AsnType checkTypeDerivedFrom( final AsnType typeToCheck, final AsnType requiredBaseType )
36 {
37 if ( typeToCheck == null ) {
38 throw new IllegalArgumentException( "type may not be null" );
39 }
40 if ( !typeToCheck.isDerivedFrom( requiredBaseType ) ) {
41 throw new IllegalArgumentException( "type '" + typeToCheck +
42 "' is not derived from the required base type '" + requiredBaseType + "'" );
43 }
44 return typeToCheck;
45 }
46
47 public final AsnType getType()
48 {
49 return type;
50 }
51
52 public final AsnTag getTag()
53 {
54 return tag != null ? tag : type.getTag();
55 }
56
57 public final void setTag( final AsnTag tag )
58 {
59 if ( tag != null ) {
60 try {
61 type.checkCompatible( tag );
62 } catch ( IllegalArgumentException e ) {
63 throw new IllegalArgumentException( "tag '" + tag +
64 "' is not compatible for type '" + type + "' (" + e.getMessage() + ")" );
65 }
66 }
67 this.tag = tag;
68 }
69
70 public final boolean isValid()
71 {
72 final ValidationInfoImpl info = new ValidationInfoImpl();
73 collectValidationInfo( info );
74 return info.getItems().isEmpty();
75 }
76
77 public final void collectValidationInfo( final ValidationInfoImpl info )
78 {
79 if ( type.hasAnyConstraints() ) {
80 AsnType t = type;
81 do {
82 if ( t.hasConstraints() ) {
83 for ( final AsnConstraint constraint : t.getConstraints() ) {
84 constraint.validate( this, info );
85 }
86 }
87 t = t.getBaseType();
88 } while ( t != null );
89 }
90 validateStructure( info );
91 }
92
93 public final void validate() throws ValidationException
94 {
95 final ValidationInfoImpl info = new ValidationInfoImpl();
96 collectValidationInfo( info );
97 if ( !info.getItems().isEmpty() ) {
98 throw new ValidationException( info );
99 }
100 }
101
102 protected void validateStructure( final ValidationInfoImpl info )
103 {
104 }
105
106 public final boolean equals( final Object compare )
107 {
108 if ( this == compare ) {
109 return true;
110 } else if ( compare == null || getClass() != compare.getClass() ) {
111 return false;
112 }
113 final AsnValueBase comp = (AsnValueBase)compare;
114 return CompareTool.areEqual( this.type, comp.type ) &&
115 CompareTool.areEqual( this.tag, comp.tag ) &&
116 isEqualTo( comp );
117 }
118
119 protected boolean isEqualTo( final Object compare )
120 {
121 return true;
122 }
123
124 public final int hashCode()
125 {
126 int hash = getClass().hashCode();
127 hash = HashCodeTool.addHashCode( hash, type );
128 hash = HashCodeTool.addHashCode( hash, tag );
129 return computeHashCode( hash );
130 }
131
132 protected int computeHashCode( final int baseHash )
133 {
134 return baseHash;
135 }
136
137 public String toString()
138 {
139 return "[" + type.toString() + "]";
140 }
141
142
143 private final AsnType type;
144 private AsnTag tag;
145
146 }
147
148