Package de.odysseus.calyxo.forms.convert

Source Code of de.odysseus.calyxo.forms.convert.BooleanConverter

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.convert;

import java.text.ParseException;

/**
* Boolean converter.
*
* @author Christoph Beck
*/
public class BooleanConverter extends SimpleConverter {
  private String trueString;
  private String falseString;
  private Boolean parsedNullOrEmpty;

  /**
   * Default constructor.
   */
  public BooleanConverter() {
    this("true", "false");
  }

  /**
   * Constructor.
   */
  public BooleanConverter(String trueValue, String falseValue) {
    this.trueString = trueValue;
    this.falseString = falseValue;
  }

  /**
   * Format method.
   */
  public String format(Object value) {
    if (value == null) {
      return null;
    }
    return ((Boolean) value).booleanValue() ? trueString : falseString;
  }

  /**
   * Get the falseValue property
   */
  public String getFalseString() {
    return falseString;
  }

  /**
   * Get the default property
   */
  public Boolean getDefault() {
    return parsedNullOrEmpty;
  }

  /**
   * Get the trueValue property
   */
  public String getTrueString() {
    return trueString;
  }

  /**
   * Parse method.
   */
  public Object parse(String value) throws ParseException {
    if (value == null || value.length() == 0)
      return parsedNullOrEmpty;
    if (value.equals(trueString))
      return Boolean.TRUE;
    if (value.equals(falseString))
      return Boolean.FALSE;
    throw new ParseException("Cannot parse: " + value, 0);
  }

  /**
   * Set the falseValue property
   */
  public void setFalseString(String newFalseString) {
    falseString = newFalseString;
  }

  /**
   * Set the default property
   */
  public void setDefault(Boolean value) {
    parsedNullOrEmpty = value;
  }

  /**
   * Set the trueValue property
   */
  public void setTrueString(String newTrueString) {
    trueString = newTrueString;
  }

  /**
   * This converter is thread-save, so answer <code>true</code>
   */
  public boolean isSharable() {
    return true;
  }
}
TOP

Related Classes of de.odysseus.calyxo.forms.convert.BooleanConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.