1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.evaluation.value;
22
23import proguard.classfile.Clazz;
24
25/**
26 * This LongValue represents a reference value that is identified by a unique ID.
27 *
28 * @author Eric Lafortune
29 */
30final class IdentifiedReferenceValue extends ReferenceValue
31{
32    private final ValueFactory valuefactory;
33    private final int          id;
34
35
36    /**
37     * Creates a new long value with the given ID.
38     */
39    public IdentifiedReferenceValue(String       type,
40                                    Clazz        referencedClass,
41                                    boolean      mayBeNull,
42                                    ValueFactory valuefactory,
43                                    int          id)
44    {
45        super(type, referencedClass, mayBeNull);
46
47        this.valuefactory = valuefactory;
48        this.id           = id;
49    }
50
51
52    // Implementations for ReferenceValue.
53
54    public int equal(ReferenceValue other)
55    {
56        return this.equals(other) ? ALWAYS : MAYBE;
57    }
58
59
60    // Implementations of binary methods of ReferenceValue.
61
62    public ReferenceValue generalize(ReferenceValue other)
63    {
64        // Remove the ID if both values don't share the same ID.
65        return this.equals(other) ?
66            this :
67            new ReferenceValue(type, referencedClass, mayBeNull).generalize(other);
68    }
69
70
71    // Implementations for Value.
72
73    public boolean isSpecific()
74    {
75        return true;
76    }
77
78
79    // Implementations for Object.
80
81    public boolean equals(Object object)
82    {
83        return this == object ||
84               super.equals(object) &&
85               this.valuefactory.equals(((IdentifiedReferenceValue)object).valuefactory) &&
86               this.id == ((IdentifiedReferenceValue)object).id;
87    }
88
89
90    public int hashCode()
91    {
92        return super.hashCode() ^
93               valuefactory.hashCode() ^
94               id;
95    }
96
97
98    public String toString()
99    {
100        return super.toString()+'#'+id;
101    }
102}