1/*
2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package sun.swing;
26
27import java.util.*;
28
29/**
30 * <b>WARNING:</b> This class is an implementation detail and is only
31 * public so that it can be used by two packages. You should NOT consider
32 * this public API.
33 * <p>
34 * <b>WARNING 2:</b> This is not a general purpose List implementation! It
35 * has a specific use and will not work correctly if you use it outside of
36 * its use.
37 * <p>
38 * A specialized ArrayList that caches its hashCode as well as overriding
39 * equals to avoid creating an Iterator. This class is useful in scenarios
40 * where the list won't change and you want to avoid the overhead of hashCode
41 * iterating through the elements invoking hashCode. This also assumes you'll
42 * only ever compare a BakedArrayList to another BakedArrayList.
43 *
44 * @author Scott Violet
45 */
46@SuppressWarnings("serial") // JDK-implementation class
47public class BakedArrayList<E> extends ArrayList<E> {
48    /**
49     * The cached hashCode.
50     */
51    private int _hashCode;
52
53    public BakedArrayList(int size) {
54        super(size);
55    }
56
57    public BakedArrayList(java.util.List<? extends E> data) {
58        this(data.size());
59        for (int counter = 0, max = data.size(); counter < max; counter++){
60            add(data.get(counter));
61        }
62        cacheHashCode();
63    }
64
65    /**
66     * Caches the hash code. It is assumed you won't modify the list, or that
67     * if you do you'll call cacheHashCode again.
68     */
69    public void cacheHashCode() {
70        _hashCode = 1;
71        for (int counter = size() - 1; counter >= 0; counter--) {
72            _hashCode = 31 * _hashCode + get(counter).hashCode();
73        }
74    }
75
76    public int hashCode() {
77        return _hashCode;
78    }
79
80    public boolean equals(Object o) {
81        @SuppressWarnings("unchecked")
82        BakedArrayList<E> list = (BakedArrayList)o;
83        int size = size();
84
85        if (list.size() != size) {
86            return false;
87        }
88        while (size-- > 0) {
89            if (!get(size).equals(list.get(size))) {
90                return false;
91            }
92        }
93        return true;
94    }
95}
96