uk.org.retep.util.state.TriState Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2007, Peter T Mount
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the retep.org.uk nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package uk.org.retep.util.state;
import uk.org.retep.util.string.StringUtils;
/**
* A Boolean that can be True, False or Default...
*
* @author peter
*/
public enum TriState
{
/**
* The DEFAULT value
*/
DEFAULT,
/**
* FALSE
*/
FALSE,
/**
* TRUE
*/
TRUE;
/**
* Boolean value. If default then returns false
* @return boolean value, false if default
*/
public boolean booleanValue()
{
return booleanValue( false );
}
/**
* Boolean value
* @param defaultValue value to return when DEFAULT
* @return boolean value, defaultValue if DEFAULT
*/
public boolean booleanValue( boolean defaultValue )
{
switch( this )
{
case FALSE:
return false;
case TRUE:
return true;
default:
return defaultValue;
}
}
/**
* Boolean value
* @param defaultValue TriState to use as the default
* @return boolean value, defaultValue if DEFAULT
*/
public boolean booleanValue( TriState defaultValue )
{
switch( this )
{
case FALSE:
return false;
case TRUE:
return true;
default:
return defaultValue.booleanValue( false );
}
}
/**
* Convert the String into a TriState
* @param s String to parse
* @return TriState
* @deprecated use {@link #parseBoolean(java.lang.String) }
*/
public static TriState valueOfDefault( String s )
{
try
{
return TriState.valueOf( s );
}
catch( Exception e )
{
return TriState.DEFAULT;
}
}
/**
* TriState representing the boolean
* @param b Boolean
* @return TriState
*/
public static TriState valueOf( boolean b )
{
return b ? TriState.TRUE : TriState.FALSE;
}
/**
* TriState representing the boolean
* @param b Boolean
* @return TriState, DEFAULT if b is null
*/
public static TriState valueOf( Boolean b )
{
return b == null ? TriState.DEFAULT : valueOf( b.booleanValue() );
}
/**
* Return one of the values depending on the state
*
* @param Type of the values being chosen
* @param t true string
* @param f false string
* @param d d
* @return t, f or d depending on the state
*/
public T choice( T t, T f, T d )
{
return d;
}
/**
* Return the state "true", "false" or the provided string for default
* @param d default string
* @return "true", "false" or d depending on the state
*/
public final String choice( String d )
{
return choice( "true", "false", d );
}
/**
* Return the state "yes", "no" or the provided string for default
* @param d default string
* @return "yes", "no" or d depending on the state
*/
public final String choiceYN( String d )
{
return choice( "yes", "no", d );
}
/**
* Parse the string for a TriState. The value returned is:
*
* - {@link #TRUE} if s starts with one of t, T, y, Y or 1
* - {@link #FALSE} if s starts with one of f, F, n, N or 0
* - {@link #DEFAULT} for any other value
*
*
* @param s String to parse
* @return TriState
* @since 9.10
*/
public static TriState parseBoolean( String s )
{
if( StringUtils.isStringEmpty( s ) )
{
return TriState.DEFAULT;
}
final char c = s.charAt( 0 );
if( c == 't' || c == 'T' || c == 'y' || c == 'Y' || c == '1' )
{
return TriState.TRUE;
}
else if( c == 'f' || c == 'F' || c == 'n' || c == 'N' || c == '0' )
{
return TriState.FALSE;
}
else
{
return TriState.DEFAULT;
}
}
}