001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.resources;
018:
019: // J2SE dependencies
020: import java.io.Serializable;
021: import java.util.AbstractList;
022:
023: /**
024: * An unmodifiable view of an array. Invoking
025: *
026: * <blockquote><code>
027: * new UnmodifiableArrayList(array);
028: * </code></blockquote>
029: *
030: * is equivalent to
031: *
032: * <blockquote><code>
033: * {@linkplain Collections#unmodifiableList Collections.unmodifiableList}({@linkplain
034: * Arrays#asList Arrays.asList}(array)));
035: * </code></blockquote>
036: *
037: * But this class provides a very slight performance improvement since it uses one less level
038: * of indirection.
039: *
040: * @since 2.1
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/resources/UnmodifiableArrayList.java $
042: * @version $Id: UnmodifiableArrayList.java 23632 2006-12-29 22:13:51Z desruisseaux $
043: * @author Martin Desruisseaux
044: */
045: public class UnmodifiableArrayList extends AbstractList implements
046: Serializable {
047: /**
048: * For compatibility with different versions.
049: */
050: private static final long serialVersionUID = -3605810209653785967L;
051:
052: /**
053: * The wrapped array.
054: */
055: private final Object[] array;
056:
057: /**
058: * Create a new instance of an array list.
059: * The array given in argument is <strong>not</strong> cloned.
060: */
061: public UnmodifiableArrayList(final Object[] array) {
062: this .array = array;
063: }
064:
065: /**
066: * Returns the list size.
067: */
068: public int size() {
069: return array.length;
070: }
071:
072: /**
073: * Returns the element at the specified index.
074: */
075: public Object get(final int index) {
076: return array[index];
077: }
078:
079: /**
080: * Returns the index in this list of the first occurence of the specified
081: * element, or -1 if the list does not contain this element. This method
082: * is overridden only for performance reason (the default implementation
083: * would work as well).
084: */
085: public int indexOf(final Object object) {
086: if (object == null) {
087: for (int i = 0; i < array.length; i++) {
088: if (array[i] == null) {
089: return i;
090: }
091: }
092: } else {
093: for (int i = 0; i < array.length; i++) {
094: if (object.equals(array[i])) {
095: return i;
096: }
097: }
098: }
099: return -1;
100: }
101:
102: /**
103: * Returns the index in this list of the last occurence of the specified
104: * element, or -1 if the list does not contain this element. This method
105: * is overridden only for performance reason (the default implementation
106: * would work as well).
107: */
108: public int lastIndexOf(final Object object) {
109: int i = array.length;
110: if (object == null) {
111: while (--i >= 0) {
112: if (array[i] == null) {
113: break;
114: }
115: }
116: } else {
117: while (--i >= 0) {
118: if (object.equals(array[i])) {
119: break;
120: }
121: }
122: }
123: return i;
124: }
125:
126: /**
127: * Returns {@code true} if this collection contains the specified element.
128: * This method is overridden only for performance reason (the default implementation
129: * would work as well).
130: */
131: public boolean contains(final Object object) {
132: int i = array.length;
133: if (object == null) {
134: while (--i >= 0) {
135: if (array[i] == null) {
136: return true;
137: }
138: }
139: } else {
140: while (--i >= 0) {
141: if (object.equals(array[i])) {
142: return true;
143: }
144: }
145: }
146: return false;
147: }
148: }
|