EncodedArrayItem.java revision 83b80f81d311b233188c281059aad4a9f5e8b4e6
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2009 Ben Gruver
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.dexlib;
30
31import org.jf.dexlib.EncodedValue.ArrayEncodedSubValue;
32import org.jf.dexlib.Util.Input;
33import org.jf.dexlib.Util.AnnotatedOutput;
34
35public class EncodedArrayItem extends Item<EncodedArrayItem> {
36    private int hashCode = 0;
37
38    private ArrayEncodedSubValue encodedArray;
39
40    /**
41     * Creates a new uninitialized <code>EncodedArrayItem</code>
42     * @param dexFile The <code>DexFile</code> that this item belongs to
43     */
44    protected EncodedArrayItem(DexFile dexFile) {
45        super(dexFile);
46    }
47
48    /**
49     * Creates a new <code>EncodedArrayItem</code> with the given values
50     * @param dexFile The <code>DexFile</code> that this item belongs to
51     * @param encodedArray The encoded array value
52     */
53    private EncodedArrayItem(DexFile dexFile, ArrayEncodedSubValue encodedArray) {
54        super(dexFile);
55        this.encodedArray = encodedArray;
56    }
57
58    /**
59     * Returns an <code>EncodedArrayItem</code> for the given values, and that has been interned into the given
60     * <code>DexFile</code>
61     * @param dexFile The <code>DexFile</code> that this item belongs to
62     * @param encodedArray The encoded array value
63     * @return an <code>EncodedArrayItem</code> for the given values, and that has been interned into the given
64     */
65    public static EncodedArrayItem getInternedEncodedArrayItem(DexFile dexFile, ArrayEncodedSubValue encodedArray) {
66        EncodedArrayItem encodedArrayItem = new EncodedArrayItem(dexFile, encodedArray);
67        return dexFile.EncodedArraysSection.intern(encodedArrayItem);
68    }
69
70    /** {@inheritDoc} */
71    protected void readItem(Input in, ReadContext readContext) {
72        encodedArray = new ArrayEncodedSubValue(dexFile, in);
73    }
74
75    /** {@inheritDoc} */
76    protected int placeItem(int offset) {
77        return encodedArray.placeValue(offset);
78    }
79
80    /** {@inheritDoc} */
81    protected void writeItem(AnnotatedOutput out) {
82        if (out.annotates()) {
83            out.annotate("encoded_array");
84        }
85        encodedArray.writeValue(out);
86    }
87
88    /** {@inheritDoc} */
89    public ItemType getItemType() {
90        return ItemType.TYPE_ENCODED_ARRAY_ITEM;
91    }
92
93    /** {@inheritDoc} */
94    public String getConciseIdentity() {
95        return "encoded_array @0x" + Integer.toHexString(getOffset());
96    }
97
98    /** {@inheritDoc} */
99    public int compareTo(EncodedArrayItem encodedArrayItem) {
100        return encodedArray.compareTo(encodedArrayItem.encodedArray);
101    }
102
103    /**
104     * @return The encoded array value
105     */
106    public ArrayEncodedSubValue getEncodedArray() {
107        return encodedArray;
108    }
109
110    /**
111     * calculate and cache the hashcode
112     */
113    private void calcHashCode() {
114        hashCode = encodedArray.hashCode();
115    }
116
117    @Override
118    public int hashCode() {
119        //there's a small possibility that the actual hash code will be 0. If so, we'll
120        //just end up recalculating it each time
121        if (hashCode == 0)
122            calcHashCode();
123        return hashCode;
124    }
125
126    @Override
127    public boolean equals(Object o) {
128        if (this==o) {
129            return true;
130        }
131        if (o==null || !this.getClass().equals(o.getClass())) {
132            return false;
133        }
134
135        EncodedArrayItem other = (EncodedArrayItem)o;
136        return (encodedArray.compareTo(other.encodedArray) == 0);
137    }
138}
139