View Javadoc

1   // -- FILE ------------------------------------------------------------------
2   // name       : AsnValueBase.java
3   // project    : Panter: LI
4   // created    : lep - 2007.01.03
5   // language   : java
6   // environment: JDK 1.5.0
7   // copyright  : (c) 2006 by Panter llc, Switzerland
8   // license    : this is free software licensed under the GPL. see COPYING
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    } // AsnValueBase
28  
29    protected AsnValueBase( final AsnValueBase copy )
30    {
31      this.type = copy.type;
32      this.tag = copy.tag;
33    } // AsnValueBase
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    } // checkTypeDerivedFrom
46  
47    public final AsnType getType()
48    {
49      return type;
50    } // getType
51  
52    public final AsnTag getTag()
53    {
54      return tag != null ? tag : type.getTag();
55    } // getTag
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    } // setTag
69  
70    public final boolean isValid()
71    {
72      final ValidationInfoImpl info = new ValidationInfoImpl();
73      collectValidationInfo( info );
74      return info.getItems().isEmpty();
75    } // isValid
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    } // collectValidationInfo
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   } // validate
101 
102   protected void validateStructure( final ValidationInfoImpl info )
103   {
104   } // validateStructure
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   } // equals
118 
119   protected boolean isEqualTo( final Object compare )
120   {
121     return true;
122   } // isEqualTo
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   } // hashCode
131 
132   protected int computeHashCode( final int baseHash )
133   {
134     return baseHash;
135   } // computeHashCode
136 
137   public String toString()
138   {
139     return "[" + type.toString() + "]";
140   } // toString
141 
142   // members
143   private final AsnType type;
144   private AsnTag tag;
145 
146 } // class AsnValueBase
147 
148 // -- EOF -------------------------------------------------------------------