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

Code
Comments
Other
Rev Date Author Line
2751 20 Oct 06 nicklas 1 /**
2751 20 Oct 06 nicklas 2   $Id$
2751 20 Oct 06 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2006 Nicklas Nordborg
2751 20 Oct 06 nicklas 5
2751 20 Oct 06 nicklas 6   This file is part of BASE - BioArray Software Environment.
2751 20 Oct 06 nicklas 7   Available at http://base.thep.lu.se/
2751 20 Oct 06 nicklas 8
2751 20 Oct 06 nicklas 9   BASE is free software; you can redistribute it and/or
2751 20 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
2751 20 Oct 06 nicklas 12   of the License, or (at your option) any later version.
2751 20 Oct 06 nicklas 13
2751 20 Oct 06 nicklas 14   BASE is distributed in the hope that it will be useful,
2751 20 Oct 06 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
2751 20 Oct 06 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2751 20 Oct 06 nicklas 17   GNU General Public License for more details.
2751 20 Oct 06 nicklas 18
2751 20 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/>.
2751 20 Oct 06 nicklas 21 */
2751 20 Oct 06 nicklas 22 package net.sf.basedb.util.parser;
2751 20 Oct 06 nicklas 23
7667 21 Mar 19 nicklas 24 import java.util.Date;
7667 21 Mar 19 nicklas 25
2751 20 Oct 06 nicklas 26 import net.sf.basedb.util.parser.FlatFileParser.Data;
2751 20 Oct 06 nicklas 27
2751 20 Oct 06 nicklas 28 /**
2751 20 Oct 06 nicklas 29    A mapper that crops a string if it is longer than a specified
2751 20 Oct 06 nicklas 30    length. The actual mapping is delegated to a parent mapper.
2751 20 Oct 06 nicklas 31
2751 20 Oct 06 nicklas 32   @author nicklas
2751 20 Oct 06 nicklas 33   @version 2.0
2751 20 Oct 06 nicklas 34   @base.modified $Date$
2751 20 Oct 06 nicklas 35 */
2751 20 Oct 06 nicklas 36 public class CropStringMapper
2751 20 Oct 06 nicklas 37   implements Mapper
2751 20 Oct 06 nicklas 38 {
2751 20 Oct 06 nicklas 39
2751 20 Oct 06 nicklas 40   private final Mapper parent;
2751 20 Oct 06 nicklas 41   private final int maxLength;
2751 20 Oct 06 nicklas 42   
2751 20 Oct 06 nicklas 43   /**
2751 20 Oct 06 nicklas 44     Create a new CropStringMapper.
2751 20 Oct 06 nicklas 45     @param parent The parent mapper that does the actual mapping
2751 20 Oct 06 nicklas 46     @param maxLength The maximum length of stings
2751 20 Oct 06 nicklas 47   */
2751 20 Oct 06 nicklas 48   public CropStringMapper(Mapper parent, int maxLength)
2751 20 Oct 06 nicklas 49   {
2751 20 Oct 06 nicklas 50     this.parent = parent;
2751 20 Oct 06 nicklas 51     this.maxLength = maxLength;
2751 20 Oct 06 nicklas 52   }
2751 20 Oct 06 nicklas 53
2751 20 Oct 06 nicklas 54   /*
2751 20 Oct 06 nicklas 55     From the Mapper interface
2751 20 Oct 06 nicklas 56     -------------------------------------------
2751 20 Oct 06 nicklas 57   */
7666 20 Mar 19 nicklas 58   @Override
7666 20 Mar 19 nicklas 59   @Deprecated
7666 20 Mar 19 nicklas 60   public String getValue(Data data)
7666 20 Mar 19 nicklas 61   {
7666 20 Mar 19 nicklas 62     return getString(data);
7666 20 Mar 19 nicklas 63   }
2751 20 Oct 06 nicklas 64   /**
2751 20 Oct 06 nicklas 65     Use the parent mapper to get the string value. Then check the
2751 20 Oct 06 nicklas 66     length against the maximum length and crop if neccessary.
2751 20 Oct 06 nicklas 67   */
6127 14 Sep 12 nicklas 68   @Override
7666 20 Mar 19 nicklas 69   public String getString(Data data)
2751 20 Oct 06 nicklas 70   {
7666 20 Mar 19 nicklas 71     String s = parent.getString(data);
2751 20 Oct 06 nicklas 72     if (s != null && s.length() > maxLength) s = s.substring(0, maxLength);
2751 20 Oct 06 nicklas 73     return s;
2751 20 Oct 06 nicklas 74   }
6127 14 Sep 12 nicklas 75   @Override
2751 20 Oct 06 nicklas 76   public Integer getInt(Data data)
2751 20 Oct 06 nicklas 77   {
2751 20 Oct 06 nicklas 78     return parent.getInt(data);
2751 20 Oct 06 nicklas 79   }
6127 14 Sep 12 nicklas 80   @Override
7668 21 Mar 19 nicklas 81   public Long getLong(Data data)
7668 21 Mar 19 nicklas 82   {
7668 21 Mar 19 nicklas 83     return parent.getLong(data);
7668 21 Mar 19 nicklas 84   }
7668 21 Mar 19 nicklas 85   @Override
2751 20 Oct 06 nicklas 86   public Float getFloat(Data data)
2751 20 Oct 06 nicklas 87   {
2751 20 Oct 06 nicklas 88     return parent.getFloat(data);
2751 20 Oct 06 nicklas 89   }
7667 21 Mar 19 nicklas 90   @Override
7668 21 Mar 19 nicklas 91   public Double getDouble(Data data)
7668 21 Mar 19 nicklas 92   {
7668 21 Mar 19 nicklas 93     return parent.getDouble(data);
7668 21 Mar 19 nicklas 94   }
7668 21 Mar 19 nicklas 95   @Override
7667 21 Mar 19 nicklas 96   public Date getDate(Data data)
7667 21 Mar 19 nicklas 97   {
7667 21 Mar 19 nicklas 98     return parent.getDate(data);
7667 21 Mar 19 nicklas 99   }
2751 20 Oct 06 nicklas 100   // -------------------------------------------
2751 20 Oct 06 nicklas 101
2751 20 Oct 06 nicklas 102 }