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
49 * The {@code class} object of the component of this collection.
51 protected CollectionType(Class<T> componentType) {
52 this.componentType = componentType;
53 collection = newCollection();
57 * Instantiate a {@code CollectionType} instance whose component is the type
58 * and initialize it with the components.
60 * @param componentType
61 * The {@code class} object of the component of this collection.
63 * Components to be set this instance.
65 protected CollectionType(Class<T> componentType, T... components) {
67 for (T e : components) {
73 * Returns the collection that contained in the data.
75 * @return The collection.
77 public Collection<T> collection() {
82 * Returns the type of component of this data.
84 * @return The type of component.
86 public Class<T> componentType() {
91 * Returns a {@link TypeSpecification} instance that associated to the
94 * @return {@link TypeSpecification} instance
96 public TypeSpecification componentSpecification() {
97 return TypeSpecification.getSpecification(componentType);
101 * Instantiates a new collection of the component type.
103 * @return A new collection.
105 protected abstract Collection<T> newCollection();
110 * @see jp.bitmeister.asn1.type.SizeCountable#size()
113 return collection.size();
119 * @see jp.bitmeister.asn1.type.ASN1Type#clear()
122 public void clear() {
123 collection = newCollection();
129 * @see jp.bitmeister.asn1.type.ASN1Type#hasValue()
132 public boolean hasValue() {
139 * @see jp.bitmeister.asn1.type.ASN1Type#valueEquals(java.lang.Object)
142 public boolean valueEquals(Object other) {
143 if (other instanceof CollectionType<?>) {
144 return collection.equals(((CollectionType<?>) other).collection);
152 * @see jp.bitmeister.asn1.type.ASN1Type#hashCode()
155 public int hashCode() {
156 return collection.hashCode();
162 * @see jp.bitmeister.asn1.type.ASN1Type#clone()
165 @SuppressWarnings("unchecked")
166 public Object clone() {
167 CollectionType<T> clone = ASN1Type.instantiate(getClass());
168 clone.collection.addAll(collection);