You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.text.MessageFormat;
  45. import java.util.Collection;
  46. import org.eclipse.jgit.internal.JGitText;
  47. /** Miscellaneous string comparison utility methods. */
  48. public final class StringUtils {
  49. private static final char[] LC;
  50. static {
  51. LC = new char['Z' + 1];
  52. for (char c = 0; c < LC.length; c++)
  53. LC[c] = c;
  54. for (char c = 'A'; c <= 'Z'; c++)
  55. LC[c] = (char) ('a' + (c - 'A'));
  56. }
  57. /**
  58. * Convert the input to lowercase.
  59. * <p>
  60. * This method does not honor the JVM locale, but instead always behaves as
  61. * though it is in the US-ASCII locale. Only characters in the range 'A'
  62. * through 'Z' are converted. All other characters are left as-is, even if
  63. * they otherwise would have a lowercase character equivalent.
  64. *
  65. * @param c
  66. * the input character.
  67. * @return lowercase version of the input.
  68. */
  69. public static char toLowerCase(final char c) {
  70. return c <= 'Z' ? LC[c] : c;
  71. }
  72. /**
  73. * Convert the input string to lower case, according to the "C" locale.
  74. * <p>
  75. * This method does not honor the JVM locale, but instead always behaves as
  76. * though it is in the US-ASCII locale. Only characters in the range 'A'
  77. * through 'Z' are converted, all other characters are left as-is, even if
  78. * they otherwise would have a lowercase character equivalent.
  79. *
  80. * @param in
  81. * the input string. Must not be null.
  82. * @return a copy of the input string, after converting characters in the
  83. * range 'A'..'Z' to 'a'..'z'.
  84. */
  85. public static String toLowerCase(final String in) {
  86. final StringBuilder r = new StringBuilder(in.length());
  87. for (int i = 0; i < in.length(); i++)
  88. r.append(toLowerCase(in.charAt(i)));
  89. return r.toString();
  90. }
  91. /**
  92. * Test if two strings are equal, ignoring case.
  93. * <p>
  94. * This method does not honor the JVM locale, but instead always behaves as
  95. * though it is in the US-ASCII locale.
  96. *
  97. * @param a
  98. * first string to compare.
  99. * @param b
  100. * second string to compare.
  101. * @return true if a equals b
  102. */
  103. public static boolean equalsIgnoreCase(final String a, final String b) {
  104. if (a == b)
  105. return true;
  106. if (a.length() != b.length())
  107. return false;
  108. for (int i = 0; i < a.length(); i++) {
  109. if (toLowerCase(a.charAt(i)) != toLowerCase(b.charAt(i)))
  110. return false;
  111. }
  112. return true;
  113. }
  114. /**
  115. * Compare two strings, ignoring case.
  116. * <p>
  117. * This method does not honor the JVM locale, but instead always behaves as
  118. * though it is in the US-ASCII locale.
  119. *
  120. * @param a
  121. * first string to compare.
  122. * @param b
  123. * second string to compare.
  124. * @return negative, zero or positive if a sorts before, is equal to, or
  125. * sorts after b.
  126. */
  127. public static int compareIgnoreCase(String a, String b) {
  128. for (int i = 0; i < a.length() && i < b.length(); i++) {
  129. int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
  130. if (d != 0)
  131. return d;
  132. }
  133. return a.length() - b.length();
  134. }
  135. /**
  136. * Compare two strings, honoring case.
  137. * <p>
  138. * This method does not honor the JVM locale, but instead always behaves as
  139. * though it is in the US-ASCII locale.
  140. *
  141. * @param a
  142. * first string to compare.
  143. * @param b
  144. * second string to compare.
  145. * @return negative, zero or positive if a sorts before, is equal to, or
  146. * sorts after b.
  147. */
  148. public static int compareWithCase(String a, String b) {
  149. for (int i = 0; i < a.length() && i < b.length(); i++) {
  150. int d = a.charAt(i) - b.charAt(i);
  151. if (d != 0)
  152. return d;
  153. }
  154. return a.length() - b.length();
  155. }
  156. /**
  157. * Parse a string as a standard Git boolean value. See
  158. * {@link #toBooleanOrNull(String)}.
  159. *
  160. * @param stringValue
  161. * the string to parse.
  162. * @return the boolean interpretation of {@code value}.
  163. * @throws IllegalArgumentException
  164. * if {@code value} is not recognized as one of the standard
  165. * boolean names.
  166. */
  167. public static boolean toBoolean(final String stringValue) {
  168. if (stringValue == null)
  169. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  170. final Boolean bool = toBooleanOrNull(stringValue);
  171. if (bool == null)
  172. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  173. return bool.booleanValue();
  174. }
  175. /**
  176. * Parse a string as a standard Git boolean value.
  177. * <p>
  178. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  179. * used to mean {@code true}.
  180. * <p>
  181. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  182. * used to mean {@code false}.
  183. * <p>
  184. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  185. *
  186. * @param stringValue
  187. * the string to parse.
  188. * @return the boolean interpretation of {@code value} or null in case the
  189. * string does not represent a boolean value
  190. */
  191. public static Boolean toBooleanOrNull(final String stringValue) {
  192. if (stringValue == null)
  193. return null;
  194. if (equalsIgnoreCase("yes", stringValue)
  195. || equalsIgnoreCase("true", stringValue)
  196. || equalsIgnoreCase("1", stringValue)
  197. || equalsIgnoreCase("on", stringValue))
  198. return Boolean.TRUE;
  199. else if (equalsIgnoreCase("no", stringValue)
  200. || equalsIgnoreCase("false", stringValue)
  201. || equalsIgnoreCase("0", stringValue)
  202. || equalsIgnoreCase("off", stringValue))
  203. return Boolean.FALSE;
  204. else
  205. return null;
  206. }
  207. /**
  208. * Join a collection of Strings together using the specified separator.
  209. *
  210. * @param parts
  211. * Strings to join
  212. * @param separator
  213. * used to join
  214. * @return a String with all the joined parts
  215. */
  216. public static String join(Collection<String> parts, String separator) {
  217. return StringUtils.join(parts, separator, separator);
  218. }
  219. /**
  220. * Join a collection of Strings together using the specified separator and a
  221. * lastSeparator which is used for joining the second last and the last
  222. * part.
  223. *
  224. * @param parts
  225. * Strings to join
  226. * @param separator
  227. * separator used to join all but the two last elements
  228. * @param lastSeparator
  229. * separator to use for joining the last two elements
  230. * @return a String with all the joined parts
  231. */
  232. public static String join(Collection<String> parts, String separator,
  233. String lastSeparator) {
  234. StringBuilder sb = new StringBuilder();
  235. int i = 0;
  236. int lastIndex = parts.size() - 1;
  237. for (String part : parts) {
  238. sb.append(part);
  239. if (i == lastIndex - 1) {
  240. sb.append(lastSeparator);
  241. } else if (i != lastIndex) {
  242. sb.append(separator);
  243. }
  244. i++;
  245. }
  246. return sb.toString();
  247. }
  248. private StringUtils() {
  249. // Do not create instances
  250. }
  251. /**
  252. * Test if a string is empty or null.
  253. *
  254. * @param stringValue
  255. * the string to check
  256. * @return <code>true</code> if the string is <code>null</code> or empty
  257. */
  258. public static boolean isEmptyOrNull(String stringValue) {
  259. return stringValue == null || stringValue.length() == 0;
  260. }
  261. }