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

Code
Comments
Other
Rev Date Author Line
5664 23 Jun 11 nicklas 1 /**
5664 23 Jun 11 nicklas 2   $Id$
5664 23 Jun 11 nicklas 3
5664 23 Jun 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5664 23 Jun 11 nicklas 5
5664 23 Jun 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5664 23 Jun 11 nicklas 7   Available at http://base.thep.lu.se/
5664 23 Jun 11 nicklas 8
5664 23 Jun 11 nicklas 9   BASE is free software; you can redistribute it and/or
5664 23 Jun 11 nicklas 10   modify it under the terms of the GNU General Public License
5664 23 Jun 11 nicklas 11   as published by the Free Software Foundation; either version 3
5664 23 Jun 11 nicklas 12   of the License, or (at your option) any later version.
5664 23 Jun 11 nicklas 13
5664 23 Jun 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5664 23 Jun 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5664 23 Jun 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5664 23 Jun 11 nicklas 17   GNU General Public License for more details.
5664 23 Jun 11 nicklas 18
5664 23 Jun 11 nicklas 19   You should have received a copy of the GNU General Public License
5664 23 Jun 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5664 23 Jun 11 nicklas 21 */
5664 23 Jun 11 nicklas 22 package net.sf.basedb.util.formatter;
5664 23 Jun 11 nicklas 23
5664 23 Jun 11 nicklas 24 import net.sf.basedb.core.Item;
5664 23 Jun 11 nicklas 25
5664 23 Jun 11 nicklas 26 /**
5664 23 Jun 11 nicklas 27   Formatter implementation that converts an integer into it's Item
5664 23 Jun 11 nicklas 28   representation and the uses name() as the formatted value.
5664 23 Jun 11 nicklas 29
5664 23 Jun 11 nicklas 30   @author Nicklas
5664 23 Jun 11 nicklas 31   @since 3.0
5664 23 Jun 11 nicklas 32   @base.modified $Date$
5664 23 Jun 11 nicklas 33 */
5664 23 Jun 11 nicklas 34 public class ItemTypeFormatter
5664 23 Jun 11 nicklas 35   implements Formatter<Integer>
5664 23 Jun 11 nicklas 36 {
5664 23 Jun 11 nicklas 37   private String nullValue = "";
5664 23 Jun 11 nicklas 38
5664 23 Jun 11 nicklas 39   /**
5664 23 Jun 11 nicklas 40     Create a new formatter. Null values are formatted as an empty string.
5664 23 Jun 11 nicklas 41   */
5664 23 Jun 11 nicklas 42   public ItemTypeFormatter()
5664 23 Jun 11 nicklas 43   {}
5664 23 Jun 11 nicklas 44
5664 23 Jun 11 nicklas 45   /**
5664 23 Jun 11 nicklas 46     Create a new formatter.
5664 23 Jun 11 nicklas 47     @param nullValue The string to return if a null item is 
5664 23 Jun 11 nicklas 48       passed to {@link #format(Integer)}
5664 23 Jun 11 nicklas 49   */
5664 23 Jun 11 nicklas 50   public ItemTypeFormatter(String nullValue)
5664 23 Jun 11 nicklas 51   {
5664 23 Jun 11 nicklas 52     this.nullValue = nullValue;
5664 23 Jun 11 nicklas 53   }
5664 23 Jun 11 nicklas 54
5664 23 Jun 11 nicklas 55   /*
5664 23 Jun 11 nicklas 56     From the Formatter interface
5664 23 Jun 11 nicklas 57     -------------------------------------------
5664 23 Jun 11 nicklas 58   */
5664 23 Jun 11 nicklas 59   @Override
5664 23 Jun 11 nicklas 60   public String format(Integer value)
5664 23 Jun 11 nicklas 61   {
5664 23 Jun 11 nicklas 62     Item item = value == null ? null : Item.fromValue(value);
5664 23 Jun 11 nicklas 63     return item == null ? nullValue : item.name(); 
5664 23 Jun 11 nicklas 64   }
5664 23 Jun 11 nicklas 65
5664 23 Jun 11 nicklas 66   @Override
5664 23 Jun 11 nicklas 67   public Integer parseString(String value)
5664 23 Jun 11 nicklas 68   {
5664 23 Jun 11 nicklas 69     try
5664 23 Jun 11 nicklas 70     {
5664 23 Jun 11 nicklas 71       return Item.valueOf(value).getValue();
5664 23 Jun 11 nicklas 72     }
5664 23 Jun 11 nicklas 73     catch (Exception ex)
5664 23 Jun 11 nicklas 74     {}
5664 23 Jun 11 nicklas 75     return null;
5664 23 Jun 11 nicklas 76   }
5664 23 Jun 11 nicklas 77   // -------------------------------------------
5664 23 Jun 11 nicklas 78     
5664 23 Jun 11 nicklas 79   
5664 23 Jun 11 nicklas 80   
5664 23 Jun 11 nicklas 81 }