001/* 002 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package jdk.internal.jvmci.meta; 024 025/** 026 * Represents a logic value that can be either {@link #TRUE}, {@link #FALSE}, or {@link #UNKNOWN}. 027 */ 028public enum TriState { 029 TRUE, 030 FALSE, 031 UNKNOWN; 032 033 public static TriState get(boolean value) { 034 return value ? TRUE : FALSE; 035 } 036 037 /** 038 * This is optimistic about {@link #UNKNOWN} (it prefers known values over {@link #UNKNOWN}) and 039 * pesimistic about known (it perfers {@link #TRUE} over {@link #FALSE}). 040 */ 041 public static TriState merge(TriState a, TriState b) { 042 if (a == TRUE || b == TRUE) { 043 return TRUE; 044 } 045 if (a == FALSE || b == FALSE) { 046 return FALSE; 047 } 048 assert a == UNKNOWN && b == UNKNOWN; 049 return UNKNOWN; 050 } 051 052 public boolean isTrue() { 053 return this == TRUE; 054 } 055 056 public boolean isFalse() { 057 return this == FALSE; 058 } 059 060 public boolean isUnknown() { 061 return this == UNKNOWN; 062 } 063 064 public boolean isKnown() { 065 return this != UNKNOWN; 066 } 067 068 public boolean toBoolean() { 069 if (isTrue()) { 070 return true; 071 } else if (isFalse()) { 072 return false; 073 } else { 074 throw new IllegalStateException("Cannot convert to boolean, TriState is in an unknown state"); 075 } 076 } 077}