src/core/net/sf/basedb/util/encode/EncodeUtil.java

Code
Comments
Other
Rev Date Author Line
5434 30 Sep 10 nicklas 1 /**
5434 30 Sep 10 nicklas 2   $Id$
5434 30 Sep 10 nicklas 3
5434 30 Sep 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5434 30 Sep 10 nicklas 5
5434 30 Sep 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5434 30 Sep 10 nicklas 7   Available at http://base.thep.lu.se/
5434 30 Sep 10 nicklas 8
5434 30 Sep 10 nicklas 9   BASE is free software; you can redistribute it and/or
5434 30 Sep 10 nicklas 10   modify it under the terms of the GNU General Public License
5434 30 Sep 10 nicklas 11   as published by the Free Software Foundation; either version 3
5434 30 Sep 10 nicklas 12   of the License, or (at your option) any later version.
5434 30 Sep 10 nicklas 13
5434 30 Sep 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5434 30 Sep 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5434 30 Sep 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5434 30 Sep 10 nicklas 17   GNU General Public License for more details.
5434 30 Sep 10 nicklas 18
5434 30 Sep 10 nicklas 19   You should have received a copy of the GNU General Public License
5434 30 Sep 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5434 30 Sep 10 nicklas 21 */
5434 30 Sep 10 nicklas 22 package net.sf.basedb.util.encode;
5434 30 Sep 10 nicklas 23
5434 30 Sep 10 nicklas 24 import java.util.ArrayList;
5434 30 Sep 10 nicklas 25 import java.util.Collection;
5434 30 Sep 10 nicklas 26 import java.util.List;
5434 30 Sep 10 nicklas 27
5434 30 Sep 10 nicklas 28 /**
5434 30 Sep 10 nicklas 29   Utility functions for encoding/decoding strings.
5434 30 Sep 10 nicklas 30
5434 30 Sep 10 nicklas 31   @author Nicklas
5434 30 Sep 10 nicklas 32   @since 2.16
5434 30 Sep 10 nicklas 33   @base.modified $Date$
5434 30 Sep 10 nicklas 34 */
5434 30 Sep 10 nicklas 35 public class EncodeUtil
5434 30 Sep 10 nicklas 36 {
5434 30 Sep 10 nicklas 37
5434 30 Sep 10 nicklas 38   /**
5434 30 Sep 10 nicklas 39     Encode all strings in a collection.
5434 30 Sep 10 nicklas 40     @param values A collection with values that should be encoded
5434 30 Sep 10 nicklas 41     @param encoder The encoder implemenation to use
5434 30 Sep 10 nicklas 42     @return A list with the encoded values
5434 30 Sep 10 nicklas 43   */
5434 30 Sep 10 nicklas 44   public static List<String> encodeAll(Collection<String> values, EncoderDecoder encoder)
5434 30 Sep 10 nicklas 45   {
5434 30 Sep 10 nicklas 46     List<String> encoded = new ArrayList<String>(values.size());
5434 30 Sep 10 nicklas 47     for (String s : values)
5434 30 Sep 10 nicklas 48     {
5434 30 Sep 10 nicklas 49       encoded.add(encoder.encode(s));
5434 30 Sep 10 nicklas 50     }
5434 30 Sep 10 nicklas 51     return encoded;
5434 30 Sep 10 nicklas 52   }
5434 30 Sep 10 nicklas 53   
5434 30 Sep 10 nicklas 54   /**
5434 30 Sep 10 nicklas 55     Decode all strings in a collection.
5434 30 Sep 10 nicklas 56     @param values A collection with values that should be decoded
5434 30 Sep 10 nicklas 57     @param decoder The decoder implemenation to use
5434 30 Sep 10 nicklas 58     @return A list with the decoded values
5434 30 Sep 10 nicklas 59   */
5434 30 Sep 10 nicklas 60   public static List<String> decodeAll(Collection<String> values, EncoderDecoder decoder)
5434 30 Sep 10 nicklas 61   {
5434 30 Sep 10 nicklas 62     List<String> decoded = new ArrayList<String>(values.size());
5434 30 Sep 10 nicklas 63     for (String s : values)
5434 30 Sep 10 nicklas 64     {
5434 30 Sep 10 nicklas 65       decoded.add(decoder.decode(s));
5434 30 Sep 10 nicklas 66     }
5434 30 Sep 10 nicklas 67     return decoded;
5434 30 Sep 10 nicklas 68   }
5434 30 Sep 10 nicklas 69
5434 30 Sep 10 nicklas 70   
5434 30 Sep 10 nicklas 71   /**
5434 30 Sep 10 nicklas 72     Encode all strings in a collection and join them into a single
5434 30 Sep 10 nicklas 73     string.
5434 30 Sep 10 nicklas 74     
5434 30 Sep 10 nicklas 75     @param values A collection with values that should be encoded
5434 30 Sep 10 nicklas 76     @param encoder The encoder implemenation to use
5434 30 Sep 10 nicklas 77     @param separator The separator string that is inserted between the decoded values
5434 30 Sep 10 nicklas 78     @param skipNull TRUE to skip null values (before and after encoding)
5434 30 Sep 10 nicklas 79     @return A string with the encoded values joined together
5434 30 Sep 10 nicklas 80   */
5434 30 Sep 10 nicklas 81   public static String encodeAndJoin(Collection<String> values, EncoderDecoder encoder, String separator, boolean skipNull)
5434 30 Sep 10 nicklas 82   {
5434 30 Sep 10 nicklas 83     StringBuilder sb = new StringBuilder();
5434 30 Sep 10 nicklas 84     boolean firstElement = true;
5434 30 Sep 10 nicklas 85     for (String s : values)
5434 30 Sep 10 nicklas 86     {
5434 30 Sep 10 nicklas 87       if (s == null && skipNull) continue;
5434 30 Sep 10 nicklas 88       s = encoder.encode(s);
5434 30 Sep 10 nicklas 89       if (s == null && skipNull) continue;
5434 30 Sep 10 nicklas 90       if (!firstElement) sb.append(separator);
5434 30 Sep 10 nicklas 91       sb.append(s);
5434 30 Sep 10 nicklas 92       firstElement = false;
5434 30 Sep 10 nicklas 93     }
5434 30 Sep 10 nicklas 94     return sb.toString();
5434 30 Sep 10 nicklas 95   }
5434 30 Sep 10 nicklas 96   
5434 30 Sep 10 nicklas 97 }