Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/StringUtils.java @ 472:783cc1b6b615
Moved directories to org.dive4elements
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 10:53:15 +0200 |
parents | artifacts-common/src/main/java/de/intevation/artifacts/common/utils/StringUtils.java@d75b7d19a3df |
children | 415df0fc4fa1 |
comparison
equal
deleted
inserted
replaced
471:1a87cb24a446 | 472:783cc1b6b615 |
---|---|
1 /* | |
2 * Copyright (c) 2010 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 | |
9 package de.intevation.artifacts.common.utils; | |
10 | |
11 import java.io.UnsupportedEncodingException; | |
12 | |
13 import java.util.UUID; | |
14 | |
15 import org.apache.commons.codec.DecoderException; | |
16 | |
17 import org.apache.commons.codec.binary.Hex; | |
18 | |
19 import org.apache.log4j.Logger; | |
20 | |
21 /** | |
22 * Commonly used string functions. | |
23 * | |
24 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
25 */ | |
26 public final class StringUtils | |
27 { | |
28 private static Logger logger = Logger.getLogger(StringUtils.class); | |
29 | |
30 /** | |
31 * Generated a random UUIDv4 in form of a string. | |
32 * @return the UUID | |
33 */ | |
34 public static final String newUUID() { | |
35 return UUID.randomUUID().toString(); | |
36 } | |
37 | |
38 /** | |
39 * Checks if a given string is a valid UUID. | |
40 * @param uuid The string to test. | |
41 * @return true if the string is a valid UUID else false. | |
42 */ | |
43 public static final boolean checkUUID(String uuid) { | |
44 try { | |
45 UUID.fromString(uuid); | |
46 } | |
47 catch (IllegalArgumentException iae) { | |
48 logger.warn(iae.getLocalizedMessage()); | |
49 return false; | |
50 } | |
51 return true; | |
52 } | |
53 | |
54 /** | |
55 * Returns the UTF-8 byte array representation of a given string. | |
56 * @param s The string to be transformed. | |
57 * @return The byte array representation. | |
58 */ | |
59 public static final byte [] getUTF8Bytes(String s) { | |
60 try { | |
61 return s.getBytes("UTF-8"); | |
62 } | |
63 catch (UnsupportedEncodingException usee) { | |
64 logger.error(usee.getLocalizedMessage(), usee); | |
65 return s.getBytes(); | |
66 } | |
67 } | |
68 | |
69 /** | |
70 * Tries to convert a Base64 encoded string into the | |
71 * corresponing byte array. | |
72 * @param s The Base64 encoded string | |
73 * @return The byte array representation or null if | |
74 * an decoding error occurs. | |
75 */ | |
76 public static final byte [] decodeHex(String s) { | |
77 try { | |
78 return Hex.decodeHex(s.toCharArray()); | |
79 } | |
80 catch (DecoderException de) { | |
81 return null; | |
82 } | |
83 } | |
84 | |
85 public static final String repeat(String s, int count, String sep) { | |
86 if (count <= 0) { | |
87 return ""; | |
88 } | |
89 StringBuilder sb = new StringBuilder(s); | |
90 for (--count; count > 0; --count) { | |
91 sb.append(sep).append(s); | |
92 } | |
93 return sb.toString(); | |
94 } | |
95 | |
96 public static final String repeat(char c, int count, char sep) { | |
97 if (count <= 0) { | |
98 return ""; | |
99 } | |
100 StringBuilder sb = new StringBuilder(2*count-1).append(c); | |
101 for (--count; count > 0; --count) { | |
102 sb.append(sep).append(c); | |
103 } | |
104 return sb.toString(); | |
105 } | |
106 | |
107 public static final String [] toUpperCase(String [] s) { | |
108 if (s == null) { | |
109 return null; | |
110 } | |
111 String [] d = new String[s.length]; | |
112 for (int i = 0; i < s.length; ++i) { | |
113 if (s[i] != null) { | |
114 d[i] = s[i].toUpperCase(); | |
115 } | |
116 } | |
117 return d; | |
118 } | |
119 | |
120 public static String join(String sep, String [] strings) { | |
121 StringBuilder sb = new StringBuilder(); | |
122 for (int i = 0; i < strings.length; ++i) { | |
123 if (i > 0) sb.append(sep); | |
124 sb.append(strings[i]); | |
125 } | |
126 return sb.toString(); | |
127 } | |
128 | |
129 public static final String [] join(String [] a, String [] b) { | |
130 if (a == null && b == null) return null; | |
131 if (a == null) return b; | |
132 if (b == null) return a; | |
133 String [] dst = new String[a.length + b.length]; | |
134 System.arraycopy(a, 0, dst, 0, a.length); | |
135 System.arraycopy(b, 0, dst, a.length, b.length); | |
136 return dst; | |
137 } | |
138 | |
139 public static final boolean contains(String needle, String [] haystack) { | |
140 for (String stray: haystack) { | |
141 if (needle.equals(stray)) { | |
142 return true; | |
143 } | |
144 } | |
145 return false; | |
146 } | |
147 } | |
148 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |