1 /*
2 * Copyright 2001-2010 Stephen Colebourne
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 org.joda.primitives.list.impl;
17
18 import java.util.Collection;
19
20 import org.joda.primitives.ByteUtils;
21
22 /**
23 * Immutable array-based implementation of <code>ByteList</code> for
24 * primitive <code>int</code> elements.
25 * <p>
26 * This class implements {@link java.util.List List} allowing
27 * seamless integration with other APIs.
28 * <p>
29 * Add, Remove, Set and Clear are not supported as this class is immutable.
30 *
31 * @author Stephen Colebourne
32 * @version CODE GENERATED
33 * @since 1.0
34 */
35 public final class ImmutableArrayByteList extends AbstractByteList {
36 // This file is CODE GENERATED. Do not change manually.
37
38 /** The empty singleton. */
39 private static final ImmutableArrayByteList EMPTY = new ImmutableArrayByteList(ByteUtils.EMPTY_BYTE_ARRAY);
40
41 /** The array of elements. */
42 private byte[] data;
43
44 /**
45 * Gets a list that is empty.
46 */
47 public static ImmutableArrayByteList empty() {
48 return EMPTY;
49 }
50
51 /**
52 * Creates a list copying the specified array.
53 *
54 * @param values an array of values to copy, null treated as zero size array
55 * @return the created list, not null
56 */
57 public static ImmutableArrayByteList copyOf(byte[] values) {
58 if (values == null) {
59 return EMPTY;
60 } else {
61 return new ImmutableArrayByteList(values.clone());
62 }
63 }
64
65 /**
66 * Creates a list copying the values from the specified collection.
67 * <p>
68 * If the collection is an instance of this class, then it is simply returned.
69 *
70 * @param coll a collection of values to copy, null treated as zero size collection
71 * @return the created list, not null
72 */
73 public static ImmutableArrayByteList copyOf(Collection<Byte> coll) {
74 if (coll == null) {
75 return EMPTY;
76 } else if (coll instanceof ImmutableArrayByteList) {
77 return (ImmutableArrayByteList) coll;
78 } else {
79 return new ImmutableArrayByteList(ByteUtils.toPrimitiveArray(coll));
80 }
81 }
82
83 /**
84 * Constructor that copies the specified values.
85 *
86 * @param values the array to assign
87 */
88 private ImmutableArrayByteList(byte[] values) {
89 super();
90 data = values;
91 }
92
93 // Implementation
94 //-----------------------------------------------------------------------
95 /**
96 * Gets the current size of the collection.
97 *
98 * @return the current size
99 */
100 public int size() {
101 return data.length;
102 }
103
104 /**
105 * Gets the primitive value at the specified index.
106 *
107 * @param index the index to get from
108 * @return value at the index
109 * @throws IndexOutOfBoundsException if the index is invalid
110 */
111 public byte getByte(int index) {
112 checkIndexExists(index);
113 return data[index];
114 }
115
116 // Overrides
117 //-----------------------------------------------------------------------
118 /**
119 * Checks whether this collection contains a specified primitive value.
120 * <p>
121 * This implementation accesses the internal storage array directly.
122 *
123 * @param value the value to search for
124 * @return <code>true</code> if the value is found
125 */
126 public boolean contains(byte value) {
127 for (int i = 0; i < data.length; i++) {
128 if (data[i] == value) {
129 return true;
130 }
131 }
132 return false;
133 }
134
135 //-----------------------------------------------------------------------
136 /**
137 * Clone implementation that returns {@code this}.
138 *
139 * @return {@code this}
140 */
141 public Object clone() {
142 return this;
143 }
144
145 /**
146 * Copies data from this collection into the specified array.
147 * This method is pre-validated.
148 *
149 * @param fromIndex the index to start from
150 * @param dest the destination array
151 * @param destIndex the destination start index
152 * @param size the number of items to copy
153 */
154 protected void arrayCopy(int fromIndex, byte[] dest, int destIndex, int size) {
155 System.arraycopy(data, fromIndex, dest, destIndex, size);
156 }
157
158 }
159