OSDN Git Service

Version 0.11
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / UnknownType.java
1 /*
2  * Copyright 2011 BitMeister Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package jp.bitmeister.asn1.type;
17
18 import java.util.Arrays;
19
20 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
21 import jp.bitmeister.asn1.annotation.ASN1Identifier;
22 import jp.bitmeister.asn1.codec.ber.BerDecoder;
23 import jp.bitmeister.asn1.processor.ASN1Visitor;
24
25 /**
26  * Represents a type that does not have definition.
27  * 
28  * <p>
29  * When a decoder meets a source data with a tag that is not registered to
30  * {@code ASN1Modules}, decoder decodes the data as an {@code UnknownType}. An
31  * instance of this class contains a tag class, a tag number and an array of
32  * {@code byte} represents row contents octets.
33  * </p>
34  * 
35  * @author WATANABE, Jun. <jwat at bitmeister.jp>
36  * 
37  * @see ASN1Modules
38  * @see BerDecoder
39  */
40 @ASN1BuiltIn
41 @ASN1Identifier("(unknown)")
42 public final class UnknownType extends ASN1Type {
43
44         private ASN1TagClass tagClass;
45
46         private int tagNumber;
47
48         private byte[] value;
49
50         /**
51          * Instantiate with a tag class and a tag number.
52          * 
53          * @param tagClass
54          *            ASN.1 tag class.
55          * @param tagNumber
56          *            ASN.1 tag number.
57          */
58         public UnknownType(ASN1TagClass tagClass, int tagNumber) {
59                 this.tagClass = tagClass;
60                 this.tagNumber = tagNumber;
61         }
62
63         /**
64          * Set contents octets.
65          * 
66          * @param value
67          *            The contents octets.
68          */
69         public void set(byte[] value) {
70                 this.value = value;
71         }
72
73         /**
74          * Returns contents octets.
75          * 
76          * @return The contents octets.
77          */
78         public byte[] value() {
79                 return value;
80         }
81
82         /**
83          * Returns tag class.
84          * 
85          * @return The ASN.1 tag class.
86          */
87         public ASN1TagClass tagClass() {
88                 return tagClass;
89         }
90
91         /**
92          * Returns tag number.
93          * 
94          * @return The ASN.1 tag number.
95          */
96         public int tagNumber() {
97                 return tagNumber;
98         }
99
100         /*
101          * (non-Javadoc)
102          * 
103          * @see jp.bitmeister.asn1.type.ASN1Type#clear()
104          */
105         @Override
106         public void clear() {
107                 tagClass = null;
108                 tagNumber = 0;
109                 value = null;
110         }
111
112         /*
113          * (non-Javadoc)
114          * 
115          * @see jp.bitmeister.asn1.type.ASN1Type#hasValue()
116          */
117         @Override
118         public boolean hasValue() {
119                 return value != null;
120         }
121
122         /*
123          * (non-Javadoc)
124          * 
125          * @see
126          * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
127          * ASN1Visitor)
128          */
129         @Override
130         public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
131                 return visitor.visit(this);
132         }
133
134         /*
135          * (non-Javadoc)
136          * 
137          * @see jp.bitmeister.asn1.type.ASN1Type#valueEquals(java.lang.Object)
138          */
139         @Override
140         public boolean valueEquals(Object other) {
141                 if (other instanceof UnknownType) {
142                         UnknownType comparison = (UnknownType) other;
143                         if (comparison.tagClass == tagClass
144                                         && comparison.tagNumber == tagNumber) {
145                                 if (Arrays.equals(comparison.value, value)) {
146                                         return true;
147                                 }
148                         }
149                 }
150                 return false;
151         }
152
153         /*
154          * (non-Javadoc)
155          * 
156          * @see jp.bitmeister.asn1.type.ASN1Type#hashCode()
157          */
158         @Override
159         public int hashCode() {
160                 return value.hashCode();
161         }
162
163         /*
164          * (non-Javadoc)
165          * 
166          * @see jp.bitmeister.asn1.type.ASN1Type#clone()
167          */
168         @Override
169         public Object clone() {
170                 UnknownType clone = new UnknownType(tagClass, tagNumber);
171                 clone.value = value.clone();
172                 return clone;
173         }
174
175 }