/**
 * 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 org.openmuc.jasn1.ber.BerByteArrayOutputStream;
import org.openmuc.jasn1.ber.BerIdentifier;
import org.openmuc.jasn1.ber.BerLength;
import org.openmuc.jasn1.ber.types.BerNull;
import org.openmuc.jasn1.ber.types.string.BerVisibleString;

public class AlternateAccessSelection {

    public byte[] code = null;

    public static class SelectAlternateAccess {

        public static class AccessSelection {

            public byte[] code = null;

            public static class Component {

                public byte[] code = null;
                private BasicIdentifier basic = null;

                public Component() {
                }

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

                public void setBasic(BasicIdentifier basic) {
                    this.basic = basic;
                }

                public BasicIdentifier getBasic() {
                    return basic;
                }

                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 (basic != null) {
                        codeLength += basic.encode(os, true);
                        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(BerVisibleString.identifier)) {
                        basic = new BasicIdentifier();
                        codeLength += basic.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 (basic != null) {
                        return "CHOICE{basic: " + basic + "}";
                    }

                    return "unknown";
                }

            }

            public static class IndexRange {

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

                public byte[] code = null;
                private Unsigned32 lowIndex = null;

                private Unsigned32 numberOfElements = null;

                public IndexRange() {
                    id = identifier;
                }

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

                public void setLowIndex(Unsigned32 lowIndex) {
                    this.lowIndex = lowIndex;
                }

                public Unsigned32 getLowIndex() {
                    return lowIndex;
                }

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

                public Unsigned32 getNumberOfElements() {
                    return numberOfElements;
                }

                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 += numberOfElements.encode(os, false);
                        // write tag: CONTEXT_CLASS, PRIMITIVE, 1
                        os.write(0x81);
                        codeLength += 1;

                        codeLength += lowIndex.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)) {
                        lowIndex = new Unsigned32();
                        subCodeLength += lowIndex.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.PRIMITIVE, 1)) {
                        numberOfElements = new Unsigned32();
                        subCodeLength += numberOfElements.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("lowIndex: ").append(lowIndex);

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

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

            }

            private Component component = null;

            private Unsigned32 index = null;

            private IndexRange indexRange = null;

            private BerNull allElements = null;

            public AccessSelection() {
            }

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

            public void setComponent(Component component) {
                this.component = component;
            }

            public Component getComponent() {
                return component;
            }

            public void setIndex(Unsigned32 index) {
                this.index = index;
            }

            public Unsigned32 getIndex() {
                return index;
            }

            public void setIndexRange(IndexRange indexRange) {
                this.indexRange = indexRange;
            }

            public IndexRange getIndexRange() {
                return indexRange;
            }

            public void setAllElements(BerNull allElements) {
                this.allElements = allElements;
            }

            public BerNull getAllElements() {
                return allElements;
            }

            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;
                int sublength;

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

                }

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

                }

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

                }

                if (component != null) {
                    sublength = component.encode(os, true);
                    codeLength += sublength;
                    codeLength += BerLength.encodeLength(os, sublength);
                    // write tag: CONTEXT_CLASS, CONSTRUCTED, 0
                    os.write(0xa0);
                    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, 0)) {
                    codeLength += length.decode(is);
                    component = new Component();
                    codeLength += component.decode(is, null);
                    return codeLength;
                }

                if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 1)) {
                    index = new Unsigned32();
                    codeLength += index.decode(is, false);
                    return codeLength;
                }

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

                if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 3)) {
                    allElements = new BerNull();
                    codeLength += allElements.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 (component != null) {
                    return "CHOICE{component: " + component + "}";
                }

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

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

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

                return "unknown";
            }

        }

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

        public byte[] code = null;
        private AccessSelection accessSelection = null;

        private AlternateAccess alternateAccess = null;

        public SelectAlternateAccess() {
            id = identifier;
        }

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

        public void setAccessSelection(AccessSelection accessSelection) {
            this.accessSelection = accessSelection;
        }

        public AccessSelection getAccessSelection() {
            return accessSelection;
        }

        public void setAlternateAccess(AlternateAccess alternateAccess) {
            this.alternateAccess = alternateAccess;
        }

        public AlternateAccess getAlternateAccess() {
            return alternateAccess;
        }

        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 += alternateAccess.encode(os, true);

                codeLength += accessSelection.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);
            accessSelection = new AccessSelection();
            subCodeLength += accessSelection.decode(is, berIdentifier);
            subCodeLength += berIdentifier.decode(is);

            if (berIdentifier.equals(AlternateAccess.identifier)) {
                alternateAccess = new AlternateAccess();
                subCodeLength += alternateAccess.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("accessSelection: ").append(accessSelection);

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

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

    }

    public static class SelectAccess {

        public byte[] code = null;

        public static class Component {

            public byte[] code = null;
            private BasicIdentifier basic = null;

            public Component() {
            }

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

            public void setBasic(BasicIdentifier basic) {
                this.basic = basic;
            }

            public BasicIdentifier getBasic() {
                return basic;
            }

            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 (basic != null) {
                    codeLength += basic.encode(os, true);
                    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(BerVisibleString.identifier)) {
                    basic = new BasicIdentifier();
                    codeLength += basic.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 (basic != null) {
                    return "CHOICE{basic: " + basic + "}";
                }

                return "unknown";
            }

        }

        public static class IndexRange {

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

            public byte[] code = null;
            private Unsigned32 lowIndex = null;

            private Unsigned32 numberOfElements = null;

            public IndexRange() {
                id = identifier;
            }

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

            public void setLowIndex(Unsigned32 lowIndex) {
                this.lowIndex = lowIndex;
            }

            public Unsigned32 getLowIndex() {
                return lowIndex;
            }

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

            public Unsigned32 getNumberOfElements() {
                return numberOfElements;
            }

            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 += numberOfElements.encode(os, false);
                    // write tag: CONTEXT_CLASS, PRIMITIVE, 1
                    os.write(0x81);
                    codeLength += 1;

                    codeLength += lowIndex.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)) {
                    lowIndex = new Unsigned32();
                    subCodeLength += lowIndex.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.PRIMITIVE, 1)) {
                    numberOfElements = new Unsigned32();
                    subCodeLength += numberOfElements.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("lowIndex: ").append(lowIndex);

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

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

        }

        private Component component = null;

        private Unsigned32 index = null;

        private IndexRange indexRange = null;

        private BerNull allElements = null;

        public SelectAccess() {
        }

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

        public void setComponent(Component component) {
            this.component = component;
        }

        public Component getComponent() {
            return component;
        }

        public void setIndex(Unsigned32 index) {
            this.index = index;
        }

        public Unsigned32 getIndex() {
            return index;
        }

        public void setIndexRange(IndexRange indexRange) {
            this.indexRange = indexRange;
        }

        public IndexRange getIndexRange() {
            return indexRange;
        }

        public void setAllElements(BerNull allElements) {
            this.allElements = allElements;
        }

        public BerNull getAllElements() {
            return allElements;
        }

        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;
            int sublength;

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

            }

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

            }

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

            }

            if (component != null) {
                sublength = component.encode(os, true);
                codeLength += sublength;
                codeLength += BerLength.encodeLength(os, sublength);
                // 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)) {
                codeLength += length.decode(is);
                component = new Component();
                codeLength += component.decode(is, null);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 2)) {
                index = new Unsigned32();
                codeLength += index.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.CONSTRUCTED, 3)) {
                indexRange = new IndexRange();
                codeLength += indexRange.decode(is, false);
                return codeLength;
            }

            if (berIdentifier.equals(BerIdentifier.CONTEXT_CLASS, BerIdentifier.PRIMITIVE, 4)) {
                allElements = new BerNull();
                codeLength += allElements.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 (component != null) {
                return "CHOICE{component: " + component + "}";
            }

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

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

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

            return "unknown";
        }

    }

    private SelectAlternateAccess selectAlternateAccess = null;

    private SelectAccess selectAccess = null;

    public AlternateAccessSelection() {
    }

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

    public void setSelectAlternateAccess(SelectAlternateAccess selectAlternateAccess) {
        this.selectAlternateAccess = selectAlternateAccess;
    }

    public SelectAlternateAccess getSelectAlternateAccess() {
        return selectAlternateAccess;
    }

    public void setSelectAccess(SelectAccess selectAccess) {
        this.selectAccess = selectAccess;
    }

    public SelectAccess getSelectAccess() {
        return selectAccess;
    }

    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 (selectAccess != null) {
            codeLength += selectAccess.encode(os, true);
            return codeLength;

        }

        if (selectAlternateAccess != null) {
            codeLength += selectAlternateAccess.encode(os, false);
            // write tag: CONTEXT_CLASS, CONSTRUCTED, 0
            os.write(0xa0);
            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, 0)) {
            selectAlternateAccess = new SelectAlternateAccess();
            codeLength += selectAlternateAccess.decode(is, false);
            return codeLength;
        }

        selectAccess = new SelectAccess();
        int choiceDecodeLength = selectAccess.decode(is, berIdentifier);
        if (choiceDecodeLength != 0) {
            codeLength += choiceDecodeLength;
            return codeLength;
        }
        else {
            selectAccess = null;
        }

        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 (selectAlternateAccess != null) {
            return "CHOICE{selectAlternateAccess: " + selectAlternateAccess + "}";
        }

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

        return "unknown";
    }

}
