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

Code
Comments
Other
Rev Date Author Line
4544 25 Sep 08 nicklas 1 /**
4544 25 Sep 08 nicklas 2   $Id $
4544 25 Sep 08 nicklas 3
4544 25 Sep 08 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4544 25 Sep 08 nicklas 5
4544 25 Sep 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4544 25 Sep 08 nicklas 7   Available at http://base.thep.lu.se/
4544 25 Sep 08 nicklas 8
4544 25 Sep 08 nicklas 9   BASE is free software; you can redistribute it and/or
4544 25 Sep 08 nicklas 10   modify it under the terms of the GNU General Public License
4544 25 Sep 08 nicklas 11   as published by the Free Software Foundation; either version 3
4544 25 Sep 08 nicklas 12   of the License, or (at your option) any later version.
4544 25 Sep 08 nicklas 13
4544 25 Sep 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4544 25 Sep 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4544 25 Sep 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4544 25 Sep 08 nicklas 17   GNU General Public License for more details.
4544 25 Sep 08 nicklas 18
4544 25 Sep 08 nicklas 19   You should have received a copy of the GNU General Public License
4544 25 Sep 08 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4544 25 Sep 08 nicklas 21 */
4544 25 Sep 08 nicklas 22 package net.sf.basedb.util.formatter;
4544 25 Sep 08 nicklas 23
4544 25 Sep 08 nicklas 24
4544 25 Sep 08 nicklas 25 /**
4544 25 Sep 08 nicklas 26    Formats a value with optional prefix and suffix. This formatter uses
4544 25 Sep 08 nicklas 27    a parent formatter to format the actual value and then adds
4544 25 Sep 08 nicklas 28    prefix and suffix as needed to the start and end of the
4544 25 Sep 08 nicklas 29    formatted value.
4544 25 Sep 08 nicklas 30    <p>
4544 25 Sep 08 nicklas 31    The {@link #parseString(String)} method will first check if
4544 25 Sep 08 nicklas 32    the string start or ends with the given prefix/suffix. If it does, 
4544 25 Sep 08 nicklas 33    the prefix/suffix is removed before parsing is delegated to the 
4544 25 Sep 08 nicklas 34    parent.
4544 25 Sep 08 nicklas 35    
4544 25 Sep 08 nicklas 36   @author nicklas
4544 25 Sep 08 nicklas 37   @version 2.9
4544 25 Sep 08 nicklas 38   @base.modified $Date$
4544 25 Sep 08 nicklas 39 */
4544 25 Sep 08 nicklas 40 public class PrefixSuffixFormatter<T>
4544 25 Sep 08 nicklas 41   implements Formatter<T>
4544 25 Sep 08 nicklas 42 {
4544 25 Sep 08 nicklas 43   
4544 25 Sep 08 nicklas 44   /**
4544 25 Sep 08 nicklas 45     The character representing a non-breaking space. Usefule to
4544 25 Sep 08 nicklas 46     avoid line wrapping between prefix/suffix and value.
4544 25 Sep 08 nicklas 47   */
4544 25 Sep 08 nicklas 48   public static final char NBSP = 0xA0;
4544 25 Sep 08 nicklas 49
4544 25 Sep 08 nicklas 50   private final String prefix;
4544 25 Sep 08 nicklas 51   private final Formatter<T> parent;
4544 25 Sep 08 nicklas 52   private final String suffix;
6268 11 Apr 13 nicklas 53   private final boolean usePrefixForNull;
6268 11 Apr 13 nicklas 54   private final boolean useSuffixForNull;
4544 25 Sep 08 nicklas 55   
4544 25 Sep 08 nicklas 56   /**
6268 11 Apr 13 nicklas 57     Create a new formatter. Prefix and suffix are included for null values.
4544 25 Sep 08 nicklas 58     @param prefix The prefix, or null to not use any prefix
4544 25 Sep 08 nicklas 59     @param parent The parent formatter, if null a {@link ToStringFormatter}
4544 25 Sep 08 nicklas 60       is automatically created
4544 25 Sep 08 nicklas 61     @param suffix The suffix, or null to not use any suffix
4544 25 Sep 08 nicklas 62   */
4544 25 Sep 08 nicklas 63   public PrefixSuffixFormatter(String prefix, Formatter<T> parent, String suffix)
4544 25 Sep 08 nicklas 64   {
6268 11 Apr 13 nicklas 65     this(prefix, true, parent, suffix, true);
6268 11 Apr 13 nicklas 66   }
6268 11 Apr 13 nicklas 67   
6268 11 Apr 13 nicklas 68   /**
6268 11 Apr 13 nicklas 69     Create a new formatter.
6268 11 Apr 13 nicklas 70     @param prefix The prefix, or null to not use any prefix
6268 11 Apr 13 nicklas 71     @param usePrefixForNull If set, the prefix is always included, otherwise only for non-null
6268 11 Apr 13 nicklas 72       values
6268 11 Apr 13 nicklas 73     @param parent The parent formatter, if null a {@link ToStringFormatter}
6268 11 Apr 13 nicklas 74       is automatically created
6268 11 Apr 13 nicklas 75     @param suffix The suffix, or null to not use any suffix
6268 11 Apr 13 nicklas 76     @param useSuffixForNull If set, the suffix is always included, otherwise only for non-null
6268 11 Apr 13 nicklas 77       values
6268 11 Apr 13 nicklas 78   */
6268 11 Apr 13 nicklas 79   public PrefixSuffixFormatter(String prefix, boolean usePrefixForNull, Formatter<T> parent, String suffix, boolean useSuffixForNull)
6268 11 Apr 13 nicklas 80   {
4544 25 Sep 08 nicklas 81     this.prefix = prefix == null ? "" : prefix;
6268 11 Apr 13 nicklas 82     this.usePrefixForNull = usePrefixForNull;
4544 25 Sep 08 nicklas 83     this.parent = parent == null ? new ToStringFormatter<T>() : parent;
4544 25 Sep 08 nicklas 84     this.suffix = suffix == null ? "" : suffix;
6268 11 Apr 13 nicklas 85     this.useSuffixForNull = useSuffixForNull;
4544 25 Sep 08 nicklas 86   }
6268 11 Apr 13 nicklas 87
4544 25 Sep 08 nicklas 88   
4544 25 Sep 08 nicklas 89   /*
4544 25 Sep 08 nicklas 90     From the Formatter interface
4544 25 Sep 08 nicklas 91     -------------------------------------------
4544 25 Sep 08 nicklas 92   */
6127 14 Sep 12 nicklas 93   @Override
4544 25 Sep 08 nicklas 94   public String format(T value)
4544 25 Sep 08 nicklas 95   {
6268 11 Apr 13 nicklas 96     StringBuilder sb = new StringBuilder();
6268 11 Apr 13 nicklas 97     if (value != null || usePrefixForNull) sb.append(prefix);
6268 11 Apr 13 nicklas 98     sb.append(parent.format(value));
6268 11 Apr 13 nicklas 99     if (value != null || useSuffixForNull) sb.append(suffix);
6268 11 Apr 13 nicklas 100     return sb.toString();
4544 25 Sep 08 nicklas 101   }
6127 14 Sep 12 nicklas 102   @Override
4544 25 Sep 08 nicklas 103   public T parseString(String value)
4544 25 Sep 08 nicklas 104   {
4544 25 Sep 08 nicklas 105     if (value != null)
4544 25 Sep 08 nicklas 106     {
4544 25 Sep 08 nicklas 107       if (prefix.length() > 0 && value.startsWith(prefix))
4544 25 Sep 08 nicklas 108       {
4544 25 Sep 08 nicklas 109         value = value.substring(prefix.length());
4544 25 Sep 08 nicklas 110       }
4544 25 Sep 08 nicklas 111       if (suffix.length() > 0 && value.endsWith(suffix))
4544 25 Sep 08 nicklas 112       {
4544 25 Sep 08 nicklas 113         value = value.substring(0, value.length()-suffix.length());
4544 25 Sep 08 nicklas 114       }
4544 25 Sep 08 nicklas 115     }
4544 25 Sep 08 nicklas 116     return parent.parseString(value);
4544 25 Sep 08 nicklas 117   }
4544 25 Sep 08 nicklas 118   // -------------------------------------------
4544 25 Sep 08 nicklas 119
4544 25 Sep 08 nicklas 120 }