KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > primitives > adapters > AbstractByteCollectionCollection


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.collections.primitives.adapters;
18
19 import java.lang.reflect.Array JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.commons.collections.primitives.ByteCollection;
24
25 /**
26  * @since Commons Primitives 1.0
27  * @version $Revision: 480462 $ $Date: 2006-11-29 00:15:00 -0800 (Wed, 29 Nov 2006) $
28  * @author Rodney Waldhoff
29  */

30 abstract class AbstractByteCollectionCollection implements Collection JavaDoc {
31     
32     public boolean add(Object JavaDoc element) {
33         return getByteCollection().add(((Number JavaDoc)element).byteValue());
34     }
35
36     public boolean addAll(Collection JavaDoc c) {
37         return getByteCollection().addAll(CollectionByteCollection.wrap(c));
38     }
39         
40     public void clear() {
41         getByteCollection().clear();
42     }
43
44     public boolean contains(Object JavaDoc element) {
45         return getByteCollection().contains(((Number JavaDoc)element).byteValue());
46     }
47    
48     
49     public boolean containsAll(Collection JavaDoc c) {
50         return getByteCollection().containsAll(CollectionByteCollection.wrap(c));
51     }
52         
53     public String JavaDoc toString() {
54         return getByteCollection().toString();
55     }
56     
57     public boolean isEmpty() {
58         return getByteCollection().isEmpty();
59     }
60     
61     /**
62      * {@link ByteIteratorIterator#wrap wraps} the
63      * {@link org.apache.commons.collections.primitives.ByteIterator ByteIterator}
64      * returned by my underlying
65      * {@link ByteCollection ByteCollection},
66      * if any.
67      */

68     public Iterator JavaDoc iterator() {
69         return ByteIteratorIterator.wrap(getByteCollection().iterator());
70     }
71      
72     public boolean remove(Object JavaDoc element) {
73         return getByteCollection().removeElement(((Number JavaDoc)element).byteValue());
74     }
75     
76     public boolean removeAll(Collection JavaDoc c) {
77         return getByteCollection().removeAll(CollectionByteCollection.wrap(c));
78     }
79     
80     public boolean retainAll(Collection JavaDoc c) {
81         return getByteCollection().retainAll(CollectionByteCollection.wrap(c));
82     }
83     
84     public int size() {
85         return getByteCollection().size();
86     }
87     
88     public Object JavaDoc[] toArray() {
89         byte[] a = getByteCollection().toArray();
90         Object JavaDoc[] A = new Object JavaDoc[a.length];
91         for(int i=0;i<a.length;i++) {
92             A[i] = new Byte JavaDoc(a[i]);
93         }
94         return A;
95     }
96     
97     public Object JavaDoc[] toArray(Object JavaDoc[] A) {
98         byte[] a = getByteCollection().toArray();
99         if(A.length < a.length) {
100             A = (Object JavaDoc[])(Array.newInstance(A.getClass().getComponentType(), a.length));
101         }
102         for(int i=0;i<a.length;i++) {
103             A[i] = new Byte JavaDoc(a[i]);
104         }
105         if(A.length > a.length) {
106             A[a.length] = null;
107         }
108
109         return A;
110     }
111
112     protected abstract ByteCollection getByteCollection();
113 }
114
Popular Tags