1/*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package jdk.vm.ci.meta;
24
25/**
26 * Represents a logic value that can be either {@link #TRUE}, {@link #FALSE}, or {@link #UNKNOWN}.
27 */
28public enum TriState {
29    TRUE,
30    FALSE,
31    UNKNOWN;
32
33    public static TriState get(boolean value) {
34        return value ? TRUE : FALSE;
35    }
36
37    /**
38     * This is optimistic about {@link #UNKNOWN} (it prefers known values over {@link #UNKNOWN}) and
39     * pesimistic about known (it perfers {@link #TRUE} over {@link #FALSE}).
40     */
41    public static TriState merge(TriState a, TriState b) {
42        if (a == TRUE || b == TRUE) {
43            return TRUE;
44        }
45        if (a == FALSE || b == FALSE) {
46            return FALSE;
47        }
48        assert a == UNKNOWN && b == UNKNOWN;
49        return UNKNOWN;
50    }
51
52    public boolean isTrue() {
53        return this == TRUE;
54    }
55
56    public boolean isFalse() {
57        return this == FALSE;
58    }
59
60    public boolean isUnknown() {
61        return this == UNKNOWN;
62    }
63
64    public boolean isKnown() {
65        return this != UNKNOWN;
66    }
67
68    public boolean toBoolean() {
69        if (isTrue()) {
70            return true;
71        } else if (isFalse()) {
72            return false;
73        } else {
74            throw new IllegalStateException("Cannot convert to boolean, TriState is in an unknown state");
75        }
76    }
77}
78