001/*
002 *  Copyright 2001-2010 Stephen Colebourne
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.joda.primitives.list.impl;
017
018import java.util.Collection;
019
020import org.joda.primitives.FloatUtils;
021
022/**
023 * Immutable array-based implementation of <code>FloatList</code> for
024 * primitive <code>int</code> elements.
025 * <p>
026 * This class implements {@link java.util.List List} allowing
027 * seamless integration with other APIs.
028 * <p>
029 * Add, Remove, Set and Clear are not supported as this class is immutable.
030 *
031 * @author Stephen Colebourne
032 * @version CODE GENERATED
033 * @since 1.0
034 */
035public final class ImmutableArrayFloatList extends AbstractFloatList {
036    // This file is CODE GENERATED. Do not change manually.
037
038    /** The empty singleton. */
039    private static final ImmutableArrayFloatList EMPTY = new ImmutableArrayFloatList(FloatUtils.EMPTY_FLOAT_ARRAY);
040
041    /** The array of elements. */
042    private float[] data;
043
044    /**
045     * Gets a list that is empty.
046     */
047    public static ImmutableArrayFloatList empty() {
048        return EMPTY;
049    }
050
051    /**
052     * Creates a list copying the specified array.
053     * 
054     * @param values  an array of values to copy, null treated as zero size array
055     * @return the created list, not null
056     */
057    public static ImmutableArrayFloatList copyOf(float[] values) {
058        if (values == null) {
059            return EMPTY;
060        } else {
061            return new ImmutableArrayFloatList(values.clone());
062        }
063    }
064
065    /**
066     * Creates a list copying the values from the specified collection.
067     * <p>
068     * If the collection is an instance of this class, then it is simply returned.
069     * 
070     * @param coll  a collection of values to copy, null treated as zero size collection
071     * @return the created list, not null
072     */
073    public static ImmutableArrayFloatList copyOf(Collection<Float> coll) {
074        if (coll == null) {
075            return EMPTY;
076        } else if (coll instanceof ImmutableArrayFloatList) {
077            return (ImmutableArrayFloatList) coll;
078        } else {
079            return new ImmutableArrayFloatList(FloatUtils.toPrimitiveArray(coll));
080        }
081    }
082
083    /**
084     * Constructor that copies the specified values.
085     * 
086     * @param values  the array to assign
087     */
088    private ImmutableArrayFloatList(float[] values) {
089        super();
090        data = values;
091    }
092
093    // Implementation
094    //-----------------------------------------------------------------------
095    /**
096     * Gets the current size of the collection.
097     * 
098     * @return the current size
099     */
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 float getFloat(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(float 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, float[] dest, int destIndex, int size) {
155        System.arraycopy(data, fromIndex, dest, destIndex, size);
156    }
157
158}
159