Commit MetaInfo

Revisionf732458a3fe40c8880ff279acc75f4b37402edef (tree)
Time2012-10-04 20:10:38
Authorjwat <jwat@user...>
Commiterjwat

Log Message

* Add size restricted 'INTEGER' and 'ENUMERATED' types which contain a Long value.
* 'INTEGER' and 'ENUMERATED' types which contain a BigInteger value renamed to 'BigINTEGER' and 'BigENUMERATED'.
* Some bugs were fixed.

Change Summary

Incremental Difference

--- a/jp/bitmeister/asn1/annotation/ASN1Enumeration.java
+++ b/jp/bitmeister/asn1/annotation/ASN1Enumeration.java
@@ -20,6 +20,8 @@ import java.lang.annotation.Retention;
2020 import java.lang.annotation.RetentionPolicy;
2121 import java.lang.annotation.Target;
2222
23+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
24+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
2325 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
2426 import jp.bitmeister.asn1.type.builtin.INTEGER;
2527
@@ -28,14 +30,15 @@ import jp.bitmeister.asn1.type.builtin.INTEGER;
2830 *
2931 * <p>
3032 * If a field annotated as {@code @ASN1Enumeration} is present in a definition
31- * of a sub-class of {@code INTEGER} or {@code ENUMERATED}, value that can be
32- * contained in the type is limited to values of {@code @ASN1Enumeration}
33- * fields. An {@code @ASN1Enumeration} field must be a {@code public}
34- * {@code static} {@code final} field and the type shall be {@code int},
35- * {@code long} or {@code BigInteger}. Each value of {@code @ASN1Enumeration}
36- * fields appearing in definition of a type must be distinct from the others. A
37- * sub-class of {@code ENUMERATED} must have at least one
38- * {@code @ASN1Enumeration} field.
33+ * of a sub-class of {@code INTEGER}, {@code ENUMERATED}, {@code BigINTEGER} or
34+ * {@code BigENUMERATED}, value that can be contained in the type is limited to
35+ * values of {@code @ASN1Enumeration} fields. An {@code @ASN1Enumeration} field
36+ * must be a {@code public} {@code static} {@code final} field and the type
37+ * shall be {@code int}, {@code long} or {@code BigInteger} (only for
38+ * {@code BigINTEGER} or {@code BigENUMERATED}). Each value of
39+ * {@code @ASN1Enumeration} fields appearing in definition of a type must be
40+ * distinct from the others. A sub-class of {@code ENUMERATED} or
41+ * {@code BigENUMERATED} must have at least one {@code @ASN1Enumeration} field.
3942 * </p>
4043 * <p>
4144 * For example, following ASN.1 definition :
@@ -66,6 +69,8 @@ import jp.bitmeister.asn1.type.builtin.INTEGER;
6669 *
6770 * @see INTEGER
6871 * @see ENUMERATED
72+ * @see BigINTEGER
73+ * @see BigENUMERATED
6974 */
7075 @Target(ElementType.FIELD)
7176 @Retention(RetentionPolicy.RUNTIME)
--- a/jp/bitmeister/asn1/codec/ber/BerDecoder.java
+++ b/jp/bitmeister/asn1/codec/ber/BerDecoder.java
@@ -42,6 +42,8 @@ import jp.bitmeister.asn1.type.UnknownType;
4242 import jp.bitmeister.asn1.type.builtin.ANY;
4343 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
4444 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
45+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
46+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
4547 import jp.bitmeister.asn1.type.builtin.CHOICE;
4648 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
4749 import jp.bitmeister.asn1.type.builtin.INTEGER;
@@ -207,9 +209,21 @@ public class BerDecoder implements ASN1Decoder,
207209 *
208210 * @see
209211 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
210- * .IntegerType)
212+ * .builtin.INTEGER)
211213 */
212214 public Void visit(INTEGER data) throws ASN1DecodingException {
215+ data.set(new BigInteger(readStream(readLength())).longValue());
216+ return null;
217+ }
218+
219+ /*
220+ * (non-Javadoc)
221+ *
222+ * @see
223+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
224+ * .IntegerType)
225+ */
226+ public Void visit(BigINTEGER data) throws ASN1DecodingException {
213227 data.set(new BigInteger(readStream(readLength())));
214228 return null;
215229 }
@@ -219,12 +233,24 @@ public class BerDecoder implements ASN1Decoder,
219233 *
220234 * @see
221235 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
222- * .EnumeratedType)
236+ * .builtin.ENUMERATED)
223237 */
224238 public Void visit(ENUMERATED data) throws ASN1DecodingException {
225239 visit((INTEGER) data);
226240 return null;
227241 }
242+
243+ /*
244+ * (non-Javadoc)
245+ *
246+ * @see
247+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
248+ * .EnumeratedType)
249+ */
250+ public Void visit(BigENUMERATED data) throws ASN1DecodingException {
251+ visit((BigINTEGER) data);
252+ return null;
253+ }
228254
229255 /*
230256 * (non-Javadoc)
--- a/jp/bitmeister/asn1/codec/ber/BerEncoder.java
+++ b/jp/bitmeister/asn1/codec/ber/BerEncoder.java
@@ -39,6 +39,8 @@ import jp.bitmeister.asn1.type.builtin.ANY;
3939 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
4040 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
4141 import jp.bitmeister.asn1.type.builtin.CHOICE;
42+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
43+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
4244 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
4345 import jp.bitmeister.asn1.type.builtin.INTEGER;
4446 import jp.bitmeister.asn1.type.builtin.NULL;
@@ -155,6 +157,13 @@ public class BerEncoder implements ASN1Encoder,
155157 return newPrimitiveOctets(data.value() ? (byte) 0xff : (byte) 0x00);
156158 }
157159
160+ /* (non-Javadoc)
161+ * @see jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type.builtin.INTEGER)
162+ */
163+ public EncodedOctets visit(INTEGER data) throws ASN1EncodingException {
164+ return newPrimitiveOctets(BigInteger.valueOf(data.value()).toByteArray());
165+ }
166+
158167 /*
159168 * (non-Javadoc)
160169 *
@@ -162,9 +171,16 @@ public class BerEncoder implements ASN1Encoder,
162171 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
163172 * .builtin.INTEGER)
164173 */
165- public EncodedOctets visit(INTEGER data) {
174+ public EncodedOctets visit(BigINTEGER data) {
166175 return newPrimitiveOctets(data.value().toByteArray());
167176 }
177+
178+ /* (non-Javadoc)
179+ * @see jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type.builtin.ENUMERATED)
180+ */
181+ public EncodedOctets visit(ENUMERATED data) throws ASN1EncodingException {
182+ return visit((INTEGER)data);
183+ }
168184
169185 /*
170186 * (non-Javadoc)
@@ -173,10 +189,10 @@ public class BerEncoder implements ASN1Encoder,
173189 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
174190 * .builtin.ENUMERATED)
175191 */
176- public EncodedOctets visit(ENUMERATED data) {
177- return visit((INTEGER) data);
192+ public EncodedOctets visit(BigENUMERATED data) {
193+ return visit((BigINTEGER) data);
178194 }
179-
195+
180196 /*
181197 * (non-Javadoc)
182198 *
--- a/jp/bitmeister/asn1/codec/xer/XerDecoder.java
+++ b/jp/bitmeister/asn1/codec/xer/XerDecoder.java
@@ -41,6 +41,8 @@ import jp.bitmeister.asn1.type.UnknownType;
4141 import jp.bitmeister.asn1.type.builtin.ANY;
4242 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
4343 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
44+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
45+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
4446 import jp.bitmeister.asn1.type.builtin.CHOICE;
4547 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
4648 import jp.bitmeister.asn1.type.builtin.INTEGER;
@@ -549,6 +551,43 @@ public class XerDecoder implements ASN1Decoder,
549551 */
550552 @Override
551553 public void characters(String characters) {
554+ data.set(Long.valueOf(characters));
555+ }
556+
557+ };
558+ }
559+
560+ /*
561+ * (non-Javadoc)
562+ *
563+ * @see
564+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
565+ * .builtin.BigINTEGER)
566+ */
567+ public DataDecoder visit(final BigINTEGER data) {
568+ return new DataDecoder() {
569+
570+ /*
571+ * (non-Javadoc)
572+ *
573+ * @see
574+ * jp.bitmeister.asn1.codec.xer.XerDecoder.DataDecoder#startChildElement
575+ * (java.lang.String)
576+ */
577+ @Override
578+ void startChildElement(String qName) {
579+ data.set(qName);
580+ }
581+
582+ /*
583+ * (non-Javadoc)
584+ *
585+ * @see
586+ * jp.bitmeister.asn1.codec.xer.XerDecoder.DataDecoder#characters
587+ * (java.lang.String)
588+ */
589+ @Override
590+ public void characters(String characters) {
552591 data.set(new BigInteger(characters));
553592 }
554593
@@ -708,7 +747,7 @@ public class XerDecoder implements ASN1Decoder,
708747
709748 };
710749 }
711-
750+
712751 /*
713752 * (non-Javadoc)
714753 *
@@ -719,6 +758,17 @@ public class XerDecoder implements ASN1Decoder,
719758 public DataDecoder visit(ENUMERATED data) {
720759 return visit((INTEGER) data);
721760 }
761+
762+ /*
763+ * (non-Javadoc)
764+ *
765+ * @see
766+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
767+ * .builtin.BigENUMERATED)
768+ */
769+ public DataDecoder visit(BigENUMERATED data) {
770+ return visit((BigINTEGER) data);
771+ }
722772
723773 /*
724774 * (non-Javadoc)
@@ -978,6 +1028,7 @@ public class XerDecoder implements ASN1Decoder,
9781028 void startElement(String qName) {
9791029 if (BOOLEAN.class.isAssignableFrom(data.componentType())
9801030 || ENUMERATED.class.isAssignableFrom(data.componentType())
1031+ || BigENUMERATED.class.isAssignableFrom(data.componentType())
9811032 || CHOICE.class.isAssignableFrom(data.componentType())) {
9821033 useXmlValueList = true;
9831034 }
--- a/jp/bitmeister/asn1/codec/xer/XerEncoder.java
+++ b/jp/bitmeister/asn1/codec/xer/XerEncoder.java
@@ -36,6 +36,7 @@ import jp.bitmeister.asn1.exception.ASN1EncodingException;
3636 import jp.bitmeister.asn1.processor.ASN1Visitor;
3737 import jp.bitmeister.asn1.type.ASN1Module;
3838 import jp.bitmeister.asn1.type.ASN1Type;
39+import jp.bitmeister.asn1.type.AbstractInteger;
3940 import jp.bitmeister.asn1.type.CollectionType;
4041 import jp.bitmeister.asn1.type.ConstructiveType;
4142 import jp.bitmeister.asn1.type.ElementSpecification;
@@ -45,6 +46,8 @@ import jp.bitmeister.asn1.type.UnknownType;
4546 import jp.bitmeister.asn1.type.builtin.ANY;
4647 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
4748 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
49+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
50+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
4851 import jp.bitmeister.asn1.type.builtin.CHOICE;
4952 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
5053 import jp.bitmeister.asn1.type.builtin.INTEGER;
@@ -215,13 +218,19 @@ public class XerEncoder implements ASN1Encoder,
215218 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
216219 * .builtin.INTEGER)
217220 */
218- public String visit(INTEGER data) {
219- String numberId = data.identifier();
220- if (numberId != null) {
221- return TAG_START + numberId + SINGLE_TAG_END;
222- } else {
223- return data.value().toString();
224- }
221+ public String visit(INTEGER data) throws ASN1EncodingException {
222+ return encodeInteger(data);
223+ }
224+
225+ /*
226+ * (non-Javadoc)
227+ *
228+ * @see
229+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
230+ * .builtin.BigINTEGER)
231+ */
232+ public String visit(BigINTEGER data) {
233+ return encodeInteger(data);
225234 }
226235
227236 /*
@@ -331,7 +340,18 @@ public class XerEncoder implements ASN1Encoder,
331340 * .builtin.ENUMERATED)
332341 */
333342 public String visit(ENUMERATED data) {
334- return visit((INTEGER) data);
343+ return encodeInteger(data);
344+ }
345+
346+ /*
347+ * (non-Javadoc)
348+ *
349+ * @see
350+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
351+ * .builtin.BigENUMERATED)
352+ */
353+ public String visit(BigENUMERATED data) {
354+ return encodeInteger(data);
335355 }
336356
337357 /*
@@ -451,6 +471,22 @@ public class XerEncoder implements ASN1Encoder,
451471 }
452472
453473 /**
474+ * Encodes an ASN.1 'INTEGER' or 'ENUMERATED' data.
475+ *
476+ * @param data
477+ * The integer data.
478+ * @return The XER representation of the data.
479+ */
480+ private String encodeInteger(AbstractInteger<?> data) {
481+ String numberId = data.identifier();
482+ if (numberId != null) {
483+ return TAG_START + numberId + SINGLE_TAG_END;
484+ } else {
485+ return data.value().toString();
486+ }
487+ }
488+
489+ /**
454490 * Encodes each element of the {@code ConstructiveType} data.
455491 *
456492 * @param data
@@ -487,6 +523,7 @@ public class XerEncoder implements ASN1Encoder,
487523 builder = new StringBuilder();
488524 if (BOOLEAN.class.isAssignableFrom(data.componentType())
489525 || ENUMERATED.class.isAssignableFrom(data.componentType())
526+ || BigENUMERATED.class.isAssignableFrom(data.componentType())
490527 || CHOICE.class.isAssignableFrom(data.componentType())) {
491528 for (ASN1Type e : data.collection()) {
492529 builder.append(e.accept(this));
--- a/jp/bitmeister/asn1/exception/ASN1DecodingException.java
+++ b/jp/bitmeister/asn1/exception/ASN1DecodingException.java
@@ -22,4 +22,21 @@ package jp.bitmeister.asn1.exception;
2222 */
2323 @SuppressWarnings("serial")
2424 public class ASN1DecodingException extends ASN1Exception {
25+
26+ /**
27+ * Instantiate an {@code ASN1DecodingException}.
28+ */
29+ public ASN1DecodingException() {
30+ }
31+
32+ /**
33+ * Instantiate an {@code ASN1DecodingException} with specified error message.
34+ *
35+ * @param message
36+ * The error message.
37+ */
38+ public ASN1DecodingException(String message) {
39+ super(message);
40+ }
41+
2542 }
--- a/jp/bitmeister/asn1/exception/ASN1EncodingException.java
+++ b/jp/bitmeister/asn1/exception/ASN1EncodingException.java
@@ -22,4 +22,21 @@ package jp.bitmeister.asn1.exception;
2222 */
2323 @SuppressWarnings("serial")
2424 public class ASN1EncodingException extends ASN1Exception {
25+
26+ /**
27+ * Instantiate an {@code ASN1EncodingException}.
28+ */
29+ public ASN1EncodingException() {
30+ }
31+
32+ /**
33+ * Instantiate an {@code ASN1EncodingException} with specified error message.
34+ *
35+ * @param message
36+ * The error message.
37+ */
38+ public ASN1EncodingException(String message) {
39+ super(message);
40+ }
41+
2542 }
--- a/jp/bitmeister/asn1/exception/ASN1Exception.java
+++ b/jp/bitmeister/asn1/exception/ASN1Exception.java
@@ -26,6 +26,22 @@ import jp.bitmeister.asn1.type.ASN1Type;
2626 public class ASN1Exception extends Exception {
2727
2828 protected DetailedMessage message;
29+
30+ /**
31+ * Instantiate an {@code ASN1Exception}.
32+ */
33+ public ASN1Exception() {
34+ }
35+
36+ /**
37+ * Instantiate an {@code ASN1Exception} with specified error message.
38+ *
39+ * @param message
40+ * The error message.
41+ */
42+ public ASN1Exception(String message) {
43+ super(message);
44+ }
2945
3046 /**
3147 * Set detailed message of this exception.
--- a/jp/bitmeister/asn1/exception/ASN1IllegalArgument.java
+++ b/jp/bitmeister/asn1/exception/ASN1IllegalArgument.java
@@ -22,4 +22,21 @@ package jp.bitmeister.asn1.exception;
2222 */
2323 @SuppressWarnings("serial")
2424 public class ASN1IllegalArgument extends ASN1RuntimeException {
25+
26+ /**
27+ * Instantiate an {@code ASN1IllegalArgument}.
28+ */
29+ public ASN1IllegalArgument() {
30+ }
31+
32+ /**
33+ * Instantiate an {@code ASN1IllegalArgument} with specified error message.
34+ *
35+ * @param message
36+ * The error message.
37+ */
38+ public ASN1IllegalArgument(String message) {
39+ super(message);
40+ }
41+
2542 }
--- a/jp/bitmeister/asn1/exception/ASN1IllegalDefinition.java
+++ b/jp/bitmeister/asn1/exception/ASN1IllegalDefinition.java
@@ -22,4 +22,21 @@ package jp.bitmeister.asn1.exception;
2222 */
2323 @SuppressWarnings("serial")
2424 public class ASN1IllegalDefinition extends ASN1RuntimeException {
25+
26+ /**
27+ * Instantiate an {@code ASN1IllegalDefinition}.
28+ */
29+ public ASN1IllegalDefinition() {
30+ }
31+
32+ /**
33+ * Instantiate an {@code ASN1IllegalDefinition} with specified error message.
34+ *
35+ * @param message
36+ * The error message.
37+ */
38+ public ASN1IllegalDefinition(String message) {
39+ super(message);
40+ }
41+
2542 }
--- a/jp/bitmeister/asn1/exception/ASN1InvalidDataValue.java
+++ b/jp/bitmeister/asn1/exception/ASN1InvalidDataValue.java
@@ -22,4 +22,21 @@ package jp.bitmeister.asn1.exception;
2222 */
2323 @SuppressWarnings("serial")
2424 public class ASN1InvalidDataValue extends ASN1RuntimeException {
25+
26+ /**
27+ * Instantiate an {@code ASN1InvalidDataValue}.
28+ */
29+ public ASN1InvalidDataValue() {
30+ }
31+
32+ /**
33+ * Instantiate an {@code ASN1InvalidDataValue} with specified error message.
34+ *
35+ * @param message
36+ * The error message.
37+ */
38+ public ASN1InvalidDataValue(String message) {
39+ super(message);
40+ }
41+
2542 }
--- a/jp/bitmeister/asn1/exception/ASN1RuntimeException.java
+++ b/jp/bitmeister/asn1/exception/ASN1RuntimeException.java
@@ -27,7 +27,23 @@ import jp.bitmeister.asn1.type.ASN1Type;
2727 public class ASN1RuntimeException extends RuntimeException {
2828
2929 private DetailedMessage message;
30-
30+
31+ /**
32+ * Instantiate an {@code ASN1RuntimeException}.
33+ */
34+ public ASN1RuntimeException() {
35+ }
36+
37+ /**
38+ * Instantiate an {@code ASN1RuntimeException} with specified error message.
39+ *
40+ * @param message
41+ * The error message.
42+ */
43+ public ASN1RuntimeException(String message) {
44+ super(message);
45+ }
46+
3147 /**
3248 * Set detailed message of this exception.
3349 *
--- a/jp/bitmeister/asn1/processor/ASN1StringBuilder.java
+++ b/jp/bitmeister/asn1/processor/ASN1StringBuilder.java
@@ -24,6 +24,7 @@ import jp.bitmeister.asn1.type.ASN1TagClass;
2424 import jp.bitmeister.asn1.type.ASN1TagMode;
2525 import jp.bitmeister.asn1.type.ASN1TagValue;
2626 import jp.bitmeister.asn1.type.ASN1Type;
27+import jp.bitmeister.asn1.type.AbstractInteger;
2728 import jp.bitmeister.asn1.type.CollectionType;
2829 import jp.bitmeister.asn1.type.ConstructiveType;
2930 import jp.bitmeister.asn1.type.ElementSpecification;
@@ -35,6 +36,8 @@ import jp.bitmeister.asn1.type.UnknownType;
3536 import jp.bitmeister.asn1.type.builtin.ANY;
3637 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
3738 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
39+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
40+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
3841 import jp.bitmeister.asn1.type.builtin.CHOICE;
3942 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
4043 import jp.bitmeister.asn1.type.builtin.INTEGER;
@@ -195,20 +198,40 @@ public class ASN1StringBuilder implements
195198 builder.append(data.value() ? "TRUE" : "FALSE");
196199 return null;
197200 }
201+
202+ /*
203+ * (non-Javadoc)
204+ *
205+ * @see
206+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1
207+ * .type.builtin.INTEGER)
208+ */
209+ public Void visit(INTEGER data) throws ASN1RuntimeException {
210+ writeInteger((AbstractInteger<?>) data);
211+ return null;
212+ }
198213
199214 /*
200215 * (non-Javadoc)
201216 *
202217 * @see
203218 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
204- * .builtin.INTEGER)
219+ * .builtin.BigINTEGER)
205220 */
206- public Void visit(INTEGER data) {
207- String numberId = data.identifier();
208- if (numberId != null) {
209- builder.append(numberId);
210- }
211- builder.append(NUMBER_OPEN).append(data.value()).append(NUMBER_CLOSE);
221+ public Void visit(BigINTEGER data) {
222+ writeInteger((AbstractInteger<?>)data);
223+ return null;
224+ }
225+
226+ /*
227+ * (non-Javadoc)
228+ *
229+ * @see
230+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1
231+ * .type.builtin.ENUMERATED)
232+ */
233+ public Void visit(ENUMERATED data) {
234+ writeInteger((AbstractInteger<?>) data);
212235 return null;
213236 }
214237
@@ -217,10 +240,10 @@ public class ASN1StringBuilder implements
217240 *
218241 * @see
219242 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
220- * .builtin.ENUMERATED)
243+ * .builtin.BigENUMERATED)
221244 */
222- public Void visit(ENUMERATED data) {
223- visit((INTEGER) data);
245+ public Void visit(BigENUMERATED data) {
246+ writeInteger((AbstractInteger<?>)data);
224247 return null;
225248 }
226249
@@ -450,6 +473,18 @@ public class ASN1StringBuilder implements
450473 }
451474
452475 /**
476+ * Writes value and identifier of an ASN.1 'INTEGER' or 'ENUMERATED' value.
477+ * @param data The integer data.
478+ */
479+ private void writeInteger(AbstractInteger<?> data) {
480+ String numberId = data.identifier();
481+ if (numberId != null) {
482+ builder.append(numberId);
483+ }
484+ builder.append(NUMBER_OPEN).append(data.value()).append(NUMBER_CLOSE);
485+ }
486+
487+ /**
453488 * Writes descriptions of each element of {@code ConstructiveType}.
454489 *
455490 * @param data
--- a/jp/bitmeister/asn1/processor/ASN1Visitor.java
+++ b/jp/bitmeister/asn1/processor/ASN1Visitor.java
@@ -22,6 +22,8 @@ import jp.bitmeister.asn1.type.UnknownType;
2222 import jp.bitmeister.asn1.type.builtin.ANY;
2323 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
2424 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
25+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
26+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
2527 import jp.bitmeister.asn1.type.builtin.CHOICE;
2628 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
2729 import jp.bitmeister.asn1.type.builtin.INTEGER;
@@ -49,7 +51,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
4951 * The data to be visited.
5052 * @return Result.
5153 * @throws E
52- * When an error occured in the {@code accept} method.
54+ * When an error occurred in the {@code accept} method.
5355 */
5456 public R visit(BOOLEAN data) throws E;
5557
@@ -60,9 +62,20 @@ public interface ASN1Visitor<R, E extends Throwable> {
6062 * The data to be visited.
6163 * @return Result.
6264 * @throws E
63- * When an error occured in the {@code accept} method.
65+ * When an error occurred in the {@code accept} method.
6466 */
6567 public R visit(INTEGER data) throws E;
68+
69+ /**
70+ * Visits the {@code BigINTEGER} data.
71+ *
72+ * @param data
73+ * The data to be visited.
74+ * @return Result.
75+ * @throws E
76+ * When an error occurred in the {@code accept} method.
77+ */
78+ public R visit(BigINTEGER data) throws E;
6679
6780 /**
6881 * Visits the {@code BIT_STRING} data.
@@ -71,7 +84,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
7184 * The data to be visited.
7285 * @return Result.
7386 * @throws E
74- * When an error occured in the {@code accept} method.
87+ * When an error occurred in the {@code accept} method.
7588 */
7689 public R visit(BIT_STRING data) throws E;
7790
@@ -82,7 +95,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
8295 * The data to be visited.
8396 * @return Result.
8497 * @throws E
85- * When an error occured in the {@code accept} method.
98+ * When an error occurred in the {@code accept} method.
8699 */
87100 public R visit(OCTET_STRING data) throws E;
88101
@@ -93,7 +106,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
93106 * The data to be visited.
94107 * @return Result.
95108 * @throws E
96- * When an error occured in the {@code accept} method.
109+ * When an error occurred in the {@code accept} method.
97110 */
98111 public R visit(NULL data) throws E;
99112
@@ -104,7 +117,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
104117 * The data to be visited.
105118 * @return Result.
106119 * @throws E
107- * When an error occured in the {@code accept} method.
120+ * When an error occurred in the {@code accept} method.
108121 */
109122 public R visit(OBJECT_IDENTIFIER data) throws E;
110123
@@ -115,7 +128,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
115128 * The data to be visited.
116129 * @return Result.
117130 * @throws E
118- * When an error occured in the {@code accept} method.
131+ * When an error occurred in the {@code accept} method.
119132 */
120133 public R visit(REAL data) throws E;
121134
@@ -126,18 +139,29 @@ public interface ASN1Visitor<R, E extends Throwable> {
126139 * The data to be visited.
127140 * @return Result.
128141 * @throws E
129- * When an error occured in the {@code accept} method.
142+ * When an error occurred in the {@code accept} method.
130143 */
131144 public R visit(ENUMERATED data) throws E;
132145
133146 /**
147+ * Visits the {@code BigENUMERATED} data.
148+ *
149+ * @param data
150+ * The data to be visited.
151+ * @return Result.
152+ * @throws E
153+ * When an error occurred in the {@code accept} method.
154+ */
155+ public R visit(BigENUMERATED data) throws E;
156+
157+ /**
134158 * Visits the {@code RELATIVE_OID} data.
135159 *
136160 * @param data
137161 * The data to be visited.
138162 * @return Result.
139163 * @throws E
140- * When an error occured in the {@code accept} method.
164+ * When an error occurred in the {@code accept} method.
141165 */
142166 public R visit(RELATIVE_OID data) throws E;
143167
@@ -148,7 +172,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
148172 * The data to be visited.
149173 * @return Result.
150174 * @throws E
151- * When an error occured in the {@code accept} method.
175+ * When an error occurred in the {@code accept} method.
152176 */
153177 public R visit(ANY data) throws E;
154178
@@ -159,7 +183,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
159183 * The data to be visited.
160184 * @return Result.
161185 * @throws E
162- * When an error occured in the {@code accept} method.
186+ * When an error occurred in the {@code accept} method.
163187 */
164188 public R visit(CHOICE data) throws E;
165189
@@ -170,7 +194,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
170194 * The data to be visited.
171195 * @return Result.
172196 * @throws E
173- * When an error occured in the {@code accept} method.
197+ * When an error occurred in the {@code accept} method.
174198 */
175199 public R visit(SEQUENCE_OF<? extends ASN1Type> data) throws E;
176200
@@ -181,7 +205,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
181205 * The data to be visited.
182206 * @return Result.
183207 * @throws E
184- * When an error occured in the {@code accept} method.
208+ * When an error occurred in the {@code accept} method.
185209 */
186210 public R visit(SEQUENCE data) throws E;
187211
@@ -192,7 +216,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
192216 * The data to be visited.
193217 * @return Result.
194218 * @throws E
195- * When an error occured in the {@code accept} method.
219+ * When an error occurred in the {@code accept} method.
196220 */
197221 public R visit(SET_OF<? extends ASN1Type> data) throws E;
198222
@@ -203,7 +227,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
203227 * The data to be visited.
204228 * @return Result.
205229 * @throws E
206- * When an error occured in the {@code accept} method.
230+ * When an error occurred in the {@code accept} method.
207231 */
208232 public R visit(SET data) throws E;
209233
@@ -214,7 +238,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
214238 * The data to be visited.
215239 * @return Result.
216240 * @throws E
217- * When an error occured in the {@code accept} method.
241+ * When an error occurred in the {@code accept} method.
218242 */
219243 public R visit(StringType data) throws E;
220244
@@ -225,7 +249,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
225249 * The data to be visited.
226250 * @return Result.
227251 * @throws E
228- * When an error occured in the {@code accept} method.
252+ * When an error occurred in the {@code accept} method.
229253 */
230254 public R visit(TimeType data) throws E;
231255
@@ -236,7 +260,7 @@ public interface ASN1Visitor<R, E extends Throwable> {
236260 * The data to be visited.
237261 * @return Result.
238262 * @throws E
239- * When an error occured in the {@code accept} method.
263+ * When an error occurred in the {@code accept} method.
240264 */
241265 public R visit(UnknownType data) throws E;
242266
--- a/jp/bitmeister/asn1/processor/ASN1VisitorAdaptor.java
+++ b/jp/bitmeister/asn1/processor/ASN1VisitorAdaptor.java
@@ -22,6 +22,8 @@ import jp.bitmeister.asn1.type.UnknownType;
2222 import jp.bitmeister.asn1.type.builtin.ANY;
2323 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
2424 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
25+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
26+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
2527 import jp.bitmeister.asn1.type.builtin.CHOICE;
2628 import jp.bitmeister.asn1.type.builtin.ENUMERATED;
2729 import jp.bitmeister.asn1.type.builtin.INTEGER;
@@ -60,7 +62,7 @@ public class ASN1VisitorAdaptor<R, E extends Throwable> implements ASN1Visitor<R
6062 public R visit(BOOLEAN data) throws E {
6163 return null;
6264 }
63-
65+
6466 /*
6567 * (non-Javadoc)
6668 *
@@ -77,6 +79,17 @@ public class ASN1VisitorAdaptor<R, E extends Throwable> implements ASN1Visitor<R
7779 *
7880 * @see
7981 * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
82+ * .builtin.BigINTEGER)
83+ */
84+ public R visit(BigINTEGER data) throws E {
85+ return null;
86+ }
87+
88+ /*
89+ * (non-Javadoc)
90+ *
91+ * @see
92+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
8093 * .builtin.BIT_STRING)
8194 */
8295 public R visit(BIT_STRING data) throws E {
@@ -126,7 +139,7 @@ public class ASN1VisitorAdaptor<R, E extends Throwable> implements ASN1Visitor<R
126139 public R visit(REAL data) throws E {
127140 return null;
128141 }
129-
142+
130143 /*
131144 * (non-Javadoc)
132145 *
@@ -138,6 +151,17 @@ public class ASN1VisitorAdaptor<R, E extends Throwable> implements ASN1Visitor<R
138151 return null;
139152 }
140153
154+ /*
155+ * (non-Javadoc)
156+ *
157+ * @see
158+ * jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type
159+ * .builtin.BigENUMERATED)
160+ */
161+ public R visit(BigENUMERATED data) throws E {
162+ return null;
163+ }
164+
141165 /* (non-Javadoc)
142166 * @see jp.bitmeister.asn1.processor.ASN1Visitor#visit(jp.bitmeister.asn1.type.builtin.RELATIVE_OID)
143167 */
--- a/jp/bitmeister/asn1/sample/FrightStatusMain.java
+++ b/jp/bitmeister/asn1/sample/FrightStatusMain.java
@@ -72,7 +72,7 @@ public class FrightStatusMain {
7272 System.out.println("Source data:");
7373 System.out.println(frights);
7474 derEncAndDec(frights);
75- xerEncAndDec(frights);
75+ xerEncAndDec(frights);
7676 }
7777
7878 static void derEncAndDec(ASN1Type data) {
--- a/jp/bitmeister/asn1/sample/FrightStatusTypes.java
+++ b/jp/bitmeister/asn1/sample/FrightStatusTypes.java
@@ -15,8 +15,6 @@
1515 */
1616 package jp.bitmeister.asn1.sample;
1717
18-import java.math.BigInteger;
19-
2018 import jp.bitmeister.asn1.annotation.ASN1Alternative;
2119 import jp.bitmeister.asn1.annotation.ASN1DefinedTypes;
2220 import jp.bitmeister.asn1.annotation.ASN1Element;
@@ -70,7 +68,7 @@ public class FrightStatusTypes extends ASN1Module {
7068 public Airport() {
7169 }
7270
73- public Airport(BigInteger value) {
71+ public Airport(Integer value) {
7472 super(value);
7573 }
7674
--- a/jp/bitmeister/asn1/type/ASN1TagValue.java
+++ b/jp/bitmeister/asn1/type/ASN1TagValue.java
@@ -143,7 +143,8 @@ public class ASN1TagValue {
143143 private void setTagMode(ASN1TagMode tagMode, ASN1TagDefault tagDefault,
144144 Class<? extends ASN1Type> type, String fieldName) {
145145 if (tagMode == ASN1TagMode.DEFAULT) {
146- if (tagDefault == ASN1TagDefault.IMPLICIT_TAGS) {
146+ if (tagDefault == ASN1TagDefault.IMPLICIT_TAGS
147+ || (tagDefault == ASN1TagDefault.AUTOMATIC_TAGS && canImplicitTagging(type))) {
147148 this.tagMode = ASN1TagMode.IMPLICIT;
148149 } else {
149150 this.tagMode = ASN1TagMode.EXPLICIT;
--- /dev/null
+++ b/jp/bitmeister/asn1/type/AbstractInteger.java
@@ -0,0 +1,156 @@
1+/*
2+ * Copyright 2011-2012 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.Map;
19+import java.util.Map.Entry;
20+
21+import jp.bitmeister.asn1.annotation.ASN1Enumeration;
22+import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
23+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
24+import jp.bitmeister.asn1.type.builtin.INTEGER;
25+
26+/**
27+ * The base class for ASN.1 'INTEGER' types.
28+ *
29+ * <p>
30+ * This class provides generic interfaces and common methods for classes that
31+ * represents ASN.1 'INTEGER' type.
32+ * </p>
33+ * <p>
34+ * A sub-class of {@code AbstractInteger} can contain one or more fields
35+ * annotated as {@code @ASN1Enumeration}. If a sub-class of
36+ * {@code AbstractInteger} contains {@code @ASN1Enumeration} fields, the value
37+ * of the data must be same as one of the {@code @ASN1Enumeration} fields.
38+ * </p>
39+ *
40+ * @author WATANABE, Jun. <jwat at bitmeister.jp>
41+ *
42+ * @see ASN1Enumeration
43+ * @see INTEGER
44+ * @see BigINTEGER
45+ */
46+public abstract class AbstractInteger<T extends Number & Comparable<T>> extends
47+ PrimitiveType<T> implements ValueComparable<AbstractInteger<T>> {
48+
49+ /**
50+ * Sets the value specified by the name of number to this data.
51+ *
52+ * @param nameOfNumber
53+ * The name of number.
54+ */
55+ public void set(String nameOfNumber) {
56+ for (Entry<T, String> e : getNamedNumberMap().entrySet()) {
57+ if (e.getValue().equals(nameOfNumber)) {
58+ set(e.getKey());
59+ return;
60+ }
61+ }
62+ ASN1IllegalArgument e = new ASN1IllegalArgument();
63+ e.setMessage("The name '" + nameOfNumber
64+ + "' is not defined in this type.", null, getClass(), null,
65+ null);
66+ throw e;
67+ }
68+
69+ /*
70+ * (non-Javadoc)
71+ *
72+ * @see jp.bitmeister.asn1.type.PrimitiveType#set(null)
73+ */
74+ @Override
75+ public void set(T value) {
76+ if (isEnum()) {
77+ Map<T, String> map = getNamedNumberMap();
78+ if (!map.isEmpty() && !map.containsKey(value)) {
79+ ASN1IllegalArgument ex = new ASN1IllegalArgument();
80+ ex.setMessage(
81+ "Invalid value '"
82+ + value
83+ + "'. Value of a class that represents ASN.1 'ENUMERATED' must be same as one of enumerations.",
84+ null, getClass(), null, null);
85+ throw ex;
86+ }
87+ }
88+ super.set(value);
89+ }
90+
91+ /**
92+ * Returns an identifier related to the value of this data if the type has
93+ * {@code @ASN1Enumeration} fields.
94+ *
95+ * @return An identifier related to the value.
96+ */
97+ public String identifier() {
98+ return getNamedNumberMap().get(value());
99+ }
100+
101+ /**
102+ * Tests if the value of this data is in the range of {@code long} value.
103+ *
104+ * @return {@code true} when the value of this data is in the range of
105+ * {@code long}.
106+ */
107+ public abstract boolean isLongValue();
108+
109+ /**
110+ * Tests if the value of this data is in the range of {@code int} value.
111+ *
112+ * @return {@code true} when the value of this data is in the range of
113+ * {@code int}.
114+ */
115+ public abstract boolean isIntValue();
116+
117+ /**
118+ * Returns a {@code long} value that converted from the value of this data.
119+ *
120+ * @return A {@code long} value.
121+ */
122+ public long longValue() {
123+ return value().longValue();
124+ }
125+
126+ /**
127+ * Returns an {@code int} value that converted from the value of this data.
128+ *
129+ * @return An {@code int} value.
130+ */
131+ public int intValue() {
132+ return value().intValue();
133+ }
134+
135+ /* (non-Javadoc)
136+ * @see jp.bitmeister.asn1.type.ValueComparable#compareTo(jp.bitmeister.asn1.type.ASN1Type)
137+ */
138+ public int compareTo(AbstractInteger<T> other) {
139+ return value().compareTo(other.value());
140+ }
141+
142+ /**
143+ * Returns map of enumerations related to this type.
144+ *
145+ * @return The map of enumerations.
146+ */
147+ protected abstract Map<T, String> getNamedNumberMap();
148+
149+ /**
150+ * Tests if base type of this data is ASN.1 'ENUMERATED'.
151+ *
152+ * @return {@code true} if base type of this data is ASN.1 'ENUMERATED'.
153+ */
154+ protected abstract boolean isEnum();
155+
156+}
--- a/jp/bitmeister/asn1/type/BuiltInModule.java
+++ b/jp/bitmeister/asn1/type/BuiltInModule.java
@@ -23,8 +23,8 @@ import jp.bitmeister.asn1.annotation.ASN1Tag;
2323 import jp.bitmeister.asn1.type.builtin.ANY;
2424 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
2525 import jp.bitmeister.asn1.type.builtin.BOOLEAN;
26-import jp.bitmeister.asn1.type.builtin.ENUMERATED;
27-import jp.bitmeister.asn1.type.builtin.INTEGER;
26+import jp.bitmeister.asn1.type.builtin.BigENUMERATED;
27+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
2828 import jp.bitmeister.asn1.type.builtin.NULL;
2929 import jp.bitmeister.asn1.type.builtin.OBJECT_IDENTIFIER;
3030 import jp.bitmeister.asn1.type.builtin.OCTET_STRING;
@@ -63,7 +63,7 @@ import jp.bitmeister.asn1.type.useful.VisibleString;
6363 @ASN1ModuleTags(ASN1TagDefault.AUTOMATIC_TAGS)
6464 @ASN1DefinedTypes({
6565 BOOLEAN.class, // UNIVERSAL 1
66- INTEGER.class, // UNIVERSAL 2
66+ BigINTEGER.class, // UNIVERSAL 2
6767 BIT_STRING.class, // UNIVERSAL 3
6868 OCTET_STRING.class, // UNIVERSAL 4
6969 NULL.class, // UNIVERSAL 5
@@ -106,13 +106,13 @@ class BuiltInModule extends ASN1Module {
106106 *
107107 * @author WATANABE, Jun. <jwat at bitmeister.jp>
108108 *
109- * @see ENUMERATED
110- * @see INTEGER
109+ * @see BigENUMERATED
110+ * @see BigINTEGER
111111 */
112112 @ASN1BuiltIn
113113 @ASN1Identifier("ENUMERATED(unknown)")
114114 @ASN1Tag(value = 10, tagClass = ASN1TagClass.UNIVERSAL, tagMode = ASN1TagMode.IMPLICIT)
115- public static class UnknownEnumerated extends INTEGER {
115+ public static class UnknownEnumerated extends BigINTEGER {
116116 }
117117
118118 /**
--- /dev/null
+++ b/jp/bitmeister/asn1/type/NamedNumberMap.java
@@ -0,0 +1,132 @@
1+/*
2+ * Copyright 2011-2012 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.lang.reflect.Field;
19+import java.lang.reflect.Modifier;
20+import java.util.HashMap;
21+import java.util.Map;
22+
23+import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
24+import jp.bitmeister.asn1.annotation.ASN1Enumeration;
25+import jp.bitmeister.asn1.annotation.ASN1Identifier;
26+import jp.bitmeister.asn1.exception.ASN1IllegalDefinition;
27+import jp.bitmeister.asn1.exception.ASN1RuntimeException;
28+
29+/**
30+ * Contains values and identifiers of enumerations related to ASN.1 'INTEGER' and 'ENUMERATED' types.
31+ *
32+ * @author WATANABE, Jun. <jwat at bitmeister.jp>
33+ *
34+ * @see AbstractInteger
35+ */
36+public abstract class NamedNumberMap<T extends Number & Comparable<T>> {
37+
38+ /**
39+ * Map of maps of enumerations that related to key types.
40+ */
41+ private Map<Class<? extends AbstractInteger<T>>, Map<T, String>> namedNumberMaps = new HashMap<Class<? extends AbstractInteger<T>>, Map<T, String>>();
42+
43+ /**
44+ * Put an empty enumeration map to {@code namedNumberMaps}.
45+ *
46+ * @param type The key type.
47+ */
48+ public void putEmptyMap(Class<? extends AbstractInteger<T>> type) {
49+ namedNumberMaps.put(type, new HashMap<T, String>());
50+ }
51+
52+ /**
53+ * Returns the map of enumerations related to the type. The {@code Map} maps
54+ * value to identifier string.
55+ *
56+ * @param type
57+ * the Type.
58+ * @return The map of enumerations.
59+ */
60+ public Map<T, String> getNamedNumberMap(Class<? extends AbstractInteger<T>> type) {
61+ Map<T, String> map = namedNumberMaps.get(type);
62+ if (map != null) {
63+ return map;
64+ }
65+ map = new HashMap<T, String>();
66+ for (Field f : type.getDeclaredFields()) {
67+ if (!f.isAnnotationPresent(ASN1Enumeration.class)) {
68+ continue;
69+ }
70+ final int modifier = Modifier.PUBLIC | Modifier.STATIC
71+ | Modifier.FINAL;
72+ ASN1Identifier id = f.getAnnotation(ASN1Identifier.class);
73+ String fieldId = id != null ? id.value() : f.getName();
74+ if ((f.getModifiers() & modifier) != modifier) {
75+ ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
76+ ex.setMessage(
77+ "An enumeration must be a public static final field.",
78+ null, type, fieldId, null);
79+ throw ex;
80+ }
81+ T value = null;
82+ try {
83+ value = getKeyValue(f);
84+ }
85+ catch (ASN1RuntimeException ex) {
86+ ex.setMessage(ex.getMessage(), ex.getCause(), type, fieldId, null);
87+ throw ex;
88+ }
89+ if (map.containsKey(value)) {
90+ ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
91+ ex.setMessage(
92+ "Each number of enumerations shall be distinct from all other enumerations in the type.",
93+ null, type, fieldId, null);
94+ throw ex;
95+ }
96+ map.put(value, fieldId);
97+ }
98+ @SuppressWarnings("unchecked")
99+ Class<? extends AbstractInteger<T>> parent = (Class<? extends AbstractInteger<T>>)type.getSuperclass();
100+ if (parent.isAnnotationPresent(ASN1BuiltIn.class)) {
101+ if (!type.getSuperclass().equals(AbstractInteger.class) && map.isEmpty()) {
102+ ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
103+ ex.setMessage(
104+ "ENUMERATED type shall have at least one enumeration item.",
105+ null, type, null, null);
106+ throw ex;
107+ }
108+ }
109+ else {
110+ if (!map.isEmpty()) {
111+ ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
112+ ex.setMessage(
113+ "A class that does not extend 'INTEGER' or 'ENUMERATED' directly, can not have own enumerations.",
114+ null, type, null, null);
115+ throw ex;
116+ }
117+ map = getNamedNumberMap(parent);
118+ }
119+ namedNumberMaps.put(type, map);
120+ return map;
121+ }
122+
123+ /**
124+ * Returns key value that defined by the enumeration field.
125+ *
126+ * @param f
127+ * The enumeration field.
128+ * @return The key value.
129+ */
130+ public abstract T getKeyValue(Field f);
131+
132+}
--- a/jp/bitmeister/asn1/type/ValueComparable.java
+++ b/jp/bitmeister/asn1/type/ValueComparable.java
@@ -15,6 +15,7 @@
1515 */
1616 package jp.bitmeister.asn1.type;
1717
18+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
1819 import jp.bitmeister.asn1.type.builtin.INTEGER;
1920 import jp.bitmeister.asn1.type.builtin.REAL;
2021
@@ -22,17 +23,18 @@ import jp.bitmeister.asn1.type.builtin.REAL;
2223 * The interface for classes whose instance can be compared to other instance.
2324 *
2425 * <p>
25- * {@code INTEGER} and {@code REAL} implements this interface and their value
26- * shall be ordered by the natural ordering. {@code compareTo} method is used
27- * for compare the value to the other value.
26+ * {@code INTEGER}, {@code BigINTEGER} and {@code REAL} implements this
27+ * interface and their value shall be ordered by the natural ordering.
28+ * {@code compareTo} method is used for compare the value to the other value.
2829 * </p>
2930 *
3031 * @author WATANABE, Jun. <jwat at bitmeister.jp>
3132 *
3233 * @see INTEGER
34+ * @see BigINTEGER
3335 * @see REAL
3436 */
35-public interface ValueComparable {
37+public interface ValueComparable<T extends ASN1Type> {
3638
3739 /**
3840 * Compares the value of this instance to the value of the other instance.
@@ -42,6 +44,6 @@ public interface ValueComparable {
4244 * @return A negative integer, zero, or a positive integer as this value is
4345 * less than, equal to, or greater than the other value.
4446 */
45- public int compareTo(ASN1Type other);
47+ public int compareTo(T other);
4648
4749 }
--- /dev/null
+++ b/jp/bitmeister/asn1/type/builtin/BigENUMERATED.java
@@ -0,0 +1,98 @@
1+/*
2+ * Copyright 2011-2012 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.builtin;
17+
18+import java.math.BigInteger;
19+
20+import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
21+import jp.bitmeister.asn1.annotation.ASN1Enumeration;
22+import jp.bitmeister.asn1.annotation.ASN1Identifier;
23+import jp.bitmeister.asn1.annotation.ASN1Tag;
24+import jp.bitmeister.asn1.processor.ASN1Visitor;
25+import jp.bitmeister.asn1.type.ASN1TagClass;
26+import jp.bitmeister.asn1.type.ASN1TagMode;
27+
28+/**
29+ * An implementation of ASN.1 'ENUMERATED' type that can contain
30+ * arbitrary-precision integer value.
31+ *
32+ * <p>
33+ * This is the base class used for defining 'ENUMERATED' types which need to
34+ * contain an arbitrary-precision value. A sub-class of {@code BigENUMERATED}
35+ * must contain one or more fields annotated as {@code @ASN1Enumeration}.
36+ * </p>
37+ *
38+ * @author WATANABE, Jun. <jwat at bitmeister.jp>
39+ *
40+ * @see ASN1Enumeration
41+ * @see ENUMERATED
42+ */
43+@ASN1BuiltIn
44+@ASN1Identifier("ENUMERATED")
45+@ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 10, tagMode = ASN1TagMode.IMPLICIT)
46+public abstract class BigENUMERATED extends BigINTEGER {
47+
48+ /**
49+ * Instantiates an empty {@code BigENUMERATED}.
50+ */
51+ public BigENUMERATED() {
52+ }
53+
54+ /**
55+ * Instantiates an {@code BigENUMERATED} and initialize it with the
56+ * {@code BigInteger} value.
57+ *
58+ * @param value
59+ * The value to be assigned.
60+ */
61+ public BigENUMERATED(BigInteger value) {
62+ super(value);
63+ }
64+
65+ /**
66+ * Instantiates an {@code BigENUMERATED} and initialize it with the {@code long}
67+ * value.
68+ *
69+ * @param value
70+ * The value to be assigned.
71+ */
72+ public BigENUMERATED(long value) {
73+ super(value);
74+ }
75+
76+ /*
77+ * (non-Javadoc)
78+ *
79+ * @see
80+ * jp.bitmeister.asn1.type.builtin.INTEGER#accept(jp.bitmeister.asn1.processor
81+ * .ASN1Visitor)
82+ */
83+ @Override
84+ public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
85+ return visitor.visit(this);
86+ }
87+
88+ /*
89+ * (non-Javadoc)
90+ *
91+ * @see jp.bitmeister.asn1.type.AbstractInteger#isEnum()
92+ */
93+ @Override
94+ protected boolean isEnum() {
95+ return true;
96+ }
97+
98+}
--- /dev/null
+++ b/jp/bitmeister/asn1/type/builtin/BigINTEGER.java
@@ -0,0 +1,185 @@
1+/*
2+ * Copyright 2011-2012 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.builtin;
17+
18+import java.lang.reflect.Field;
19+import java.math.BigInteger;
20+import java.util.Map;
21+
22+import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
23+import jp.bitmeister.asn1.annotation.ASN1Identifier;
24+import jp.bitmeister.asn1.annotation.ASN1Tag;
25+import jp.bitmeister.asn1.exception.ASN1IllegalDefinition;
26+import jp.bitmeister.asn1.exception.ASN1RuntimeException;
27+import jp.bitmeister.asn1.processor.ASN1Visitor;
28+import jp.bitmeister.asn1.type.ASN1TagClass;
29+import jp.bitmeister.asn1.type.ASN1TagMode;
30+import jp.bitmeister.asn1.type.AbstractInteger;
31+import jp.bitmeister.asn1.type.NamedNumberMap;
32+
33+/**
34+ * An implementation of ASN.1 'INTEGER' type that can contain
35+ * arbitrary-precision integer value.
36+ *
37+ * <p>
38+ * An instance of this class represents an 'INTEGER' type data that needs to
39+ * contain an arbitrary-precision value. It has a {@link java.math.BigInteger}
40+ * value.
41+ * </p>
42+ *
43+ * @author WATANABE, Jun. <jwat at bitmeister.jp>
44+ *
45+ * @see INTEGER
46+ */
47+@ASN1BuiltIn
48+@ASN1Identifier("INTEGER")
49+@ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 2, tagMode = ASN1TagMode.IMPLICIT)
50+public class BigINTEGER extends AbstractInteger<BigInteger> {
51+
52+ /**
53+ * Contains the maps of enumerations for all of the sub-types of {@code BigINTEGER}
54+ * and {@code BigENUMERATED}.
55+ */
56+ private static final NamedNumberMap<BigInteger> NAMED_NUMBER_MAPS = new NamedNumberMap<BigInteger>() {
57+
58+ /* (non-Javadoc)
59+ * @see jp.bitmeister.asn1.type.NamedNumberMap#getKeyValue(java.lang.reflect.Field)
60+ */
61+ @Override
62+ public BigInteger getKeyValue(Field f) {
63+ BigInteger value;
64+ if (f.getType() == BigInteger.class) {
65+ try {
66+ value = (BigInteger) f.get(null);
67+ } catch (Exception e) {
68+ ASN1RuntimeException ex = new ASN1RuntimeException(
69+ "Failed to retreave the BigInteger value.");
70+ throw ex;
71+ }
72+ } else if (f.getType() == int.class || f.getType() == long.class) {
73+ try {
74+ value = BigInteger.valueOf(f.getLong(null));
75+ } catch (Exception e) {
76+ ASN1RuntimeException ex = new ASN1RuntimeException(
77+ "Failed to convert the value to BigInteger.");
78+ throw ex;
79+ }
80+ } else {
81+ ASN1IllegalDefinition ex = new ASN1IllegalDefinition(
82+ "An enumeration of BigINTEGER type must be an 'int', a 'long' or a 'BigInteger' field.");
83+ throw ex;
84+ }
85+ return value;
86+ }
87+
88+ };
89+
90+ static {
91+ NAMED_NUMBER_MAPS.putEmptyMap(BigINTEGER.class);
92+ }
93+
94+ /**
95+ * Instantiates an empty {@code INTEGER}.
96+ */
97+ public BigINTEGER() {
98+ }
99+
100+ /**
101+ * Instantiates an {@code INTEGER} and initialize it with the
102+ * {@code BigInteger} value.
103+ *
104+ * @param value
105+ * The value to be assigned.
106+ */
107+ public BigINTEGER(BigInteger value) {
108+ set(value);
109+ }
110+
111+ /**
112+ * Instantiates an {@code INTEGER} and initialize it with the {@code long}
113+ * value.
114+ *
115+ * @param value
116+ * The value to be assigned.
117+ */
118+ public BigINTEGER(long value) {
119+ set(value);
120+ }
121+
122+ /**
123+ * Sets the {@code long} value to this data.
124+ *
125+ * @param value
126+ * The value to be assigned.
127+ */
128+ public void set(long value) {
129+ set(BigInteger.valueOf(value));
130+ }
131+
132+ /**
133+ * Tests if the value of this data is in the range of {@code long} value.
134+ *
135+ * @return {@code true} when the value of this data is in the range of
136+ * {@code long}.
137+ */
138+ public boolean isLongValue() {
139+ return value().compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0
140+ && value().compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0;
141+ }
142+
143+ /**
144+ * Tests if the value of this data is in the range of {@code int} value.
145+ *
146+ * @return {@code true} when the value of this data is in the range of
147+ * {@code int}.
148+ */
149+ public boolean isIntValue() {
150+ return value().compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0
151+ && value().compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0;
152+ }
153+
154+
155+ /* (non-Javadoc)
156+ * @see jp.bitmeister.asn1.type.AbstractInteger#getNamedNumberMap()
157+ */
158+ @Override
159+ public Map<BigInteger, String> getNamedNumberMap() {
160+ return NAMED_NUMBER_MAPS.getNamedNumberMap(getClass());
161+ }
162+
163+ /*
164+ * (non-Javadoc)
165+ *
166+ * @see
167+ * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
168+ * ASN1Visitor)
169+ */
170+ @Override
171+ public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
172+ return visitor.visit(this);
173+ }
174+
175+ /*
176+ * (non-Javadoc)
177+ *
178+ * @see jp.bitmeister.asn1.type.AbstractInteger#isEnum()
179+ */
180+ @Override
181+ protected boolean isEnum() {
182+ return false;
183+ }
184+
185+}
--- a/jp/bitmeister/asn1/type/builtin/CHOICE.java
+++ b/jp/bitmeister/asn1/type/builtin/CHOICE.java
@@ -49,8 +49,7 @@ public abstract class CHOICE extends SelectiveType {
4949
5050 /**
5151 * Instantiates a {@code CHOICE} and initialize it with the parameter. The
52- * ASN.1 tag that assigned to the type of the data is used for select an
53- * alternative.
52+ * ASN.1 type of the data is used for select an alternative.
5453 *
5554 * @param data
5655 * The data to be assigned.
--- a/jp/bitmeister/asn1/type/builtin/ENUMERATED.java
+++ b/jp/bitmeister/asn1/type/builtin/ENUMERATED.java
@@ -15,8 +15,6 @@
1515 */
1616 package jp.bitmeister.asn1.type.builtin;
1717
18-import java.math.BigInteger;
19-
2018 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
2119 import jp.bitmeister.asn1.annotation.ASN1Enumeration;
2220 import jp.bitmeister.asn1.annotation.ASN1Tag;
@@ -25,17 +23,21 @@ import jp.bitmeister.asn1.type.ASN1TagClass;
2523 import jp.bitmeister.asn1.type.ASN1TagMode;
2624
2725 /**
28- * Represents ASN.1 'ENUMERATED' type.
26+ * An implementation of ASN.1 'ENUMERATED' type that contains {@code Long}
27+ * value.
2928 *
3029 * <p>
31- * This is the base class used for defining 'ENUMERATED' types. A sub-class of
32- * {@code ENUMERATED} must contain one or more fields annotated as
33- * {@code @ASN1Enumeration}.
30+ * This is the base class used for defining 'ENUMERATED' types, and has a
31+ * {@link java.lang.Long} value. If the data needs to contain value which is
32+ * bigger than {@code Long.MAX_VALUE} or smaller than {@code Long.MIN_VALUE},
33+ * {@code BigENUMERATED} type shall be used. A sub-class of {@code ENUMERATED}
34+ * must contain one or more fields annotated as {@code @ASN1Enumeration}.
3435 * </p>
3536 *
3637 * @author WATANABE, Jun. <jwat at bitmeister.jp>
3738 *
3839 * @see ASN1Enumeration
40+ * @see BigENUMERATED
3941 */
4042 @ASN1BuiltIn
4143 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 10, tagMode = ASN1TagMode.IMPLICIT)
@@ -48,17 +50,6 @@ public abstract class ENUMERATED extends INTEGER {
4850 }
4951
5052 /**
51- * Instantiates an {@code ENUMERATED} and initialize it with the
52- * {@code BigInteger} value.
53- *
54- * @param value
55- * The value to be assigned.
56- */
57- public ENUMERATED(BigInteger value) {
58- super(value);
59- }
60-
61- /**
6253 * Instantiates an {@code ENUMERATED} and initialize it with the {@code long}
6354 * value.
6455 *
@@ -81,4 +72,14 @@ public abstract class ENUMERATED extends INTEGER {
8172 return visitor.visit(this);
8273 }
8374
75+ /*
76+ * (non-Javadoc)
77+ *
78+ * @see jp.bitmeister.asn1.type.AbstractInteger#isEnum()
79+ */
80+ @Override
81+ protected boolean isEnum() {
82+ return true;
83+ }
84+
8485 }
--- a/jp/bitmeister/asn1/type/builtin/INTEGER.java
+++ b/jp/bitmeister/asn1/type/builtin/INTEGER.java
@@ -1,159 +1,65 @@
1-/*
2- * Copyright 2011-2012 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- */
161 package jp.bitmeister.asn1.type.builtin;
172
183 import java.lang.reflect.Field;
19-import java.lang.reflect.Modifier;
20-import java.math.BigInteger;
21-import java.util.HashMap;
224 import java.util.Map;
23-import java.util.Map.Entry;
245
256 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
26-import jp.bitmeister.asn1.annotation.ASN1Enumeration;
27-import jp.bitmeister.asn1.annotation.ASN1Identifier;
287 import jp.bitmeister.asn1.annotation.ASN1Tag;
29-import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
308 import jp.bitmeister.asn1.exception.ASN1IllegalDefinition;
319 import jp.bitmeister.asn1.exception.ASN1RuntimeException;
3210 import jp.bitmeister.asn1.processor.ASN1Visitor;
3311 import jp.bitmeister.asn1.type.ASN1TagClass;
3412 import jp.bitmeister.asn1.type.ASN1TagMode;
35-import jp.bitmeister.asn1.type.ASN1Type;
36-import jp.bitmeister.asn1.type.PrimitiveType;
37-import jp.bitmeister.asn1.type.ValueComparable;
13+import jp.bitmeister.asn1.type.AbstractInteger;
14+import jp.bitmeister.asn1.type.NamedNumberMap;
3815
3916 /**
40- * Represents ASN.1 'INTEGER' type.
17+ * An implementation of ASN.1 'INTEGER' type that contains {@code Long} value.
4118 *
4219 * <p>
4320 * An instance of this class represents an 'INTEGER' type data, and has a
44- * {@link java.math.BigInteger} value.
45- * </p>
46- * <p>
47- * A sub-class of {@code INTEGER} can contain one or more fields annotated as
48- * {@code @ASN1Enumeration}. If a sub-class of {@code INTEGER} contains
49- * {@code @ASN1Enumeration} fields, the value of the data must be same as one of
50- * the {@code @ASN1Enumeration} fields.
21+ * {@link java.lang.Long} value. If the data needs to contain value which is
22+ * bigger than {@code Long.MAX_VALUE} or smaller than {@code Long.MIN_VALUE},
23+ * {@code BigINTEGER} type shall be used.
5124 * </p>
5225 *
5326 * @author WATANABE, Jun. <jwat at bitmeister.jp>
5427 *
55- * @see ASN1Enumeration
28+ * @see BigINTEGER
5629 */
5730 @ASN1BuiltIn
5831 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 2, tagMode = ASN1TagMode.IMPLICIT)
59-public class INTEGER extends PrimitiveType<BigInteger> implements
60- ValueComparable {
32+public class INTEGER extends AbstractInteger<Long> {
6133
6234 /**
63- * Contains the maps of enumerations for all of the sub-types of 'INTEGER'
64- * and 'ENUMERATED'.
35+ * Contains the maps of enumerations for all of the sub-types of {@code INTEGER}
36+ * and {@code ENUMERATED}.
6537 */
66- private static final Map<Class<? extends INTEGER>, Map<BigInteger, String>> NAMED_NUMBER_MAPS = new HashMap<Class<? extends INTEGER>, Map<BigInteger, String>>();
67-
68- static {
69- NAMED_NUMBER_MAPS.put(INTEGER.class, new HashMap<BigInteger, String>());
70- }
38+ private static final NamedNumberMap<Long> NAMED_NUMBER_MAPS = new NamedNumberMap<Long>() {
7139
72- /**
73- * Returns the map of enumerations for the type. The {@code Map} maps value
74- * to identifier.
75- *
76- * @param type
77- * The type.
78- * @return The map of enumerations.
79- */
80- private static Map<BigInteger, String> getNamedNumberMap(
81- Class<? extends INTEGER> type) {
82- if (NAMED_NUMBER_MAPS.containsKey(type)) {
83- return NAMED_NUMBER_MAPS.get(type);
84- }
85- Map<BigInteger, String> map = new HashMap<BigInteger, String>();
86- for (Field f : type.getDeclaredFields()) {
87- if (!f.isAnnotationPresent(ASN1Enumeration.class)) {
88- continue;
89- }
90- final int modifier = Modifier.PUBLIC | Modifier.STATIC
91- | Modifier.FINAL;
92- ASN1Identifier id = f.getAnnotation(ASN1Identifier.class);
93- String fieldId = id != null ? id.value() : f.getName();
94- if ((f.getModifiers() & modifier) != modifier) {
95- ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
96- ex.setMessage(
97- "An enumeration must be a public static final field.",
98- null, type, fieldId, null);
99- throw ex;
100- }
101- BigInteger value;
102- if (f.getType() == BigInteger.class) {
103- try {
104- value = (BigInteger) f.get(null);
105- } catch (Exception e) {
106- ASN1RuntimeException ex = new ASN1RuntimeException();
107- ex.setMessage("Failed to retreave the BigInteger value.",
108- e, type, fieldId, null);
109- throw ex;
110- }
111- } else if (f.getType() == int.class || f.getType() == long.class) {
40+ @Override
41+ public Long getKeyValue(Field f) {
42+ Long value;
43+ if (f.getType() == int.class || f.getType() == long.class) {
11244 try {
113- value = BigInteger.valueOf(f.getLong(null));
45+ value = f.getLong(null);
11446 } catch (Exception e) {
115- ASN1RuntimeException ex = new ASN1RuntimeException();
116- ex.setMessage("Failed to convert the value to BigInteger.",
117- e, type, fieldId, null);
47+ ASN1RuntimeException ex = new ASN1RuntimeException(
48+ "Failed to retreave the Long value.");
11849 throw ex;
11950 }
12051 } else {
121- ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
122- ex.setMessage(
123- "An enumeration must be an 'int', 'long' or 'BigInteger' field.",
124- null, type, fieldId, null);
125- throw ex;
126- }
127- if (map.containsKey(value)) {
128- ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
129- ex.setMessage(
130- "Each number of enumerations shall be distinct from all other enumerations in the type.",
131- null, type, fieldId, null);
132- throw ex;
133- }
134- map.put(value, fieldId);
135- }
136- Class<? extends INTEGER> parent = type.getSuperclass().asSubclass(INTEGER.class);
137- if (parent == ENUMERATED.class) {
138- if (map.isEmpty()) {
139- ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
140- ex.setMessage(
141- "ENUMERATED type shall have at least one enumeration item.",
142- null, type, null, null);
52+ ASN1IllegalDefinition ex = new ASN1IllegalDefinition(
53+ "An enumeration of INTEGER type must be an 'int' or a 'long' field.");
14354 throw ex;
14455 }
145- } else if (parent != INTEGER.class) {
146- if (!map.isEmpty()) {
147- ASN1IllegalDefinition ex = new ASN1IllegalDefinition();
148- ex.setMessage(
149- "A class that does not extend 'INTEGER' or 'ENUMERATED' directly, can not have own enumerations.",
150- null, type, null, null);
151- throw ex;
152- }
153- map = getNamedNumberMap(parent);
56+ return value;
15457 }
155- NAMED_NUMBER_MAPS.put(type, map);
156- return map;
58+
59+ };
60+
61+ static {
62+ NAMED_NUMBER_MAPS.putEmptyMap(INTEGER.class);
15763 }
15864
15965 /**
@@ -163,17 +69,6 @@ public class INTEGER extends PrimitiveType<BigInteger> implements
16369 }
16470
16571 /**
166- * Instantiates an {@code INTEGER} and initialize it with the
167- * {@code BigInteger} value.
168- *
169- * @param value
170- * The value to be assigned.
171- */
172- public INTEGER(BigInteger value) {
173- set(value);
174- }
175-
176- /**
17772 * Instantiates an {@code INTEGER} and initialize it with the {@code long}
17873 * value.
17974 *
@@ -184,125 +79,47 @@ public class INTEGER extends PrimitiveType<BigInteger> implements
18479 set(value);
18580 }
18681
187- /*
188- * (non-Javadoc)
189- *
190- * @see jp.bitmeister.asn1.type.PrimitiveType#set(null)
82+ /* (non-Javadoc)
83+ * @see jp.bitmeister.asn1.type.AbstractInteger#getNamedNumberMap()
19184 */
19285 @Override
193- public void set(BigInteger value) {
194- Map<BigInteger, String> map = getNamedNumberMap(getClass());
195- if (!map.isEmpty() && !map.containsKey(value)) {
196- ASN1IllegalArgument ex = new ASN1IllegalArgument();
197- ex.setMessage(
198- "Invalid value '"
199- + value
200- + "'. If a class has enumerations, the value must be same as one of them.",
201- null, getClass(), null, null);
202- throw ex;
203- }
204- super.set(value);
205- }
206-
207- /**
208- * Sets the {@code long} value to this data.
209- *
210- * @param value
211- * The value to be assigned.
212- */
213- public void set(long value) {
214- set(BigInteger.valueOf(value));
215- }
216-
217- /**
218- * Sets the value specified by the name of number to this data.
219- *
220- * @param nameOfNumber
221- * The name of number.
222- */
223- public void set(String nameOfNumber) {
224- for (Entry<BigInteger, String> e: getNamedNumberMap(getClass()).entrySet()) {
225- if (e.getValue().equals(nameOfNumber)) {
226- set(e.getKey());
227- return;
228- }
229- }
230- ASN1IllegalArgument e = new ASN1IllegalArgument();
231- e.setMessage("The name '" + nameOfNumber + "' is not defined in this type.", null, getClass(), null, null);
232- throw e;
86+ public Map<Long, String> getNamedNumberMap() {
87+ return NAMED_NUMBER_MAPS.getNamedNumberMap(getClass());
23388 }
23489
235- /**
236- * Returns an identifier related to the value of this data if the type has
237- * {@code @ASN1Enumeration} fields.
238- *
239- * @return An identifier related to the value.
90+ /* (non-Javadoc)
91+ * @see jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.ASN1Visitor)
24092 */
241- public String identifier() {
242- return getNamedNumberMap(getClass()).get(value());
93+ @Override
94+ public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor)
95+ throws E {
96+ return visitor.visit(this);
24397 }
24498
245- /**
246- * Tests if the value of this data is in the range of {@code long} value.
247- *
248- * @return {@code true} when the value of this data is in the range of
249- * {@code long}.
99+ /* (non-Javadoc)
100+ * @see jp.bitmeister.asn1.type.AbstractInteger#isLongValue()
250101 */
102+ @Override
251103 public boolean isLongValue() {
252- return value().compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0
253- && value().compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0;
104+ return true;
254105 }
255106
256- /**
257- * Tests if the value of this data is in the range of {@code int} value.
258- *
259- * @return {@code true} when the value of this data is in the range of
260- * {@code int}.
107+ /* (non-Javadoc)
108+ * @see jp.bitmeister.asn1.type.AbstractInteger#isIntValue()
261109 */
110+ @Override
262111 public boolean isIntValue() {
263- return value().compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0
264- && value().compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0;
265- }
266-
267- /**
268- * Returns a {@code long} value that converted from the value of this data.
269- *
270- * @return A {@code long} value.
271- */
272- public long longValue() {
273- return value().longValue();
274- }
275-
276- /**
277- * Returns an {@code int} value that converted from the value of this data.
278- *
279- * @return An {@code int} value.
280- */
281- public int intValue() {
282- return value().intValue();
283- }
284-
285- /*
286- * (non-Javadoc)
287- *
288- * @see
289- * jp.bitmeister.asn1.type.ValueComparable#compareTo(jp.bitmeister.asn1.
290- * type.ASN1Type)
291- */
292- public int compareTo(ASN1Type other) {
293- return value().compareTo(((INTEGER) other).value());
112+ return Integer.MIN_VALUE <= value() && value() <= Integer.MAX_VALUE;
294113 }
295-
114+
296115 /*
297116 * (non-Javadoc)
298117 *
299- * @see
300- * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
301- * ASN1Visitor)
118+ * @see jp.bitmeister.asn1.type.AbstractInteger#isEnum()
302119 */
303120 @Override
304- public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
305- return visitor.visit(this);
121+ protected boolean isEnum() {
122+ return false;
306123 }
307124
308125 }
--- a/jp/bitmeister/asn1/type/builtin/REAL.java
+++ b/jp/bitmeister/asn1/type/builtin/REAL.java
@@ -21,7 +21,6 @@ import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
2121 import jp.bitmeister.asn1.processor.ASN1Visitor;
2222 import jp.bitmeister.asn1.type.ASN1TagClass;
2323 import jp.bitmeister.asn1.type.ASN1TagMode;
24-import jp.bitmeister.asn1.type.ASN1Type;
2524 import jp.bitmeister.asn1.type.PrimitiveType;
2625 import jp.bitmeister.asn1.type.ValueComparable;
2726
@@ -39,7 +38,7 @@ import jp.bitmeister.asn1.type.ValueComparable;
3938 */
4039 @ASN1BuiltIn
4140 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 9, tagMode = ASN1TagMode.IMPLICIT)
42-public class REAL extends PrimitiveType<Double> implements ValueComparable {
41+public class REAL extends PrimitiveType<Double> implements ValueComparable<REAL> {
4342
4443 private boolean isBinary = false;
4544
@@ -165,8 +164,8 @@ public class REAL extends PrimitiveType<Double> implements ValueComparable {
165164 * jp.bitmeister.asn1.type.ValueComparable#compareTo(jp.bitmeister.asn1.
166165 * type.ASN1Type)
167166 */
168- public int compareTo(ASN1Type other) {
169- return value().compareTo(((REAL) other).value());
167+ public int compareTo(REAL other) {
168+ return value().compareTo(other.value());
170169 }
171170
172171 /*
--- a/jp/bitmeister/asn1/type/useful/EMBEDDED_PDV.java
+++ b/jp/bitmeister/asn1/type/useful/EMBEDDED_PDV.java
@@ -26,7 +26,7 @@ import jp.bitmeister.asn1.type.ASN1TagClass;
2626 import jp.bitmeister.asn1.type.ASN1TagMode;
2727 import jp.bitmeister.asn1.type.ASN1Type;
2828 import jp.bitmeister.asn1.type.builtin.CHOICE;
29-import jp.bitmeister.asn1.type.builtin.INTEGER;
29+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
3030 import jp.bitmeister.asn1.type.builtin.NULL;
3131 import jp.bitmeister.asn1.type.builtin.OBJECT_IDENTIFIER;
3232 import jp.bitmeister.asn1.type.builtin.OCTET_STRING;
@@ -126,7 +126,7 @@ public class EMBEDDED_PDV extends SEQUENCE {
126126 */
127127 @ASN1Element(0)
128128 @ASN1Identifier("presentation-context-id")
129- public INTEGER presentation_context_id;
129+ public BigINTEGER presentation_context_id;
130130
131131 /**
132132 * 'transfer-syntax' field.
@@ -150,7 +150,7 @@ public class EMBEDDED_PDV extends SEQUENCE {
150150 * @param transfer_syntax
151151 * 'transfer-syntax' field.
152152 */
153- public ContextNegotiation(INTEGER presentation_context_id,
153+ public ContextNegotiation(BigINTEGER presentation_context_id,
154154 OBJECT_IDENTIFIER transfer_syntax) {
155155 this.presentation_context_id = presentation_context_id;
156156 this.transfer_syntax = transfer_syntax;
@@ -183,7 +183,7 @@ public class EMBEDDED_PDV extends SEQUENCE {
183183 */
184184 @ASN1Alternative(2)
185185 @ASN1Identifier("presentation-context-id")
186- public INTEGER presentation_context_id;
186+ public BigINTEGER presentation_context_id;
187187
188188 /**
189189 * 'context-negotiation' field.
--- a/jp/bitmeister/asn1/type/useful/EXTERNAL.java
+++ b/jp/bitmeister/asn1/type/useful/EXTERNAL.java
@@ -27,7 +27,7 @@ import jp.bitmeister.asn1.type.ASN1Type;
2727 import jp.bitmeister.asn1.type.builtin.ANY;
2828 import jp.bitmeister.asn1.type.builtin.BIT_STRING;
2929 import jp.bitmeister.asn1.type.builtin.CHOICE;
30-import jp.bitmeister.asn1.type.builtin.INTEGER;
30+import jp.bitmeister.asn1.type.builtin.BigINTEGER;
3131 import jp.bitmeister.asn1.type.builtin.OBJECT_IDENTIFIER;
3232 import jp.bitmeister.asn1.type.builtin.OCTET_STRING;
3333 import jp.bitmeister.asn1.type.builtin.SEQUENCE;
@@ -71,7 +71,7 @@ public class EXTERNAL extends SEQUENCE {
7171 */
7272 @ASN1Element(value = 1, optional = true)
7373 @ASN1Identifier("indirect-reference")
74- public INTEGER indirect_reference;
74+ public BigINTEGER indirect_reference;
7575
7676 /**
7777 * 'data-value-descriptor' field.
@@ -134,7 +134,7 @@ public class EXTERNAL extends SEQUENCE {
134134 }
135135
136136 public EXTERNAL(OBJECT_IDENTIFIER direct_reference,
137- INTEGER indirect_reference, ObjectDescriptor data_value_descriptor,
137+ BigINTEGER indirect_reference, ObjectDescriptor data_value_descriptor,
138138 Encoding encoding) {
139139 this.direct_reference = direct_reference;
140140 this.indirect_reference = indirect_reference;
Show on old repository browser