2 * Copyright 2011 BitMeister Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package jp.bitmeister.asn1.type;
18 import java.util.Collection;
20 import jp.bitmeister.asn1.type.builtin.SEQUENCE_OF;
21 import jp.bitmeister.asn1.type.builtin.SET_OF;
24 * The base class for structured types defined by referencing a single ASN.1
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}.
35 * @author WATANABE, Jun. <jwat at bitmeister.jp>
37 public abstract class CollectionType<T extends ASN1Type> extends ASN1Type
38 implements SizeCountable {
40 private Class<T> componentType;
42 private Collection<T> collection;
45 * Instantiate a {@code CollectionType} instance whose component is the
48 * @param componentType
50 protected CollectionType(Class<T> componentType) {
51 this.componentType = componentType;
52 collection = newCollection();
56 * Returns the collection that contained in the data.
58 * @return The collection.
60 public Collection<T> collection() {
65 * Returns the type of component of this data.
67 * @return The type of component.
69 public Class<T> componentType() {
74 * Returns a {@link TypeSpecification} instance that associated to the
77 * @return {@link TypeSpecification} instance
79 public TypeSpecification componentSpecification() {
80 return TypeSpecification.getSpecification(componentType);
84 * Instantiates a new collection of the component type.
86 * @return A new collection.
88 protected abstract Collection<T> newCollection();
93 * @see jp.bitmeister.asn1.type.SizeCountable#size()
97 return collection.size();
103 * @see jp.bitmeister.asn1.type.ASN1Type#clear()
106 public void clear() {
107 collection = newCollection();
113 * @see jp.bitmeister.asn1.type.ASN1Type#hasValue()
116 public boolean hasValue() {
123 * @see jp.bitmeister.asn1.type.ASN1Type#valueEquals(java.lang.Object)
126 public boolean valueEquals(Object other) {
127 if (other instanceof CollectionType<?>) {
128 return collection.equals(((CollectionType<?>) other).collection);
136 * @see jp.bitmeister.asn1.type.ASN1Type#hashCode()
139 public int hashCode() {
140 return collection.hashCode();
146 * @see jp.bitmeister.asn1.type.ASN1Type#clone()
149 @SuppressWarnings("unchecked")
150 public Object clone() {
151 CollectionType<T> clone = ASN1Type.instantiate(getClass());
152 clone.collection.addAll(collection);