OSDN Git Service

The first commit.
[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          */
50         protected CollectionType(Class<T> componentType) {
51                 this.componentType = componentType;
52                 collection = newCollection();
53         }
54
55         /**
56          * Returns the collection that contained in the data.
57          * 
58          * @return The collection.
59          */
60         public Collection<T> collection() {
61                 return collection;
62         }
63
64         /**
65          * Returns the type of component of this data.
66          * 
67          * @return The type of component.
68          */
69         public Class<T> componentType() {
70                 return componentType;
71         }
72
73         /**
74          * Returns a {@link TypeSpecification} instance that associated to the
75          * component type.
76          * 
77          * @return {@link TypeSpecification} instance
78          */
79         public TypeSpecification componentSpecification() {
80                 return TypeSpecification.getSpecification(componentType);
81         }
82
83         /**
84          * Instantiates a new collection of the component type.
85          * 
86          * @return A new collection.
87          */
88         protected abstract Collection<T> newCollection();
89
90         /*
91          * (non-Javadoc)
92          * 
93          * @see jp.bitmeister.asn1.type.SizeCountable#size()
94          */
95         @Override
96         public int size() {
97                 return collection.size();
98         }
99
100         /*
101          * (non-Javadoc)
102          * 
103          * @see jp.bitmeister.asn1.type.ASN1Type#clear()
104          */
105         @Override
106         public void clear() {
107                 collection = newCollection();
108         }
109
110         /*
111          * (non-Javadoc)
112          * 
113          * @see jp.bitmeister.asn1.type.ASN1Type#hasValue()
114          */
115         @Override
116         public boolean hasValue() {
117                 return true;
118         }
119
120         /*
121          * (non-Javadoc)
122          * 
123          * @see jp.bitmeister.asn1.type.ASN1Type#valueEquals(java.lang.Object)
124          */
125         @Override
126         public boolean valueEquals(Object other) {
127                 if (other instanceof CollectionType<?>) {
128                         return collection.equals(((CollectionType<?>) other).collection);
129                 }
130                 return false;
131         }
132
133         /*
134          * (non-Javadoc)
135          * 
136          * @see jp.bitmeister.asn1.type.ASN1Type#hashCode()
137          */
138         @Override
139         public int hashCode() {
140                 return collection.hashCode();
141         }
142
143         /*
144          * (non-Javadoc)
145          * 
146          * @see jp.bitmeister.asn1.type.ASN1Type#clone()
147          */
148         @Override
149         @SuppressWarnings("unchecked")
150         public Object clone() {
151                 CollectionType<T> clone = ASN1Type.instantiate(getClass());
152                 clone.collection.addAll(collection);
153                 return clone;
154         }
155
156 }