/**
 * This class file was automatically generated by jASN1 v1.6.0 (http://www.openmuc.org)
 */

package org.openmuc.openiec61850.internal.mms.asn1;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.openmuc.jasn1.ber.BerByteArrayOutputStream;
import org.openmuc.jasn1.ber.BerIdentifier;
import org.openmuc.jasn1.ber.BerLength;
import org.openmuc.jasn1.ber.types.BerBoolean;
import org.openmuc.jasn1.ber.types.BerInteger;
import org.openmuc.jasn1.ber.types.BerNull;

public class TypeDescription {

    public byte[] code = null;

    public static class Array {

        public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                BerIdentifier.CONSTRUCTED, 16);
        protected BerIdentifier id;

        public byte[] code = null;
        private BerBoolean packed = null;

        private Unsigned32 numberOfElements = null;

        private TypeSpecification elementType = null;

        public Array() {
            id = identifier;
        }

        public Array(byte[] code) {
            id = identifier;
            this.code = code;
        }

        public void setPacked(BerBoolean packed) {
            this.packed = packed;
        }

        public BerBoolean getPacked() {
            return packed;
        }

        public void setNumberOfElements(Unsigned32 numberOfElements) {
            this.numberOfElements = numberOfElements;
        }

        public Unsigned32 getNumberOfElements() {
            return numberOfElements;
        }

        public void setElementType(TypeSpecification elementType) {
            this.elementType = elementType;
        }

        public TypeSpecification getElementType() {
            return elementType;
        }

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {

            int codeLength;

            if (code != null) {
                codeLength = code.length;
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
            }
            else {
                codeLength = 0;
                int sublength;

                sublength = elementType.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
                os.write(0xa2);
                codeLength += 1;

                codeLength += numberOfElements.encode(os, false);
                // write tag: CONTEXT_CLASS, PRIMITIVE, 1
                os.write(0x81);
                codeLength += 1;

                if (packed != null) {
                    codeLength += packed.encode(os, false);
                    // write tag: CONTEXT_CLASS, PRIMITIVE, 0
                    os.write(0x80);
                    codeLength += 1;
                }

                codeLength += BerLength.encodeLength(os, codeLength);
            }

            if (explicit) {
                codeLength += id.encode(os);
            }

            return codeLength;

        }

        public int decode(InputStream is, boolean explicit) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerIdentifier berIdentifier = new BerIdentifier();

            if (explicit) {
                codeLength += id.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);

            int totalLength = length.val;
            codeLength += totalLength;

            subCodeLength += berIdentifier.decode(is);
            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 0)) {
                packed = new BerBoolean();
                subCodeLength += packed.decode(is, false);
                subCodeLength += berIdentifier.decode(is);
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
                numberOfElements = new Unsigned32();
                subCodeLength += numberOfElements.decode(is, false);
                subCodeLength += berIdentifier.decode(is);
            }
            else {
                throw new IOException("Identifier does not match the mandatory sequence element identifer.");
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
                subCodeLength += length.decode(is);
                elementType = new TypeSpecification();
                subCodeLength += elementType.decode(is, null);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
            }
            throw new IOException("Unexpected end of sequence, length tag: " + totalLength
                    + ", actual sequence length: " + subCodeLength);

        }

        public void encodeAndSave(int encodingSizeGuess) throws IOException {
            BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
            encode(os, false);
            code = os.getArray();
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("SEQUENCE{");
            boolean firstSelectedElement = true;
            if (packed != null) {
                sb.append("packed: ").append(packed);
                firstSelectedElement = false;
            }

            if (!firstSelectedElement) {
                sb.append(", ");
            }
            sb.append("numberOfElements: ").append(numberOfElements);

            sb.append(", ");
            sb.append("elementType: ").append(elementType);

            sb.append("}");
            return sb.toString();
        }

    }

    public static class Structure {

        public static class Components {

            public static class SEQUENCE {

                public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                        BerIdentifier.CONSTRUCTED, 16);
                protected BerIdentifier id;

                public byte[] code = null;
                private Identifier componentName = null;

                private TypeSpecification componentType = null;

                public SEQUENCE() {
                    id = identifier;
                }

                public SEQUENCE(byte[] code) {
                    id = identifier;
                    this.code = code;
                }

                public void setComponentName(Identifier componentName) {
                    this.componentName = componentName;
                }

                public Identifier getComponentName() {
                    return componentName;
                }

                public void setComponentType(TypeSpecification componentType) {
                    this.componentType = componentType;
                }

                public TypeSpecification getComponentType() {
                    return componentType;
                }

                public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {

                    int codeLength;

                    if (code != null) {
                        codeLength = code.length;
                        for (int i = code.length - 1; i >= 0; i--) {
                            os.write(code[i]);
                        }
                    }
                    else {
                        codeLength = 0;
                        int sublength;

                        sublength = componentType.encode(os, true);
                        codeLength += sublength;
                        codeLength += BerLength.encodeLength(os, sublength);
                        // write tag: CONTEXT_CLASS, CONSTRUCTED, 1
                        os.write(0xa1);
                        codeLength += 1;

                        if (componentName != null) {
                            codeLength += componentName.encode(os, false);
                            // write tag: CONTEXT_CLASS, PRIMITIVE, 0
                            os.write(0x80);
                            codeLength += 1;
                        }

                        codeLength += BerLength.encodeLength(os, codeLength);
                    }

                    if (explicit) {
                        codeLength += id.encode(os);
                    }

                    return codeLength;

                }

                public int decode(InputStream is, boolean explicit) throws IOException {
                    int codeLength = 0;
                    int subCodeLength = 0;
                    BerIdentifier berIdentifier = new BerIdentifier();

                    if (explicit) {
                        codeLength += id.decodeAndCheck(is);
                    }

                    BerLength length = new BerLength();
                    codeLength += length.decode(is);

                    int totalLength = length.val;
                    codeLength += totalLength;

                    subCodeLength += berIdentifier.decode(is);
                    if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 0)) {
                        componentName = new Identifier();
                        subCodeLength += componentName.decode(is, false);
                        subCodeLength += berIdentifier.decode(is);
                    }

                    if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
                        subCodeLength += length.decode(is);
                        componentType = new TypeSpecification();
                        subCodeLength += componentType.decode(is, null);
                        if (subCodeLength == totalLength) {
                            return codeLength;
                        }
                    }
                    throw new IOException("Unexpected end of sequence, length tag: " + totalLength
                            + ", actual sequence length: " + subCodeLength);

                }

                public void encodeAndSave(int encodingSizeGuess) throws IOException {
                    BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
                    encode(os, false);
                    code = os.getArray();
                }

                @Override
                public String toString() {
                    StringBuilder sb = new StringBuilder("SEQUENCE{");
                    boolean firstSelectedElement = true;
                    if (componentName != null) {
                        sb.append("componentName: ").append(componentName);
                        firstSelectedElement = false;
                    }

                    if (!firstSelectedElement) {
                        sb.append(", ");
                    }
                    sb.append("componentType: ").append(componentType);

                    sb.append("}");
                    return sb.toString();
                }

            }

            public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                    BerIdentifier.CONSTRUCTED, 16);
            protected BerIdentifier id;

            public byte[] code = null;
            private List<SEQUENCE> seqOf = null;

            public Components() {
                id = identifier;
                seqOf = new ArrayList<>();
            }

            public Components(byte[] code) {
                id = identifier;
                this.code = code;
            }

            public List<SEQUENCE> getSEQUENCE() {
                if (seqOf == null) {
                    seqOf = new ArrayList<>();
                }
                return seqOf;
            }

            public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
                int codeLength;

                if (code != null) {
                    codeLength = code.length;
                    for (int i = code.length - 1; i >= 0; i--) {
                        os.write(code[i]);
                    }
                }
                else {
                    codeLength = 0;
                    for (int i = (seqOf.size() - 1); i >= 0; i--) {
                        codeLength += seqOf.get(i).encode(os, true);
                    }

                    codeLength += BerLength.encodeLength(os, codeLength);

                }

                if (explicit) {
                    codeLength += id.encode(os);
                }

                return codeLength;
            }

            public int decode(InputStream is, boolean explicit) throws IOException {
                int codeLength = 0;
                int subCodeLength = 0;
                BerIdentifier berIdentifier = new BerIdentifier();
                if (explicit) {
                    codeLength += id.decodeAndCheck(is);
                }

                BerLength length = new BerLength();
                codeLength += length.decode(is);
                int totalLength = length.val;

                while (subCodeLength < totalLength) {
                    SEQUENCE element = new SEQUENCE();
                    subCodeLength += element.decode(is, true);
                    seqOf.add(element);
                }
                if (subCodeLength != totalLength) {
                    throw new IOException("Decoded SequenceOf or SetOf has wrong length. Expected " + totalLength
                            + " but has " + subCodeLength);

                }
                codeLength += subCodeLength;

                return codeLength;
            }

            public void encodeAndSave(int encodingSizeGuess) throws IOException {
                BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
                encode(os, false);
                code = os.getArray();
            }

            @Override
            public String toString() {
                StringBuilder sb = new StringBuilder("SEQUENCE OF{");

                if (seqOf == null) {
                    sb.append("null");
                }
                else {
                    Iterator<SEQUENCE> it = seqOf.iterator();
                    if (it.hasNext()) {
                        sb.append(it.next());
                        while (it.hasNext()) {
                            sb.append(", ").append(it.next());
                        }
                    }
                }

                sb.append("}");

                return sb.toString();
            }

        }

        public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                BerIdentifier.CONSTRUCTED, 16);
        protected BerIdentifier id;

        public byte[] code = null;
        private BerBoolean packed = null;

        private Components components = null;

        public Structure() {
            id = identifier;
        }

        public Structure(byte[] code) {
            id = identifier;
            this.code = code;
        }

        public void setPacked(BerBoolean packed) {
            this.packed = packed;
        }

        public BerBoolean getPacked() {
            return packed;
        }

        public void setComponents(Components components) {
            this.components = components;
        }

        public Components getComponents() {
            return components;
        }

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {

            int codeLength;

            if (code != null) {
                codeLength = code.length;
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
            }
            else {
                codeLength = 0;
                codeLength += components.encode(os, false);
                // write tag: CONTEXT_CLASS, CONSTRUCTED, 1
                os.write(0xa1);
                codeLength += 1;

                if (packed != null) {
                    codeLength += packed.encode(os, false);
                    // write tag: CONTEXT_CLASS, PRIMITIVE, 0
                    os.write(0x80);
                    codeLength += 1;
                }

                codeLength += BerLength.encodeLength(os, codeLength);
            }

            if (explicit) {
                codeLength += id.encode(os);
            }

            return codeLength;

        }

        public int decode(InputStream is, boolean explicit) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerIdentifier berIdentifier = new BerIdentifier();

            if (explicit) {
                codeLength += id.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);

            int totalLength = length.val;
            codeLength += totalLength;

            subCodeLength += berIdentifier.decode(is);
            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 0)) {
                packed = new BerBoolean();
                subCodeLength += packed.decode(is, false);
                subCodeLength += berIdentifier.decode(is);
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
                components = new Components();
                subCodeLength += components.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
            }
            throw new IOException("Unexpected end of sequence, length tag: " + totalLength
                    + ", actual sequence length: " + subCodeLength);

        }

        public void encodeAndSave(int encodingSizeGuess) throws IOException {
            BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
            encode(os, false);
            code = os.getArray();
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("SEQUENCE{");
            boolean firstSelectedElement = true;
            if (packed != null) {
                sb.append("packed: ").append(packed);
                firstSelectedElement = false;
            }

            if (!firstSelectedElement) {
                sb.append(", ");
            }
            sb.append("components: ").append(components);

            sb.append("}");
            return sb.toString();
        }

    }

    public static class FloatingPoint {

        public static final BerIdentifier identifier = new BerIdentifier(BerIdentifier.UNIVERSAL_CLASS,
                BerIdentifier.CONSTRUCTED, 16);
        protected BerIdentifier id;

        public byte[] code = null;
        private Unsigned8 formatWidth = null;

        private Unsigned8 exponentWidth = null;

        public FloatingPoint() {
            id = identifier;
        }

        public FloatingPoint(byte[] code) {
            id = identifier;
            this.code = code;
        }

        public void setFormatWidth(Unsigned8 formatWidth) {
            this.formatWidth = formatWidth;
        }

        public Unsigned8 getFormatWidth() {
            return formatWidth;
        }

        public void setExponentWidth(Unsigned8 exponentWidth) {
            this.exponentWidth = exponentWidth;
        }

        public Unsigned8 getExponentWidth() {
            return exponentWidth;
        }

        public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {

            int codeLength;

            if (code != null) {
                codeLength = code.length;
                for (int i = code.length - 1; i >= 0; i--) {
                    os.write(code[i]);
                }
            }
            else {
                codeLength = 0;
                codeLength += exponentWidth.encode(os, true);

                codeLength += formatWidth.encode(os, true);

                codeLength += BerLength.encodeLength(os, codeLength);
            }

            if (explicit) {
                codeLength += id.encode(os);
            }

            return codeLength;

        }

        public int decode(InputStream is, boolean explicit) throws IOException {
            int codeLength = 0;
            int subCodeLength = 0;
            BerIdentifier berIdentifier = new BerIdentifier();

            if (explicit) {
                codeLength += id.decodeAndCheck(is);
            }

            BerLength length = new BerLength();
            codeLength += length.decode(is);

            int totalLength = length.val;
            codeLength += totalLength;

            subCodeLength += berIdentifier.decode(is);
            if (berIdentifier.equals(BerInteger.identifier)) {
                formatWidth = new Unsigned8();
                subCodeLength += formatWidth.decode(is, false);
                subCodeLength += berIdentifier.decode(is);
            }
            else {
                throw new IOException("Identifier does not match the mandatory sequence element identifer.");
            }

            if (berIdentifier.equals(BerInteger.identifier)) {
                exponentWidth = new Unsigned8();
                subCodeLength += exponentWidth.decode(is, false);
                if (subCodeLength == totalLength) {
                    return codeLength;
                }
            }
            throw new IOException("Unexpected end of sequence, length tag: " + totalLength
                    + ", actual sequence length: " + subCodeLength);

        }

        public void encodeAndSave(int encodingSizeGuess) throws IOException {
            BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
            encode(os, false);
            code = os.getArray();
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("SEQUENCE{");
            sb.append("formatWidth: ").append(formatWidth);

            sb.append(", ");
            sb.append("exponentWidth: ").append(exponentWidth);

            sb.append("}");
            return sb.toString();
        }

    }

    private Array array = null;

    private Structure structure = null;

    private BerNull bool = null;

    private Integer32 bitString = null;

    private Unsigned8 integer = null;

    private Unsigned8 unsigned = null;

    private FloatingPoint floatingPoint = null;

    private Integer32 octetString = null;

    private Integer32 visibleString = null;

    private BerBoolean binaryTime = null;

    private Integer32 mMSString = null;

    private BerNull utcTime = null;

    public TypeDescription() {
    }

    public TypeDescription(byte[] code) {
        this.code = code;
    }

    public void setArray(Array array) {
        this.array = array;
    }

    public Array getArray() {
        return array;
    }

    public void setStructure(Structure structure) {
        this.structure = structure;
    }

    public Structure getStructure() {
        return structure;
    }

    public void setBool(BerNull bool) {
        this.bool = bool;
    }

    public BerNull getBool() {
        return bool;
    }

    public void setBitString(Integer32 bitString) {
        this.bitString = bitString;
    }

    public Integer32 getBitString() {
        return bitString;
    }

    public void setInteger(Unsigned8 integer) {
        this.integer = integer;
    }

    public Unsigned8 getInteger() {
        return integer;
    }

    public void setUnsigned(Unsigned8 unsigned) {
        this.unsigned = unsigned;
    }

    public Unsigned8 getUnsigned() {
        return unsigned;
    }

    public void setFloatingPoint(FloatingPoint floatingPoint) {
        this.floatingPoint = floatingPoint;
    }

    public FloatingPoint getFloatingPoint() {
        return floatingPoint;
    }

    public void setOctetString(Integer32 octetString) {
        this.octetString = octetString;
    }

    public Integer32 getOctetString() {
        return octetString;
    }

    public void setVisibleString(Integer32 visibleString) {
        this.visibleString = visibleString;
    }

    public Integer32 getVisibleString() {
        return visibleString;
    }

    public void setBinaryTime(BerBoolean binaryTime) {
        this.binaryTime = binaryTime;
    }

    public BerBoolean getBinaryTime() {
        return binaryTime;
    }

    public void setMMSString(Integer32 mMSString) {
        this.mMSString = mMSString;
    }

    public Integer32 getMMSString() {
        return mMSString;
    }

    public void setUtcTime(BerNull utcTime) {
        this.utcTime = utcTime;
    }

    public BerNull getUtcTime() {
        return utcTime;
    }

    public int encode(BerByteArrayOutputStream os, boolean explicit) throws IOException {
        if (code != null) {
            for (int i = code.length - 1; i >= 0; i--) {
                os.write(code[i]);
            }
            return code.length;

        }
        int codeLength = 0;
        if (utcTime != null) {
            codeLength += utcTime.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 17
            os.write(0x91);
            codeLength += 1;
            return codeLength;

        }

        if (mMSString != null) {
            codeLength += mMSString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 16
            os.write(0x90);
            codeLength += 1;
            return codeLength;

        }

        if (binaryTime != null) {
            codeLength += binaryTime.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 12
            os.write(0x8c);
            codeLength += 1;
            return codeLength;

        }

        if (visibleString != null) {
            codeLength += visibleString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 10
            os.write(0x8a);
            codeLength += 1;
            return codeLength;

        }

        if (octetString != null) {
            codeLength += octetString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 9
            os.write(0x89);
            codeLength += 1;
            return codeLength;

        }

        if (floatingPoint != null) {
            codeLength += floatingPoint.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 7
            os.write(0xa7);
            codeLength += 1;
            return codeLength;

        }

        if (unsigned != null) {
            codeLength += unsigned.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 6
            os.write(0x86);
            codeLength += 1;
            return codeLength;

        }

        if (integer != null) {
            codeLength += integer.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 5
            os.write(0x85);
            codeLength += 1;
            return codeLength;

        }

        if (bitString != null) {
            codeLength += bitString.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 4
            os.write(0x84);
            codeLength += 1;
            return codeLength;

        }

        if (bool != null) {
            codeLength += bool.encode(os, false);
            // write tag: CONTEXT_CLASS, PRIMITIVE, 3
            os.write(0x83);
            codeLength += 1;
            return codeLength;

        }

        if (structure != null) {
            codeLength += structure.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 2
            os.write(0xa2);
            codeLength += 1;
            return codeLength;

        }

        if (array != null) {
            codeLength += array.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 1
            os.write(0xa1);
            codeLength += 1;
            return codeLength;

        }

        throw new IOException("Error encoding BerChoice: No item in choice was selected.");
    }

    public int decode(InputStream is, BerIdentifier berIdentifier) throws IOException {
        int codeLength = 0;
        BerIdentifier passedIdentifier = berIdentifier;

        if (berIdentifier == null) {
            berIdentifier = new BerIdentifier();
            codeLength += berIdentifier.decode(is);
        }

        BerLength length = new BerLength();
        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 1)) {
            array = new Array();
            codeLength += array.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 2)) {
            structure = new Structure();
            codeLength += structure.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 3)) {
            bool = new BerNull();
            codeLength += bool.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 4)) {
            bitString = new Integer32();
            codeLength += bitString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 5)) {
            integer = new Unsigned8();
            codeLength += integer.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 6)) {
            unsigned = new Unsigned8();
            codeLength += unsigned.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 7)) {
            floatingPoint = new FloatingPoint();
            codeLength += floatingPoint.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 9)) {
            octetString = new Integer32();
            codeLength += octetString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 10)) {
            visibleString = new Integer32();
            codeLength += visibleString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 12)) {
            binaryTime = new BerBoolean();
            codeLength += binaryTime.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 16)) {
            mMSString = new Integer32();
            codeLength += mMSString.decode(is, false);
            return codeLength;
        }

        if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 17)) {
            utcTime = new BerNull();
            codeLength += utcTime.decode(is, false);
            return codeLength;
        }

        if (passedIdentifier != null) {
            return 0;
        }
        throw new IOException("Error decoding BerChoice: Identifier matched to no item.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws IOException {
        BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess);
        encode(os, false);
        code = os.getArray();
    }

    @Override
    public String toString() {
        if (array != null) {
            return "CHOICE{array: " + array + "}";
        }

        if (structure != null) {
            return "CHOICE{structure: " + structure + "}";
        }

        if (bool != null) {
            return "CHOICE{bool: " + bool + "}";
        }

        if (bitString != null) {
            return "CHOICE{bitString: " + bitString + "}";
        }

        if (integer != null) {
            return "CHOICE{integer: " + integer + "}";
        }

        if (unsigned != null) {
            return "CHOICE{unsigned: " + unsigned + "}";
        }

        if (floatingPoint != null) {
            return "CHOICE{floatingPoint: " + floatingPoint + "}";
        }

        if (octetString != null) {
            return "CHOICE{octetString: " + octetString + "}";
        }

        if (visibleString != null) {
            return "CHOICE{visibleString: " + visibleString + "}";
        }

        if (binaryTime != null) {
            return "CHOICE{binaryTime: " + binaryTime + "}";
        }

        if (mMSString != null) {
            return "CHOICE{mMSString: " + mMSString + "}";
        }

        if (utcTime != null) {
            return "CHOICE{utcTime: " + utcTime + "}";
        }

        return "unknown";
    }

}
