OSDN Git Service

01e5027c65fb4a06ef997209088ab7a49522f93d
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / builtin / SET_OF.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.builtin;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20
21 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
22 import jp.bitmeister.asn1.annotation.ASN1Identifier;
23 import jp.bitmeister.asn1.annotation.ASN1Tag;
24 import jp.bitmeister.asn1.annotation.ASN1XmlTypeName;
25 import jp.bitmeister.asn1.processor.ASN1Visitor;
26 import jp.bitmeister.asn1.type.ASN1TagClass;
27 import jp.bitmeister.asn1.type.ASN1TagMode;
28 import jp.bitmeister.asn1.type.ASN1Type;
29 import jp.bitmeister.asn1.type.CollectionType;
30
31 /**
32  * Represents ASN.1 'SET OF' type.
33  * 
34  * <p>
35  * This is the class used for defining 'SET OF' types. Generic type parameter
36  * {@code T} indicates component type of the 'SET OF' type. A constructor of a
37  * class that extends {@code SET_OF} must call parent constructor with
38  * {@code componentType} parameter to assign the {@code Class} object of T.
39  * </p>
40  * 
41  * @author WATANABE, Jun. <jwat at bitmeister.jp>
42  */
43 @ASN1BuiltIn
44 @ASN1Identifier("SET")
45 @ASN1XmlTypeName("SET_OF")
46 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 17, tagMode = ASN1TagMode.IMPLICIT)
47 public abstract class SET_OF<T extends ASN1Type> extends CollectionType<T> {
48
49         /**
50          * Instantiate an empty {@code SET_OF}.
51          * 
52          * @param componentType
53          *            The class instance of component type.
54          */
55         public SET_OF(Class<T> componentType) {
56                 super(componentType);
57         }
58
59         /*
60          * (non-Javadoc)
61          * 
62          * @see jp.bitmeister.asn1.type.CollectionType#newCollection()
63          */
64         @Override
65         @SuppressWarnings("serial")
66         protected Collection<T> newCollection() {
67                 return new ArrayList<T>() {
68
69                         @Override
70                         @SuppressWarnings("unchecked")
71                         public boolean equals(Object other) {
72                                 if (other == null || !getClass().equals(other.getClass())
73                                                 || ((ArrayList<T>) other).size() != size()) {
74                                         return false;
75                                 }
76                                 boolean[] checked = new boolean[size()];
77                                 for (ASN1Type e : (ArrayList<T>) other) {
78                                         for (int i = 0; i < size(); i++) {
79                                                 if (!checked[i] && get(i).equals(e)) {
80                                                         checked[i] = true;
81                                                         break;
82                                                 }
83                                         }
84                                 }
85                                 for (int i = 0; i < size(); i++) {
86                                         if (!checked[i]) {
87                                                 return false;
88                                         }
89                                 }
90                                 return true;
91                         }
92
93                 };
94         }
95
96         /*
97          * (non-Javadoc)
98          * 
99          * @see
100          * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
101          * ASN1Visitor)
102          */
103         @Override
104         public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
105                 return visitor.visit(this);
106         }
107
108 }