001 /**
002 * Copyright (c) 2010, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.base.string;
029
030
031 import java.io.Serializable;
032 import java.util.HashMap;
033 import java.util.Map;
034 import org.apache.commons.collections15.Transformer;
035
036
037 /**
038 * {@code StringConverters} simply provides converter from string into specific
039 * values.
040 *
041 * @author nikitin
042 *
043 * @version 1.0
044 *
045 */
046 public final class StringConverters implements Serializable {
047
048 private static final long serialVersionUID = -5511811932302734965L;
049
050 public static abstract class StringConverter<T> implements
051 Transformer<String, T>, Serializable {
052
053 private static final long serialVersionUID = -7583591561073492788L;
054
055 public abstract T transform(String object);
056 }
057
058 /**
059 * Code a dynamic StringConverter from a string (@see javax.tools).
060 *
061 * @param content the source code to compile.
062 * @return an instance of StringConverter.
063 */
064 @SuppressWarnings( {"unchecked", "unused"})
065 private static final StringConverter valueOf(String content) {
066 return null;
067 }
068
069 /** String -> String */
070 public static final StringConverter<String> STR_2_STR =
071 new StringConverter<String>() {
072
073 private static final long serialVersionUID = -1064438881441457342L;
074
075 @Override
076 public final String transform(String str) {
077 return str;
078 }
079
080 };
081
082 /** String -> Integer */
083 public static final StringConverter<Integer> STR_2_INT =
084 new StringConverter<Integer>() {
085
086 private static final long serialVersionUID = 1321255579382956146L;
087
088 public final Integer transform(String str) {
089 return Integer.valueOf(str);
090 }
091
092 };
093
094 /** String -> Double */
095 public static final StringConverter<Double> STR_2_DBL =
096 new StringConverter<Double>() {
097
098 private static final long serialVersionUID = -8223318729056528439L;
099
100 public final Double transform(String str) {
101 return Double.valueOf(str);
102 }
103
104 };
105
106 /** String -> Number */
107 public static final StringConverter<Number> STR_2_NBR =
108 new StringConverter<Number>() {
109
110 private static final long serialVersionUID = -8223318729056528439L;
111
112 public final Double transform(String str) {
113 return Double.valueOf(str);
114 }
115
116 };
117
118 /** String -> Boolean */
119 public static final StringConverter<Boolean> STR_2_BOOL =
120 new StringConverter<Boolean>() {
121
122 private static final long serialVersionUID = 6173773001495413861L;
123
124 public final Boolean transform(String str) {
125 return Boolean.valueOf(str);
126 }
127
128 };
129
130 /** String -> Map<String, String> */
131 @SuppressWarnings("unchecked")
132 public static final StringConverter<Map> STR_2_MAP =
133 new StringConverter<Map>() {
134
135 private static final long serialVersionUID = -4705259938781158320L;
136
137 @Override
138 public Map<String, String> transform(String ms) {
139
140 Map<String, String> m = new HashMap<String, String>();
141
142 if (ms.charAt(0) == '{' && ms.charAt(ms.length() - 1) == '}') {
143 ms = ms.substring(1, ms.length() - 1);
144
145 String[] fields = ms.split(",\\s*");
146
147 for (String field : fields) {
148 String[] kv = field.split("=");
149
150 m.put(kv[0], kv[1]);
151 }
152 } else {
153 throw new IllegalStateException(ms + ": map parse error");
154 }
155 return m;
156 }
157
158 };
159
160 /** String -> RecordsCount<String> */
161 // @SuppressWarnings("unchecked")
162 // public static final StringConverter<RecordsCount> STR_2_RECORD_COUNTS
163 // =
164 // new StringConverter<RecordsCount>() {
165 //
166 // private static final long serialVersionUID = -4705259938781158320L;
167 //
168 // @Override
169 // public RecordsCount<String> convert(String s) {
170 // return RecordsCount.valueOf(s);
171 // }
172 //
173 // };
174
175 }