KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > TriStateBool


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * TriStateBool.java
26  *
27  * Created on March 14, 2003, 5:21 PM
28  */

29
30 package com.sun.enterprise.util;
31
32 /**
33  *
34  * @author bnevins
35  */

36 /** This class is a Java Enum for a bool value that also has an "undefined" state.
37  **/

38
39 public class TriStateBool
40 {
41     private TriStateBool()
42     {
43     }
44
45     //////////////////////////////////////////////////////////////////////////
46

47     /** Convert a String into one of the three possible TriStateBool instances.
48      * It does a case-insensitive string comparison:
49      * <p>
50      * "true" --> TRUE
51      * "false" --> FALSE
52      * anything-else --> UNDEFINED
53      * @param s String representation of the TriStateBool
54      * @return One of the 3 possible values
55      */

56     public static final TriStateBool translate(String JavaDoc s)
57     {
58         if(StringUtils.ok(s))
59         {
60             s = s.toLowerCase();
61
62             if(s.equals("true"))
63                 return TRUE;
64             if(s.equals("false"))
65                 return FALSE;
66         }
67         return UNDEFINED;
68     }
69
70     //////////////////////////////////////////////////////////////////////////
71

72     public String JavaDoc toString()
73     {
74         if(this == TRUE)
75             return "TRUE";
76         else if(this == FALSE)
77             return "FALSE";
78         if(this == UNDEFINED)
79             return "UNDEFINED";
80         else
81             return "IMPOSSIBLE VALUE!!";
82     }
83     
84     //////////////////////////////////////////////////////////////////////////
85

86     public static final TriStateBool TRUE = new TriStateBool();
87     public static final TriStateBool FALSE = new TriStateBool();
88     public static final TriStateBool UNDEFINED = new TriStateBool();
89
90     //////////////////////////////////////////////////////////////////////////
91

92     public static void main(String JavaDoc[] args)
93     {
94         System.out.println("TruE: " + translate("TruE"));
95         System.out.println("FALse: " + translate("FALse"));
96         System.out.println("TruEX: " + translate("TruEX"));
97     }
98
99 }
100
Popular Tags