src/core/net/sf/basedb/util/parser/ColumnMapper.java

Code
Comments
Other
Rev Date Author Line
2203 28 Apr 06 nicklas 1 /*
2203 28 Apr 06 nicklas 2   $Id$
2203 28 Apr 06 nicklas 3   
3675 16 Aug 07 jari 4   Copyright (C) 2006, 2007 Nicklas Nordborg
2203 28 Apr 06 nicklas 5   
2203 28 Apr 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2203 28 Apr 06 nicklas 7   Available at http://base.thep.lu.se/
2203 28 Apr 06 nicklas 8   
2203 28 Apr 06 nicklas 9   BASE is free software; you can redistribute it and/or modify it
2203 28 Apr 06 nicklas 10   under the terms of the GNU General Public License as published by
4479 05 Sep 08 jari 11   the Free Software Foundation; either version 3 of the License, or
2203 28 Apr 06 nicklas 12   (at your option) any later version.
2203 28 Apr 06 nicklas 13   
2203 28 Apr 06 nicklas 14   BASE is distributed in the hope that it will be useful, but
2203 28 Apr 06 nicklas 15   WITHOUT ANY WARRANTY; without even the implied warranty of
2203 28 Apr 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2203 28 Apr 06 nicklas 17   General Public License for more details.
2203 28 Apr 06 nicklas 18   
2203 28 Apr 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/>.
2203 28 Apr 06 nicklas 21 */
2203 28 Apr 06 nicklas 22 package net.sf.basedb.util.parser;
2203 28 Apr 06 nicklas 23
3023 12 Dec 06 nicklas 24 import java.text.NumberFormat;
7667 21 Mar 19 nicklas 25 import java.util.Date;
3023 12 Dec 06 nicklas 26
7667 21 Mar 19 nicklas 27 import net.sf.basedb.util.formatter.Formatter;
2203 28 Apr 06 nicklas 28 import net.sf.basedb.util.parser.FlatFileParser.Data;
2203 28 Apr 06 nicklas 29
2203 28 Apr 06 nicklas 30 /**
2203 28 Apr 06 nicklas 31   Returns the string found in the column given by the index
2203 28 Apr 06 nicklas 32   set in the constructor. If the index is larger than the number
2203 28 Apr 06 nicklas 33   of columns, an empty string is returned.
2203 28 Apr 06 nicklas 34
2203 28 Apr 06 nicklas 35   @author nicklas
2203 28 Apr 06 nicklas 36   @version 2.0
2302 22 May 06 nicklas 37   @base.modified $Date$
2203 28 Apr 06 nicklas 38 */
2203 28 Apr 06 nicklas 39 public class ColumnMapper
2203 28 Apr 06 nicklas 40   implements Mapper
2203 28 Apr 06 nicklas 41 {
2203 28 Apr 06 nicklas 42   private final int index;
2203 28 Apr 06 nicklas 43   private final String name;
3023 12 Dec 06 nicklas 44   private final NumberFormat parser;
7667 21 Mar 19 nicklas 45   private final Formatter<Date> dateParser;
3472 11 Jun 07 nicklas 46   private final boolean nullIfException;
2203 28 Apr 06 nicklas 47
2203 28 Apr 06 nicklas 48   /**
2203 28 Apr 06 nicklas 49     Create a new column mapper.
2203 28 Apr 06 nicklas 50     @param index The index of the data column to use, starting at 0
2203 28 Apr 06 nicklas 51     @param name An optional name of the column, use in the <code>toString</code>
2203 28 Apr 06 nicklas 52       method
2203 28 Apr 06 nicklas 53    */
2203 28 Apr 06 nicklas 54   public ColumnMapper(int index, String name)
2203 28 Apr 06 nicklas 55   {
7667 21 Mar 19 nicklas 56     this(index, name, null, null, false);
3023 12 Dec 06 nicklas 57   }
3023 12 Dec 06 nicklas 58   
3023 12 Dec 06 nicklas 59   /**
3023 12 Dec 06 nicklas 60     Create a new column mapper using a specific number format.
3023 12 Dec 06 nicklas 61     @param index The index of the data column to use, starting at 0
3023 12 Dec 06 nicklas 62     @param name An optional name of the column, use in the <code>toString</code>
3023 12 Dec 06 nicklas 63       method
3472 11 Jun 07 nicklas 64     @param nullIfException If TRUE, the mapper returns null for unparsable numeric
3472 11 Jun 07 nicklas 65       values, otherwise an excption is thrown
3590 23 Jul 07 nicklas 66     @param parser The parser to use or null to use Double.valueOf()
3472 11 Jun 07 nicklas 67     @since 2.4
7668 21 Mar 19 nicklas 68     @deprecated In 3.15, use {@link ColumnMapper#ColumnMapper(int, String, NumberFormat, Formatter, boolean)} instead
3472 11 Jun 07 nicklas 69   */
7668 21 Mar 19 nicklas 70   @Deprecated
3472 11 Jun 07 nicklas 71   public ColumnMapper(int index, String name, NumberFormat parser, boolean nullIfException)
3023 12 Dec 06 nicklas 72   {
7667 21 Mar 19 nicklas 73     this(index, name, parser, null, nullIfException);
7667 21 Mar 19 nicklas 74   }
7667 21 Mar 19 nicklas 75   
7667 21 Mar 19 nicklas 76   /**
7667 21 Mar 19 nicklas 77     Create a new column mapper using a specific number or date format.
7667 21 Mar 19 nicklas 78     @param index The index of the data column to use, starting at 0
7667 21 Mar 19 nicklas 79     @param name An optional name of the column, use in the <code>toString</code>
7667 21 Mar 19 nicklas 80       method
7667 21 Mar 19 nicklas 81     @param nullIfException If TRUE, the mapper returns null for unparsable numeric
7667 21 Mar 19 nicklas 82       values, otherwise an excption is thrown
7667 21 Mar 19 nicklas 83     @param parser The parser to use for numeric values or null to use Double.valueOf()
7703 11 Apr 19 nicklas 84     @param dateParser The parser to use for date values
7667 21 Mar 19 nicklas 85     @since 3.15
7667 21 Mar 19 nicklas 86   */
7667 21 Mar 19 nicklas 87   public ColumnMapper(int index, String name, NumberFormat parser, Formatter<Date> dateParser, boolean nullIfException)
7667 21 Mar 19 nicklas 88   {
2203 28 Apr 06 nicklas 89     this.index = index;
2203 28 Apr 06 nicklas 90     this.name = name;
3023 12 Dec 06 nicklas 91     this.parser = parser;
7667 21 Mar 19 nicklas 92     this.dateParser = dateParser;
3472 11 Jun 07 nicklas 93     this.nullIfException = nullIfException;
2203 28 Apr 06 nicklas 94   }
7667 21 Mar 19 nicklas 95
2203 28 Apr 06 nicklas 96   /*
2203 28 Apr 06 nicklas 97     From the Mapper interface
2203 28 Apr 06 nicklas 98     -------------------------------------------
2203 28 Apr 06 nicklas 99   */
6127 14 Sep 12 nicklas 100   @Override
7666 20 Mar 19 nicklas 101   @Deprecated
2203 28 Apr 06 nicklas 102   public String getValue(Data data)
2203 28 Apr 06 nicklas 103   {
7666 20 Mar 19 nicklas 104     return getString(data);
7666 20 Mar 19 nicklas 105   }
7666 20 Mar 19 nicklas 106   @Override
7666 20 Mar 19 nicklas 107   public String getString(Data data)
7666 20 Mar 19 nicklas 108   {
7665 20 Mar 19 nicklas 109     return index < data.columns() ? data.getString(index) : null;
2203 28 Apr 06 nicklas 110   }
6127 14 Sep 12 nicklas 111   @Override
2225 09 May 06 nicklas 112   public Integer getInt(Data data)
2225 09 May 06 nicklas 113   {
7664 20 Mar 19 nicklas 114     return index < data.columns() ? data.getInt(index, parser, nullIfException) : null;
2225 09 May 06 nicklas 115   }
6127 14 Sep 12 nicklas 116   @Override
7668 21 Mar 19 nicklas 117   public Long getLong(Data data)
7668 21 Mar 19 nicklas 118   {
7668 21 Mar 19 nicklas 119     return index < data.columns() ? data.getLong(index, parser, nullIfException) : null;
7668 21 Mar 19 nicklas 120   }
7668 21 Mar 19 nicklas 121   @Override
2225 09 May 06 nicklas 122   public Float getFloat(Data data)
2225 09 May 06 nicklas 123   {
7664 20 Mar 19 nicklas 124     return index < data.columns() ? data.getFloat(index, parser, nullIfException) : null;
2225 09 May 06 nicklas 125   }
7667 21 Mar 19 nicklas 126   @Override
7668 21 Mar 19 nicklas 127   public Double getDouble(Data data)
7668 21 Mar 19 nicklas 128   {
7668 21 Mar 19 nicklas 129     return index < data.columns() ? data.getDouble(index, parser, nullIfException) : null;
7668 21 Mar 19 nicklas 130   }
7668 21 Mar 19 nicklas 131   @Override
7667 21 Mar 19 nicklas 132   public Date getDate(Data data)
7667 21 Mar 19 nicklas 133   {
7667 21 Mar 19 nicklas 134     return index < data.columns() ? data.getDate(index, dateParser, nullIfException) : null;
7667 21 Mar 19 nicklas 135   }
2203 28 Apr 06 nicklas 136   // -------------------------------------------
2203 28 Apr 06 nicklas 137   
2203 28 Apr 06 nicklas 138   /*
2203 28 Apr 06 nicklas 139     From the Object class
2203 28 Apr 06 nicklas 140     -------------------------------------------
2203 28 Apr 06 nicklas 141   */
6127 14 Sep 12 nicklas 142   @Override
2203 28 Apr 06 nicklas 143   public String toString()
2203 28 Apr 06 nicklas 144   {
2203 28 Apr 06 nicklas 145     return name == null ? "\\" + index + "\\" : "\\" + name + "\\";
2203 28 Apr 06 nicklas 146   }
2203 28 Apr 06 nicklas 147   // -------------------------------------------
7664 20 Mar 19 nicklas 148
2203 28 Apr 06 nicklas 149 }
2203 28 Apr 06 nicklas 150