1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.rop.cst;
18
19import com.android.dx.util.ToHuman;
20
21/**
22 * Base class for constants of all sorts.
23 */
24public abstract class Constant
25        implements ToHuman, Comparable<Constant> {
26    /**
27     * Returns {@code true} if this instance is a category-2 constant,
28     * meaning it takes up two slots in the constant pool, or
29     * {@code false} if this instance is category-1.
30     *
31     * @return {@code true} iff this instance is category-2
32     */
33    public abstract boolean isCategory2();
34
35    /**
36     * Returns the human name for the particular type of constant
37     * this instance is.
38     *
39     * @return {@code non-null;} the name
40     */
41    public abstract String typeName();
42
43    /**
44     * {@inheritDoc}
45     *
46     * This compares in class-major and value-minor order.
47     */
48    public final int compareTo(Constant other) {
49        Class clazz = getClass();
50        Class otherClazz = other.getClass();
51
52        if (clazz != otherClazz) {
53            return clazz.getName().compareTo(otherClazz.getName());
54        }
55
56        return compareTo0(other);
57    }
58
59    /**
60     * Compare the values of this and another instance, which are guaranteed
61     * to be of the same class. Subclasses must implement this.
62     *
63     * @param other {@code non-null;} the instance to compare to
64     * @return {@code -1}, {@code 0}, or {@code 1}, as usual
65     * for a comparison
66     */
67    protected abstract int compareTo0(Constant other);
68}
69