src/core/net/sf/basedb/util/formatter/BooleanFormatter.java

Code
Comments
Other
Rev Date Author Line
2733 16 Oct 06 nicklas 1 /**
2733 16 Oct 06 nicklas 2   $Id$
2733 16 Oct 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
2733 16 Oct 06 nicklas 5
2733 16 Oct 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2733 16 Oct 06 nicklas 7   Available at http://base.thep.lu.se/
2733 16 Oct 06 nicklas 8
2733 16 Oct 06 nicklas 9   BASE is free software; you can redistribute it and/or
2733 16 Oct 06 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
2733 16 Oct 06 nicklas 12   of the License, or (at your option) any later version.
2733 16 Oct 06 nicklas 13
2733 16 Oct 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2733 16 Oct 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2733 16 Oct 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2733 16 Oct 06 nicklas 17   GNU General Public License for more details.
2733 16 Oct 06 nicklas 18
2733 16 Oct 06 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2733 16 Oct 06 nicklas 21 */
2942 22 Nov 06 nicklas 22 package net.sf.basedb.util.formatter;
2733 16 Oct 06 nicklas 23
2942 22 Nov 06 nicklas 24 import net.sf.basedb.util.Values;
7649 14 Mar 19 nicklas 25 import net.sf.basedb.util.excel.ExcelFormatter;
7649 14 Mar 19 nicklas 26 import net.sf.basedb.util.excel.ExcelValue;
2942 22 Nov 06 nicklas 27
2733 16 Oct 06 nicklas 28 /**
2733 16 Oct 06 nicklas 29   Formats a boolean value. It is possible to select between yes/no, true/false, and 1/0
7649 14 Mar 19 nicklas 30   mappings. 
2733 16 Oct 06 nicklas 31
2733 16 Oct 06 nicklas 32   @author nicklas
2733 16 Oct 06 nicklas 33   @version 2.0
2733 16 Oct 06 nicklas 34   @base.modified $Date$
2733 16 Oct 06 nicklas 35 */
2733 16 Oct 06 nicklas 36 public class BooleanFormatter
7649 14 Mar 19 nicklas 37   implements Formatter<Boolean>, ExcelFormatter<Boolean, Boolean>
2733 16 Oct 06 nicklas 38 {
2733 16 Oct 06 nicklas 39
2733 16 Oct 06 nicklas 40   private final Type type;
2733 16 Oct 06 nicklas 41
2733 16 Oct 06 nicklas 42   /**
2733 16 Oct 06 nicklas 43     Create a new boolean formatter.
2733 16 Oct 06 nicklas 44     @param type The type of output
2733 16 Oct 06 nicklas 45   */
2733 16 Oct 06 nicklas 46   public BooleanFormatter(Type type)
2733 16 Oct 06 nicklas 47   {
2733 16 Oct 06 nicklas 48     this.type = type;
2733 16 Oct 06 nicklas 49   }
2733 16 Oct 06 nicklas 50   
2733 16 Oct 06 nicklas 51   /*
2733 16 Oct 06 nicklas 52     From the Formatter interface
2733 16 Oct 06 nicklas 53     -------------------------------------------
2733 16 Oct 06 nicklas 54   */
6127 14 Sep 12 nicklas 55   @Override
2733 16 Oct 06 nicklas 56   public String format(Boolean value)
2733 16 Oct 06 nicklas 57   {
2733 16 Oct 06 nicklas 58     return value == null ? "" : type.getString(value);
2733 16 Oct 06 nicklas 59   }
6127 14 Sep 12 nicklas 60   @Override
2942 22 Nov 06 nicklas 61   public Boolean parseString(String value)
2942 22 Nov 06 nicklas 62   {
2942 22 Nov 06 nicklas 63     return Values.getBoolean(value);
2942 22 Nov 06 nicklas 64   }
2733 16 Oct 06 nicklas 65   // -------------------------------------------
2733 16 Oct 06 nicklas 66   
2733 16 Oct 06 nicklas 67   /**
7649 14 Mar 19 nicklas 68     @since 3.15
7649 14 Mar 19 nicklas 69   */
7649 14 Mar 19 nicklas 70   @Override
7649 14 Mar 19 nicklas 71   public ExcelValue<Boolean> toExcelValue(Boolean value) 
7649 14 Mar 19 nicklas 72   {
7649 14 Mar 19 nicklas 73     return ExcelValue.asBoolean(value);
7649 14 Mar 19 nicklas 74   }
7649 14 Mar 19 nicklas 75
7649 14 Mar 19 nicklas 76   /**
2733 16 Oct 06 nicklas 77     Enum for holding the various types of boolean output.
2733 16 Oct 06 nicklas 78   */
2733 16 Oct 06 nicklas 79   public enum Type
2733 16 Oct 06 nicklas 80   {
2733 16 Oct 06 nicklas 81     /**
2733 16 Oct 06 nicklas 82       Display yes/no.
2733 16 Oct 06 nicklas 83     */
2733 16 Oct 06 nicklas 84     YES_NO("yes", "no"),
2733 16 Oct 06 nicklas 85
2733 16 Oct 06 nicklas 86     /**
2733 16 Oct 06 nicklas 87       Display true/false.
2733 16 Oct 06 nicklas 88     */
2733 16 Oct 06 nicklas 89     TRUE_FALSE("true", "false"),
2733 16 Oct 06 nicklas 90
2733 16 Oct 06 nicklas 91     /**
2733 16 Oct 06 nicklas 92       Display 1/0.
2733 16 Oct 06 nicklas 93     */
2733 16 Oct 06 nicklas 94     ONE_ZERO("1", "0");
2733 16 Oct 06 nicklas 95     
2733 16 Oct 06 nicklas 96     private final String trueValue;
2733 16 Oct 06 nicklas 97     private final String falseValue;
2733 16 Oct 06 nicklas 98     
2733 16 Oct 06 nicklas 99     Type(String trueValue, String falseValue)
2733 16 Oct 06 nicklas 100     {
2733 16 Oct 06 nicklas 101       this.trueValue = trueValue;
2733 16 Oct 06 nicklas 102       this.falseValue = falseValue;
2733 16 Oct 06 nicklas 103     }
2733 16 Oct 06 nicklas 104     
2733 16 Oct 06 nicklas 105     public String getString(boolean value)
2733 16 Oct 06 nicklas 106     {
2733 16 Oct 06 nicklas 107       return value ? trueValue : falseValue;
2733 16 Oct 06 nicklas 108     }
2733 16 Oct 06 nicklas 109   }
7649 14 Mar 19 nicklas 110
2733 16 Oct 06 nicklas 111 }