1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2010 Ben Gruver (JesusFreke)
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.AnnotatedOutput;
33import org.jf.dexlib.Util.Input;
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 internEncodedArrayItem(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        encodedArray.writeValue(out);
83    }
84
85    /** {@inheritDoc} */
86    public ItemType getItemType() {
87        return ItemType.TYPE_ENCODED_ARRAY_ITEM;
88    }
89
90    /** {@inheritDoc} */
91    public String getConciseIdentity() {
92        return "encoded_array @0x" + Integer.toHexString(getOffset());
93    }
94
95    /** {@inheritDoc} */
96    public int compareTo(EncodedArrayItem encodedArrayItem) {
97        return encodedArray.compareTo(encodedArrayItem.encodedArray);
98    }
99
100    /**
101     * @return The encoded array value
102     */
103    public ArrayEncodedSubValue getEncodedArray() {
104        return encodedArray;
105    }
106
107    /**
108     * calculate and cache the hashcode
109     */
110    private void calcHashCode() {
111        hashCode = encodedArray.hashCode();
112    }
113
114    @Override
115    public int hashCode() {
116        //there's a small possibility that the actual hash code will be 0. If so, we'll
117        //just end up recalculating it each time
118        if (hashCode == 0)
119            calcHashCode();
120        return hashCode;
121    }
122
123    @Override
124    public boolean equals(Object o) {
125        if (this==o) {
126            return true;
127        }
128        if (o==null || !this.getClass().equals(o.getClass())) {
129            return false;
130        }
131
132        EncodedArrayItem other = (EncodedArrayItem)o;
133        return (encodedArray.compareTo(other.encodedArray) == 0);
134    }
135}
136