001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1;
019    
020    import java.io.IOException;
021    import java.math.BigInteger;
022    
023    public class DEREnumerated
024        extends DERObject
025    {
026        byte[]      bytes;
027    
028        /**
029         * return an integer from the passed in object
030         *
031         * @exception IllegalArgumentException if the object cannot be converted.
032         */
033        public static DEREnumerated getInstance(
034            Object  obj)
035        {
036            if (obj == null || obj instanceof DEREnumerated)
037            {
038                return (DEREnumerated)obj;
039            }
040    
041            if (obj instanceof ASN1OctetString)
042            {
043                return new DEREnumerated(((ASN1OctetString)obj).getOctets());
044            }
045    
046            if (obj instanceof ASN1TaggedObject)
047            {
048                return getInstance(((ASN1TaggedObject)obj).getObject());
049            }
050    
051            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
052        }
053    
054        /**
055         * return an Enumerated from a tagged object.
056         *
057         * @param obj the tagged object holding the object we want
058         * @param explicit true if the object is meant to be explicitly
059         *              tagged false otherwise.
060         * @exception IllegalArgumentException if the tagged object cannot
061         *               be converted.
062         */
063        public static DEREnumerated getInstance(
064            ASN1TaggedObject obj,
065            boolean          explicit)
066        {
067            return getInstance(obj.getObject());
068        }
069    
070        public DEREnumerated(
071            int         value)
072        {
073            bytes = BigInteger.valueOf(value).toByteArray();
074        }
075    
076        public DEREnumerated(
077            BigInteger   value)
078        {
079            bytes = value.toByteArray();
080        }
081    
082        public DEREnumerated(
083            byte[]   bytes)
084        {
085            this.bytes = bytes;
086        }
087    
088        public BigInteger getValue()
089        {
090            return new BigInteger(bytes);
091        }
092    
093        void encode(
094            DEROutputStream out)
095            throws IOException
096        {
097            out.writeEncoded(ENUMERATED, bytes);
098        }
099    
100        public boolean equals(
101            Object  o)
102        {
103            if (o == null || !(o instanceof DEREnumerated))
104            {
105                return false;
106            }
107    
108            DEREnumerated other = (DEREnumerated)o;
109    
110            if (bytes.length != other.bytes.length)
111            {
112                return false;
113            }
114    
115            for (int i = 0; i != bytes.length; i++)
116            {
117                if (bytes[i] != other.bytes[i])
118                {
119                    return false;
120                }
121            }
122    
123            return true;
124        }
125    
126        public int hashCode()
127        {
128            return this.getValue().hashCode();
129        }
130    }