Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/StringUtils.java @ 301:b0a949d3fe09
Moved StringUtils to common package. Added some Override annotations.
artifacts/trunk@2394 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 22 Jul 2011 09:36:04 +0000 |
parents | |
children | 9a85e0ef6e02 |
comparison
equal
deleted
inserted
replaced
300:0035e2511342 | 301:b0a949d3fe09 |
---|---|
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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |