Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/swing/BakedArrayList.java
38829 views
/*1* Copyright (c) 2003, 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*/45public class BakedArrayList extends ArrayList {46/**47* The cached hashCode.48*/49private int _hashCode;5051public BakedArrayList(int size) {52super(size);53}5455public BakedArrayList(java.util.List data) {56this(data.size());57for (int counter = 0, max = data.size(); counter < max; counter++){58add(data.get(counter));59}60cacheHashCode();61}6263/**64* Caches the hash code. It is assumed you won't modify the list, or that65* if you do you'll call cacheHashCode again.66*/67public void cacheHashCode() {68_hashCode = 1;69for (int counter = size() - 1; counter >= 0; counter--) {70_hashCode = 31 * _hashCode + get(counter).hashCode();71}72}7374public int hashCode() {75return _hashCode;76}7778public boolean equals(Object o) {79BakedArrayList list = (BakedArrayList)o;80int size = size();8182if (list.size() != size) {83return false;84}85while (size-- > 0) {86if (!get(size).equals(list.get(size))) {87return false;88}89}90return true;91}92}939495