From 425bd35c736b7e1c0dbe5b3f9162e195b3be6018 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Thu, 25 Feb 2021 11:02:26 +0100 Subject: - bug fix on TicketType Enum - duplicate function getDocument removed - issuedOnLine null value allowed - maven install added (required renaming of some packages) --- src/org/uic/barcode/asn1/datatypes/Alphabet.java | 20 +++++ .../barcode/asn1/datatypes/AlphabetBuilder.java | 32 ++++++++ .../barcode/asn1/datatypes/Asn1AnonymousType.java | 15 ++++ .../uic/barcode/asn1/datatypes/Asn1BigInteger.java | 72 ++++++++++++++++ .../uic/barcode/asn1/datatypes/Asn1Default.java | 12 +++ .../uic/barcode/asn1/datatypes/Asn1Integer.java | 56 +++++++++++++ .../uic/barcode/asn1/datatypes/Asn1Optional.java | 20 +++++ .../uic/barcode/asn1/datatypes/Asn1SequenceOf.java | 70 ++++++++++++++++ src/org/uic/barcode/asn1/datatypes/Asn1String.java | 17 ++++ .../asn1/datatypes/Asn1VarSizeBitstring.java | 58 +++++++++++++ src/org/uic/barcode/asn1/datatypes/Bitstring.java | 16 ++++ .../asn1/datatypes/CharacterRestriction.java | 13 +++ src/org/uic/barcode/asn1/datatypes/Choice.java | 12 +++ .../barcode/asn1/datatypes/DefaultAlphabet.java | 8 ++ src/org/uic/barcode/asn1/datatypes/FieldOrder.java | 13 +++ src/org/uic/barcode/asn1/datatypes/FixedSize.java | 12 +++ .../barcode/asn1/datatypes/HasExtensionMarker.java | 12 +++ .../uic/barcode/asn1/datatypes/IntMinValue.java | 13 +++ src/org/uic/barcode/asn1/datatypes/IntRange.java | 14 ++++ .../uic/barcode/asn1/datatypes/IsExtension.java | 12 +++ .../uic/barcode/asn1/datatypes/NoAsn1Field.java | 10 +++ src/org/uic/barcode/asn1/datatypes/Optional.java | 96 ++++++++++++++++++++++ .../barcode/asn1/datatypes/RestrictedString.java | 13 +++ src/org/uic/barcode/asn1/datatypes/Sequence.java | 9 ++ src/org/uic/barcode/asn1/datatypes/SizeRange.java | 14 ++++ .../uic/barcode/asn1/datatypes/package-info.java | 7 ++ 26 files changed, 646 insertions(+) create mode 100644 src/org/uic/barcode/asn1/datatypes/Alphabet.java create mode 100644 src/org/uic/barcode/asn1/datatypes/AlphabetBuilder.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1AnonymousType.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1BigInteger.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1Default.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1Integer.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1Optional.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1String.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Asn1VarSizeBitstring.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Bitstring.java create mode 100644 src/org/uic/barcode/asn1/datatypes/CharacterRestriction.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Choice.java create mode 100644 src/org/uic/barcode/asn1/datatypes/DefaultAlphabet.java create mode 100644 src/org/uic/barcode/asn1/datatypes/FieldOrder.java create mode 100644 src/org/uic/barcode/asn1/datatypes/FixedSize.java create mode 100644 src/org/uic/barcode/asn1/datatypes/HasExtensionMarker.java create mode 100644 src/org/uic/barcode/asn1/datatypes/IntMinValue.java create mode 100644 src/org/uic/barcode/asn1/datatypes/IntRange.java create mode 100644 src/org/uic/barcode/asn1/datatypes/IsExtension.java create mode 100644 src/org/uic/barcode/asn1/datatypes/NoAsn1Field.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Optional.java create mode 100644 src/org/uic/barcode/asn1/datatypes/RestrictedString.java create mode 100644 src/org/uic/barcode/asn1/datatypes/Sequence.java create mode 100644 src/org/uic/barcode/asn1/datatypes/SizeRange.java create mode 100644 src/org/uic/barcode/asn1/datatypes/package-info.java (limited to 'src/org/uic/barcode/asn1/datatypes') diff --git a/src/org/uic/barcode/asn1/datatypes/Alphabet.java b/src/org/uic/barcode/asn1/datatypes/Alphabet.java new file mode 100644 index 0000000..2b153ae --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Alphabet.java @@ -0,0 +1,20 @@ +package org.uic.barcode.asn1.datatypes; + +/** + * Alphabet class for Restricted Strings. + * + * Use {@link AlphabetBuilder} for convenient construction of restriction alphabets. + */ +public abstract class Alphabet { + + private final String chars; + + protected Alphabet(String chars) { + this.chars = chars; + } + + public final String chars() { + return chars; + } + +} diff --git a/src/org/uic/barcode/asn1/datatypes/AlphabetBuilder.java b/src/org/uic/barcode/asn1/datatypes/AlphabetBuilder.java new file mode 100644 index 0000000..b768897 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/AlphabetBuilder.java @@ -0,0 +1,32 @@ +package org.uic.barcode.asn1.datatypes; + + +public class AlphabetBuilder { + + private final StringBuilder sb = new StringBuilder(); + + public AlphabetBuilder() {} + + public String chars() { + return sb.toString(); + } + + public AlphabetBuilder withRange(char from, char to) { + for (char c = from; c <= to; c++) { + sb.append(c); + } + return this; + } + + public AlphabetBuilder withChars(String str) { + sb.append(str); + return this; + } + + public AlphabetBuilder withChars(Character... chars) { + for (char c : chars) { + sb.append(c); + } + return this; + } +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1AnonymousType.java b/src/org/uic/barcode/asn1/datatypes/Asn1AnonymousType.java new file mode 100644 index 0000000..b1b0499 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1AnonymousType.java @@ -0,0 +1,15 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +/** + * This annotation indicates that the class is not present in the original ASN.1 declaration. + * This happens when SEQUENCE members have restrictions (ranges, alphabets etc). + * + * This annotation plays no role in the UPER encoding. + * + */ +@Target({ElementType.TYPE}) +public @interface Asn1AnonymousType { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1BigInteger.java b/src/org/uic/barcode/asn1/datatypes/Asn1BigInteger.java new file mode 100644 index 0000000..4adca22 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1BigInteger.java @@ -0,0 +1,72 @@ +package org.uic.barcode.asn1.datatypes; + +import java.math.BigInteger; + +//outdated: use BigInteger +public class Asn1BigInteger { + + private final BigInteger value; + + public Asn1BigInteger(final BigInteger value) { + this.value = value; + } + + @Override public String toString() { + return "" + value; + } + + public BigInteger value() { return value; } + + public Long longValue() { + return value.longValue(); + } + + public Integer intValue() { + return value.intValue(); + } + + public Asn1BigInteger(Long num) { + this.value = BigInteger.valueOf(num); + } + + public Asn1BigInteger(long num) { + this.value = BigInteger.valueOf(num); + } + + public Asn1BigInteger(Integer num) { + this.value = BigInteger.valueOf(num); + } + + public Asn1BigInteger(int num) { + this.value = BigInteger.valueOf(num); + } + + public static Long toLong(Asn1BigInteger object) { + if (object == null) return null; + return object.longValue(); + } + + public static Asn1BigInteger toAsn1(Long object) { + if (object == null) return null; + return new Asn1BigInteger(object); + } + + public static Asn1BigInteger toAsn1(Integer object) { + if (object == null) return null; + return new Asn1BigInteger(object); + } + + public Long toLong(){ + if (this.value != null) { + return this.value.longValue(); + } + return null; + } + + public BigInteger toBigInteger(){ + return value; + } + + + +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1Default.java b/src/org/uic/barcode/asn1/datatypes/Asn1Default.java new file mode 100644 index 0000000..bf5cfff --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1Default.java @@ -0,0 +1,12 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Asn1Default { + String value(); +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1Integer.java b/src/org/uic/barcode/asn1/datatypes/Asn1Integer.java new file mode 100644 index 0000000..e12f8ec --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1Integer.java @@ -0,0 +1,56 @@ +package org.uic.barcode.asn1.datatypes; + + + +//outdated: use BigInteger +public class Asn1Integer { + + public long value; + + public Asn1Integer() {} + public Asn1Integer(long value) { + this.value = value; + } + + public Long value() { return value; } + + @Override public String toString() { + return "" + value; + } + + public Long longObject () { + return new Long(value()); + } + + public Asn1Integer(Long num) { + this.value = num; + } + + + public Asn1Integer(Integer num) { + this.value = num; + } + + public Asn1Integer(int num) { + this.value = num; + } + + public static Long toLong(Asn1Integer object) { + if (object == null) return null; + return object.value(); + } + + + public static Asn1Integer toAsn1(Long object) { + if (object == null) return null; + return new Asn1Integer(object); + } + + + + + + + + +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1Optional.java b/src/org/uic/barcode/asn1/datatypes/Asn1Optional.java new file mode 100644 index 0000000..71946ec --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1Optional.java @@ -0,0 +1,20 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Indicates that the field is OPTIONAL in ASN.1. Implemented as null. Equivalent to @Nullable. + * + * Using Optional would require Manifests to capture generics (like in Gson). + * + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Asn1Optional { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java b/src/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java new file mode 100644 index 0000000..4924b50 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java @@ -0,0 +1,70 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.reflect.ParameterizedType; +import java.util.*; + +import org.uic.barcode.logger.Logger; +import org.uic.barcode.logger.LoggerFactory; + + +/** + * Class to represent ASN.1 construct "SEQUENCE OF". + *

+ * Extending classes should specify concrete types for T, generic collections can't be decoded (yet?). + *

+ * Usage example: + *

+ * 
+ * {@literal @}Sequence
+ * public class Person {
+ *     {@literal @}IntRange(minValue=0, maxValue=100, hasExtensionMarker=true)
+ *     int age;
+ *     Children children;
+ * }
+ * public class Children extends {@code Asn1SequenceOf } {
+ *     public Children() { super(); }
+ *     public Children({@code Collection} coll) { super(coll); }
+ * }
+ * 
+ * 
+ * + *

+ * Actually, UPER decoder and encoder consider anything that extends {@code List} as a SEQUENCE OF. + * + * + * @param type of elements contained. + */ +public abstract class Asn1SequenceOf extends AbstractList { + private final static Logger logger = LoggerFactory.getLogger("asnLogger"); + + private final List bakingList; + + @Override public T get(int index) { return bakingList.get(index); } + @Override public int size() { return bakingList.size(); } + @Override public boolean add (T e){ return bakingList.add(e);} + + public Asn1SequenceOf() { this(new ArrayList()); } + public Asn1SequenceOf(Collection coll) { + logger.debug(String.format("Instantiating Sequence Of %s with %s", + ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0], + coll)); + bakingList = new ArrayList<>(coll); + } + + + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Asn1SequenceOf that = (Asn1SequenceOf) o; + return Objects.equals(bakingList, that.bakingList); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), bakingList); + } +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1String.java b/src/org/uic/barcode/asn1/datatypes/Asn1String.java new file mode 100644 index 0000000..fb80b92 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1String.java @@ -0,0 +1,17 @@ +package org.uic.barcode.asn1.datatypes; + +public class Asn1String { + + private String value; + + public Asn1String() { this(""); } + + public Asn1String(String value) { + this.value = value; + } + + @Override public String toString() { return value; } + + public String value() { return value; } + +} diff --git a/src/org/uic/barcode/asn1/datatypes/Asn1VarSizeBitstring.java b/src/org/uic/barcode/asn1/datatypes/Asn1VarSizeBitstring.java new file mode 100644 index 0000000..c07f7f0 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Asn1VarSizeBitstring.java @@ -0,0 +1,58 @@ +package org.uic.barcode.asn1.datatypes; + +import java.util.AbstractList; +import java.util.BitSet; +import java.util.Collection; +import java.util.Objects; + +/** + * Convenience class for Bitstrings of variable size. + * For UPER, {@code List} works just as well. + */ +public class Asn1VarSizeBitstring extends AbstractList { + + private final BitSet backing; + + @Override public Boolean get(int index) { + return backing.get(index); + } + + @Override public int size() { + return backing.length(); + } + + public Asn1VarSizeBitstring(Collection coll) { + backing = new BitSet(); + int bitIndex = 0; + for (Boolean b : coll) { + backing.set(bitIndex, b); + bitIndex++; + } + } + + public Asn1VarSizeBitstring(BitSet bitset) { + backing = (BitSet) bitset.clone(); + } + + protected void setBit(int bitIndex, boolean value) { + backing.set(bitIndex, value); + } + + public boolean getBit(int bitIndex) { + return backing.get(bitIndex); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Asn1VarSizeBitstring booleen = (Asn1VarSizeBitstring) o; + return Objects.equals(backing, booleen.backing); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), backing); + } +} diff --git a/src/org/uic/barcode/asn1/datatypes/Bitstring.java b/src/org/uic/barcode/asn1/datatypes/Bitstring.java new file mode 100644 index 0000000..1543f64 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Bitstring.java @@ -0,0 +1,16 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** This annotation is used for bitstrings. + * In UPER, a SEQUENCE OF Booleans would look exactly as bitstring, so this annotation can be + * omitted for {@code List}. + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Bitstring { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/CharacterRestriction.java b/src/org/uic/barcode/asn1/datatypes/CharacterRestriction.java new file mode 100644 index 0000000..e74c436 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/CharacterRestriction.java @@ -0,0 +1,13 @@ +package org.uic.barcode.asn1.datatypes; + +public enum CharacterRestriction { + NumericString, + PrintableString, + VisibleString, + ISO646String, + IA5String, + BMPString, + UniversalString, + UTF8String, + ObjectIdentifier; +} diff --git a/src/org/uic/barcode/asn1/datatypes/Choice.java b/src/org/uic/barcode/asn1/datatypes/Choice.java new file mode 100644 index 0000000..01a0034 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Choice.java @@ -0,0 +1,12 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Choice { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/DefaultAlphabet.java b/src/org/uic/barcode/asn1/datatypes/DefaultAlphabet.java new file mode 100644 index 0000000..62d13f4 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/DefaultAlphabet.java @@ -0,0 +1,8 @@ +package org.uic.barcode.asn1.datatypes; + +public class DefaultAlphabet extends Alphabet { + + public DefaultAlphabet() { + super(""); + } +} diff --git a/src/org/uic/barcode/asn1/datatypes/FieldOrder.java b/src/org/uic/barcode/asn1/datatypes/FieldOrder.java new file mode 100644 index 0000000..b8c378f --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/FieldOrder.java @@ -0,0 +1,13 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface FieldOrder { + int order() default -1; +} + diff --git a/src/org/uic/barcode/asn1/datatypes/FixedSize.java b/src/org/uic/barcode/asn1/datatypes/FixedSize.java new file mode 100644 index 0000000..4c17e60 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/FixedSize.java @@ -0,0 +1,12 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface FixedSize { + int value(); +} diff --git a/src/org/uic/barcode/asn1/datatypes/HasExtensionMarker.java b/src/org/uic/barcode/asn1/datatypes/HasExtensionMarker.java new file mode 100644 index 0000000..b8945fc --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/HasExtensionMarker.java @@ -0,0 +1,12 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface HasExtensionMarker { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/IntMinValue.java b/src/org/uic/barcode/asn1/datatypes/IntMinValue.java new file mode 100644 index 0000000..e045287 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/IntMinValue.java @@ -0,0 +1,13 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IntMinValue { + long minValue(); + boolean hasExtensionMarker() default false; +} diff --git a/src/org/uic/barcode/asn1/datatypes/IntRange.java b/src/org/uic/barcode/asn1/datatypes/IntRange.java new file mode 100644 index 0000000..08fc1fb --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/IntRange.java @@ -0,0 +1,14 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IntRange { + long minValue(); + long maxValue(); + boolean hasExtensionMarker() default false; +} diff --git a/src/org/uic/barcode/asn1/datatypes/IsExtension.java b/src/org/uic/barcode/asn1/datatypes/IsExtension.java new file mode 100644 index 0000000..8aacd32 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/IsExtension.java @@ -0,0 +1,12 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IsExtension { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/NoAsn1Field.java b/src/org/uic/barcode/asn1/datatypes/NoAsn1Field.java new file mode 100644 index 0000000..0fb7d2c --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/NoAsn1Field.java @@ -0,0 +1,10 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface NoAsn1Field {} \ No newline at end of file diff --git a/src/org/uic/barcode/asn1/datatypes/Optional.java b/src/org/uic/barcode/asn1/datatypes/Optional.java new file mode 100644 index 0000000..757ba29 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Optional.java @@ -0,0 +1,96 @@ +package org.uic.barcode.asn1.datatypes; + +import java.util.NoSuchElementException; +import java.util.Objects; + +/** Represents optional values. + * + * Should be replaced by java.util.Optional from Java 8, when project moves to Java 8. + * + * @param type of contained elements */ +public class Optional { + + private final T element; + private final boolean isPresent; + + private Optional(T element, boolean isPresent) { + this.element = element; + this.isPresent = isPresent; + } + + /** @return true if the Option contains a value */ + public boolean isPresent() { + return isPresent; + } + + /** @return the element if the option is not empty + * @throws java.util.NoSuchElementException if the option is empty */ + public T get() { + if (isPresent) { + return element; + } else { + throw new NoSuchElementException("None.get"); + } + } + + /** @return the value, if present, otherwise return {@code other} + * @param other the value to be returned if there is no value present */ + public T orElse(T other) { + return isPresent() ? get() : other; + } + + /** + * Indicates whether some other object is "equal to" this Optional. The + * other object is considered equal if: + *

+ * + * @param obj an object to be tested for equality + * @return {code true} if the other object is "equal to" this object + * otherwise {@code false} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (!(obj instanceof Optional)) { + return false; + } + + Optional other = (Optional) obj; + return Objects.equals(element, other.element); + } + + /** + * Returns the hash code value of the present value, if any, or 0 (zero) if + * no value is present. + * + * @return hash code value of the present value or 0 if no value is present + */ + @Override + public int hashCode() { + return Objects.hashCode(element); + } + + /** Returns an Option containing the value. + * + * @param the type of the value + * @param element contained value + * @return a new Option that contains the value */ + public static Optional of(final A element) { + return new Optional(element, true); + } + + /** Returns an empty option. + * + * @param + * @return an empty Option */ + public static Optional empty() { + return new Optional(null, false); + } +} diff --git a/src/org/uic/barcode/asn1/datatypes/RestrictedString.java b/src/org/uic/barcode/asn1/datatypes/RestrictedString.java new file mode 100644 index 0000000..7539aed --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/RestrictedString.java @@ -0,0 +1,13 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface RestrictedString { + CharacterRestriction value(); + Class alphabet() default DefaultAlphabet.class; +} diff --git a/src/org/uic/barcode/asn1/datatypes/Sequence.java b/src/org/uic/barcode/asn1/datatypes/Sequence.java new file mode 100644 index 0000000..31163d7 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/Sequence.java @@ -0,0 +1,9 @@ +package org.uic.barcode.asn1.datatypes; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +//@Target({ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Sequence { + +} diff --git a/src/org/uic/barcode/asn1/datatypes/SizeRange.java b/src/org/uic/barcode/asn1/datatypes/SizeRange.java new file mode 100644 index 0000000..7005d47 --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/SizeRange.java @@ -0,0 +1,14 @@ +package org.uic.barcode.asn1.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface SizeRange { + int minValue(); + int maxValue(); + boolean hasExtensionMarker() default false; +} diff --git a/src/org/uic/barcode/asn1/datatypes/package-info.java b/src/org/uic/barcode/asn1/datatypes/package-info.java new file mode 100644 index 0000000..aaa134e --- /dev/null +++ b/src/org/uic/barcode/asn1/datatypes/package-info.java @@ -0,0 +1,7 @@ +/** Annotations to create Java classes that correspond to ASN.1 specifications. + * + * Some annotations (e.g. {@link SizeRange}, {@link FixedSize}, {@link IntRange},{@link IntMaxValue} + * {@link RestrictedString}) are Type-only annotations and sometime require creating extra classes, + * they can be extended to work as Field annotations too, but this will require modifications to the + * Encoder. */ +package org.uic.barcode.asn1.datatypes; -- cgit v1.2.3