Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

StringUtils.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * @since 2.0
  127. */
  128. public static int compareIgnoreCase(String a, String b) {
  129. for (int i = 0; i < a.length() && i < b.length(); i++) {
  130. int d = toLowerCase(a.charAt(i)) - toLowerCase(b.charAt(i));
  131. if (d != 0)
  132. return d;
  133. }
  134. return a.length() - b.length();
  135. }
  136. /**
  137. * Compare two strings, honoring case.
  138. * <p>
  139. * This method does not honor the JVM locale, but instead always behaves as
  140. * though it is in the US-ASCII locale.
  141. *
  142. * @param a
  143. * first string to compare.
  144. * @param b
  145. * second string to compare.
  146. * @return negative, zero or positive if a sorts before, is equal to, or
  147. * sorts after b.
  148. * @since 2.0
  149. */
  150. public static int compareWithCase(String a, String b) {
  151. for (int i = 0; i < a.length() && i < b.length(); i++) {
  152. int d = a.charAt(i) - b.charAt(i);
  153. if (d != 0)
  154. return d;
  155. }
  156. return a.length() - b.length();
  157. }
  158. /**
  159. * Parse a string as a standard Git boolean value. See
  160. * {@link #toBooleanOrNull(String)}.
  161. *
  162. * @param stringValue
  163. * the string to parse.
  164. * @return the boolean interpretation of {@code value}.
  165. * @throws IllegalArgumentException
  166. * if {@code value} is not recognized as one of the standard
  167. * boolean names.
  168. */
  169. public static boolean toBoolean(final String stringValue) {
  170. if (stringValue == null)
  171. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  172. final Boolean bool = toBooleanOrNull(stringValue);
  173. if (bool == null)
  174. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  175. return bool.booleanValue();
  176. }
  177. /**
  178. * Parse a string as a standard Git boolean value.
  179. * <p>
  180. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  181. * used to mean {@code true}.
  182. * <p>
  183. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  184. * used to mean {@code false}.
  185. * <p>
  186. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  187. *
  188. * @param stringValue
  189. * the string to parse.
  190. * @return the boolean interpretation of {@code value} or null in case the
  191. * string does not represent a boolean value
  192. */
  193. public static Boolean toBooleanOrNull(final String stringValue) {
  194. if (stringValue == null)
  195. return null;
  196. if (equalsIgnoreCase("yes", stringValue)
  197. || equalsIgnoreCase("true", stringValue)
  198. || equalsIgnoreCase("1", stringValue)
  199. || equalsIgnoreCase("on", stringValue))
  200. return Boolean.TRUE;
  201. else if (equalsIgnoreCase("no", stringValue)
  202. || equalsIgnoreCase("false", stringValue)
  203. || equalsIgnoreCase("0", stringValue)
  204. || equalsIgnoreCase("off", stringValue))
  205. return Boolean.FALSE;
  206. else
  207. return null;
  208. }
  209. /**
  210. * Join a collection of Strings together using the specified separator.
  211. *
  212. * @param parts
  213. * Strings to join
  214. * @param separator
  215. * used to join
  216. * @return a String with all the joined parts
  217. */
  218. public static String join(Collection<String> parts, String separator) {
  219. return StringUtils.join(parts, separator, separator);
  220. }
  221. /**
  222. * Join a collection of Strings together using the specified separator and a
  223. * lastSeparator which is used for joining the second last and the last
  224. * part.
  225. *
  226. * @param parts
  227. * Strings to join
  228. * @param separator
  229. * separator used to join all but the two last elements
  230. * @param lastSeparator
  231. * separator to use for joining the last two elements
  232. * @return a String with all the joined parts
  233. */
  234. public static String join(Collection<String> parts, String separator,
  235. String lastSeparator) {
  236. StringBuilder sb = new StringBuilder();
  237. int i = 0;
  238. int lastIndex = parts.size() - 1;
  239. for (String part : parts) {
  240. sb.append(part);
  241. if (i == lastIndex - 1) {
  242. sb.append(lastSeparator);
  243. } else if (i != lastIndex) {
  244. sb.append(separator);
  245. }
  246. i++;
  247. }
  248. return sb.toString();
  249. }
  250. private StringUtils() {
  251. // Do not create instances
  252. }
  253. /**
  254. * Test if a string is empty or null.
  255. *
  256. * @param stringValue
  257. * the string to check
  258. * @return <code>true</code> if the string is <code>null</code> or empty
  259. */
  260. public static boolean isEmptyOrNull(String stringValue) {
  261. return stringValue == null || stringValue.length() == 0;
  262. }
  263. }