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.rop.type.Type;
20
21/**
22 * Constants of type {@code boolean}.
23 */
24public final class CstBoolean
25        extends CstLiteral32 {
26    /** {@code non-null;} instance representing {@code false} */
27    public static final CstBoolean VALUE_FALSE = new CstBoolean(false);
28
29    /** {@code non-null;} instance representing {@code true} */
30    public static final CstBoolean VALUE_TRUE = new CstBoolean(true);
31
32    /**
33     * Makes an instance for the given value. This will return an
34     * already-allocated instance.
35     *
36     * @param value the {@code boolean} value
37     * @return {@code non-null;} the appropriate instance
38     */
39    public static CstBoolean make(boolean value) {
40        return value ? VALUE_TRUE : VALUE_FALSE;
41    }
42
43    /**
44     * Makes an instance for the given {@code int} value. This
45     * will return an already-allocated instance.
46     *
47     * @param value must be either {@code 0} or {@code 1}
48     * @return {@code non-null;} the appropriate instance
49     */
50    public static CstBoolean make(int value) {
51        if (value == 0) {
52            return VALUE_FALSE;
53        } else if (value == 1) {
54            return VALUE_TRUE;
55        } else {
56            throw new IllegalArgumentException("bogus value: " + value);
57        }
58    }
59
60    /**
61     * Constructs an instance. This constructor is private; use {@link #make}.
62     *
63     * @param value the {@code boolean} value
64     */
65    private CstBoolean(boolean value) {
66        super(value ? 1 : 0);
67    }
68
69    /** {@inheritDoc} */
70    @Override
71    public String toString() {
72        return getValue() ? "boolean{true}" : "boolean{false}";
73    }
74
75    /** {@inheritDoc} */
76    public Type getType() {
77        return Type.BOOLEAN;
78    }
79
80    /** {@inheritDoc} */
81    @Override
82    public String typeName() {
83        return "boolean";
84    }
85
86    /** {@inheritDoc} */
87    public String toHuman() {
88        return getValue() ? "true" : "false";
89    }
90
91    /**
92     * Gets the {@code boolean} value.
93     *
94     * @return the value
95     */
96    public boolean getValue() {
97        return (getIntBits() == 0) ? false : true;
98    }
99}
100