BooleanCodec.java :  » Game » rugl » com » ryanm » config » serial » imp » Java Open Source

Java Open Source » Game » rugl 
rugl » com » ryanm » config » serial » imp » BooleanCodec.java

package com.ryanm.config.serial.imp;

import com.ryanm.config.serial.ConfiguratorCodec;

/**
 * @author s9902505
 */
public class BooleanCodec implements ConfiguratorCodec<Boolean>
{
  private static final String[] yes = new String[] { "true", "1", "yes", "y", "t" };

  private static String description = null;

  @Override
  public String encode( Boolean value )
  {
    return value.toString();
  }

  @Override
  public Boolean decode( String encoded, Class type )
  {
    assert type.equals( getType() );

    encoded = encoded.toLowerCase();

    for( int i = 0; i < yes.length; i++ )
    {
      if( encoded.equals( yes[ i ] ) )
      {
        return new Boolean( true );
      }
    }

    return new Boolean( false );
  }

  @Override
  public Class getType()
  {
    return boolean.class;
  }

  @Override
  public String getDescription()
  {
    if( description == null )
    {
      StringBuilder buff = new StringBuilder( "\"" );
      buff.append( yes[ 0 ] );

      for( int i = 0; i < yes.length; i++ )
      {
        buff.append( "\", \"" );
        buff.append( yes[ i ] );
      }

      buff.append( "\" for true, or anything else for false" );
    }

    return description;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.