Path: blob/master/src/java.desktop/share/classes/sun/swing/BakedArrayList.java
41153 views
/*1* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing;2526import java.util.*;2728/**29* <b>WARNING:</b> This class is an implementation detail and is only30* public so that it can be used by two packages. You should NOT consider31* this public API.32* <p>33* <b>WARNING 2:</b> This is not a general purpose List implementation! It34* has a specific use and will not work correctly if you use it outside of35* its use.36* <p>37* A specialized ArrayList that caches its hashCode as well as overriding38* equals to avoid creating an Iterator. This class is useful in scenarios39* where the list won't change and you want to avoid the overhead of hashCode40* iterating through the elements invoking hashCode. This also assumes you'll41* only ever compare a BakedArrayList to another BakedArrayList.42*43* @author Scott Violet44*/45@SuppressWarnings("serial") // JDK-implementation class46public class BakedArrayList<E> extends ArrayList<E> {47/**48* The cached hashCode.49*/50private int _hashCode;5152public BakedArrayList(int size) {53super(size);54}5556public BakedArrayList(java.util.List<? extends E> data) {57this(data.size());58for (int counter = 0, max = data.size(); counter < max; counter++){59add(data.get(counter));60}61cacheHashCode();62}6364/**65* Caches the hash code. It is assumed you won't modify the list, or that66* if you do you'll call cacheHashCode again.67*/68public void cacheHashCode() {69_hashCode = 1;70for (int counter = size() - 1; counter >= 0; counter--) {71_hashCode = 31 * _hashCode + get(counter).hashCode();72}73}7475public int hashCode() {76return _hashCode;77}7879public boolean equals(Object o) {80@SuppressWarnings("unchecked")81BakedArrayList<E> list = (BakedArrayList)o;82int size = size();8384if (list.size() != size) {85return false;86}87while (size-- > 0) {88if (!get(size).equals(list.get(size))) {89return false;90}91}92return true;93}94}959697