OSDN Git Service

modifications for Java1.5
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / CollectionType.java
1 /*
2  * Copyright 2011 BitMeister Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package jp.bitmeister.asn1.type;
17
18 import java.util.Collection;
19
20 import jp.bitmeister.asn1.type.builtin.SEQUENCE_OF;
21 import jp.bitmeister.asn1.type.builtin.SET_OF;
22
23 /**
24  * The base class for structured types defined by referencing a single ASN.1
25  * type.
26  * 
27  * <p>
28  * This class provides generic interfaces and common methods for classes that
29  * represents structured types which defined by referencing a single ASN.1 type.
30  * This class is the parent class of {@link SEQUENCE_OF} and {@link SET_OF}.
31  * </p>
32  * 
33  * @see SEQUENCE_OF
34  * @see SET_OF
35  * @author WATANABE, Jun. <jwat at bitmeister.jp>
36  */
37 public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
38                 implements SizeCountable {
39
40         private Class<T> componentType;
41
42         private Collection<T> collection;
43
44         /**
45          * Instantiate a {@code CollectionType} instance whose component is the
46          * type.
47          * 
48          * @param componentType
49          *            The {@code class} object of the component of this collection.
50          */
51         protected CollectionType(Class<T> componentType) {
52                 this.componentType = componentType;
53                 collection = newCollection();
54         }
55
56         /**
57          * Instantiate a {@code CollectionType} instance whose component is the type
58          * and initialize it with the components.
59          * 
60          * @param componentType
61          *            The {@code class} object of the component of this collection.
62          * @param components
63          *            Components to be set this instance.
64          */
65         protected CollectionType(Class<T> componentType, T... components) {
66                 this(componentType);
67                 for (T e : components) {
68                         collection.add(e);
69                 }
70         }
71
72         /**
73          * Returns the collection that contained in the data.
74          * 
75          * @return The collection.
76          */
77         public Collection<T> collection() {
78                 return collection;
79         }
80
81         /**
82          * Returns the type of component of this data.
83          * 
84          * @return The type of component.
85          */
86         public Class<T> componentType() {
87                 return componentType;
88         }
89
90         /**
91          * Returns a {@link TypeSpecification} instance that associated to the
92          * component type.
93          * 
94          * @return {@link TypeSpecification} instance
95          */
96         public TypeSpecification componentSpecification() {
97                 return TypeSpecification.getSpecification(componentType);
98         }
99
100         /**
101          * Instantiates a new collection of the component type.
102          * 
103          * @return A new collection.
104          */
105         protected abstract Collection<T> newCollection();
106
107         /*
108          * (non-Javadoc)
109          * 
110          * @see jp.bitmeister.asn1.type.SizeCountable#size()
111          */
112         public int size() {
113                 return collection.size();
114         }
115
116         /*
117          * (non-Javadoc)
118          * 
119          * @see jp.bitmeister.asn1.type.ASN1Type#clear()
120          */
121         @Override
122         public void clear() {
123                 collection = newCollection();
124         }
125
126         /*
127          * (non-Javadoc)
128          * 
129          * @see jp.bitmeister.asn1.type.ASN1Type#hasValue()
130          */
131         @Override
132         public boolean hasValue() {
133                 return true;
134         }
135
136         /*
137          * (non-Javadoc)
138          * 
139          * @see jp.bitmeister.asn1.type.ASN1Type#valueEquals(java.lang.Object)
140          */
141         @Override
142         public boolean valueEquals(Object other) {
143                 if (other instanceof CollectionType<?>) {
144                         return collection.equals(((CollectionType<?>) other).collection);
145                 }
146                 return false;
147         }
148
149         /*
150          * (non-Javadoc)
151          * 
152          * @see jp.bitmeister.asn1.type.ASN1Type#hashCode()
153          */
154         @Override
155         public int hashCode() {
156                 return collection.hashCode();
157         }
158
159         /*
160          * (non-Javadoc)
161          * 
162          * @see jp.bitmeister.asn1.type.ASN1Type#clone()
163          */
164         @Override
165         @SuppressWarnings("unchecked")
166         public Object clone() {
167                 CollectionType<T> clone = ASN1Type.instantiate(getClass());
168                 clone.collection.addAll(collection);
169                 return clone;
170         }
171
172 }