ByteListImpl.java revision 649:507d4f7efba6
1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.dv.util;
23
24import java.util.AbstractList;
25
26import com.sun.org.apache.xerces.internal.xs.XSException;
27import com.sun.org.apache.xerces.internal.xs.datatypes.ByteList;
28
29/**
30 * Implementation of <code>com.sun.org.apache.xerces.internal.xs.datatypes.ByteList</code>.
31 *
32 * @xerces.internal
33 *
34 * @author Ankit Pasricha, IBM
35 *
36 */
37public class ByteListImpl extends AbstractList implements ByteList {
38
39    // actually data stored in a byte array
40    protected final byte[] data;
41
42    // canonical representation of the data
43    protected String canonical;
44
45    public ByteListImpl(byte[] data) {
46        this.data = data;
47    }
48
49    /**
50     * The number of <code>byte</code>s in the list. The range of
51     * valid child object indices is 0 to <code>length-1</code> inclusive.
52     */
53    public int getLength() {
54        return data.length;
55    }
56
57    /**
58     * Checks if the <code>byte</code> <code>item</code> is a
59     * member of this list.
60     * @param item  <code>byte</code> whose presence in this list
61     *   is to be tested.
62     * @return  True if this list contains the <code>byte</code>
63     *   <code>item</code>.
64     */
65    public boolean contains(byte item) {
66        for (int i = 0; i < data.length; ++i) {
67            if (data[i] == item) {
68                return true;
69            }
70        }
71        return false;
72    }
73
74    /**
75     * Returns the <code>index</code>th item in the collection. The index
76     * starts at 0.
77     * @param index  index into the collection.
78     * @return  The <code>byte</code> at the <code>index</code>th
79     *   position in the <code>ByteList</code>.
80     * @exception XSException
81     *   INDEX_SIZE_ERR: if <code>index</code> is greater than or equal to the
82     *   number of objects in the list.
83     */
84    public byte item(int index)
85        throws XSException {
86
87        if(index < 0 || index > data.length - 1) {
88            throw new XSException(XSException.INDEX_SIZE_ERR, null);
89        }
90        return data[index];
91    }
92
93    /*
94     * List methods
95     */
96
97    public Object get(int index) {
98        if (index >= 0 && index < data.length) {
99            return new Byte(data[index]);
100        }
101        throw new IndexOutOfBoundsException("Index: " + index);
102    }
103
104    public int size() {
105        return getLength();
106    }
107
108    public byte[] toByteArray() {
109        byte[] ret = new byte[data.length];
110        System.arraycopy(data, 0, ret, 0, data.length);
111        return ret;
112    }
113}
114