001    package edu.rice.cs.cunit.classFile.constantPool;
002    
003    import edu.rice.cs.cunit.classFile.constantPool.APoolInfo;
004    
005    import java.util.ArrayList;
006    import java.util.Collection;
007    
008    /**
009     * Constant pool.
010     *
011     * @author Mathias Ricken
012     */
013    public class ConstantPool extends ArrayList<APoolInfo> {
014        /**
015         * Constructs an empty constant pool with the specified initial capacity.
016         *
017         * @param initialCapacity the initial capacity of the constant pool.
018         *
019         * @throws IllegalArgumentException if the specified initial capacity is negative
020         */
021        public ConstantPool(int initialCapacity) {
022            super(initialCapacity);
023        }
024    
025        /**
026         * Constructs an empty constant pool with an initial capacity of ten.
027         */
028        public ConstantPool() {
029        }
030    
031        /**
032         * Constructs a constant pool containing the elements of the specified collection, in the order they are returned
033         * by the collection's iterator.  The <tt>ConstantPool</tt> instance has an initial capacity of 110% the size of
034         * the specified collection.
035         *
036         * @param aPoolInfos the collection whose elements are to be placed into this constant pool.
037         *
038         * @throws NullPointerException if the specified collection is null.
039         */
040        public ConstantPool(Collection<? extends APoolInfo> aPoolInfos) {
041            super(aPoolInfos);
042        }
043    
044        /**
045         * Return the index of the pool item in the pool.
046         *
047         * @param item          item
048         *
049         * @return index
050         */
051        public short indexOf(APoolInfo item) {
052            for(int i = 0; i < size(); i++) {
053                if (item == get(i)) {
054                    return (short)i;
055                }
056            }
057            throw new AssertionError("Item not in pool: "+item.toStringVerbose());
058        }
059    }