您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StringUtils.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.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. * Parse a string as a standard Git boolean value. See
  116. * {@link #toBooleanOrNull(String)}.
  117. *
  118. * @param stringValue
  119. * the string to parse.
  120. * @return the boolean interpretation of {@code value}.
  121. * @throws IllegalArgumentException
  122. * if {@code value} is not recognized as one of the standard
  123. * boolean names.
  124. */
  125. public static boolean toBoolean(final String stringValue) {
  126. if (stringValue == null)
  127. throw new NullPointerException(JGitText.get().expectedBooleanStringValue);
  128. final Boolean bool = toBooleanOrNull(stringValue);
  129. if (bool == null)
  130. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notABoolean, stringValue));
  131. return bool.booleanValue();
  132. }
  133. /**
  134. * Parse a string as a standard Git boolean value.
  135. * <p>
  136. * The terms {@code yes}, {@code true}, {@code 1}, {@code on} can all be
  137. * used to mean {@code true}.
  138. * <p>
  139. * The terms {@code no}, {@code false}, {@code 0}, {@code off} can all be
  140. * used to mean {@code false}.
  141. * <p>
  142. * Comparisons ignore case, via {@link #equalsIgnoreCase(String, String)}.
  143. *
  144. * @param stringValue
  145. * the string to parse.
  146. * @return the boolean interpretation of {@code value} or null in case the
  147. * string does not represent a boolean value
  148. */
  149. public static Boolean toBooleanOrNull(final String stringValue) {
  150. if (stringValue == null)
  151. return null;
  152. if (equalsIgnoreCase("yes", stringValue)
  153. || equalsIgnoreCase("true", stringValue)
  154. || equalsIgnoreCase("1", stringValue)
  155. || equalsIgnoreCase("on", stringValue))
  156. return Boolean.TRUE;
  157. else if (equalsIgnoreCase("no", stringValue)
  158. || equalsIgnoreCase("false", stringValue)
  159. || equalsIgnoreCase("0", stringValue)
  160. || equalsIgnoreCase("off", stringValue))
  161. return Boolean.FALSE;
  162. else
  163. return null;
  164. }
  165. /**
  166. * Join a collection of Strings together using the specified separator.
  167. *
  168. * @param parts
  169. * Strings to join
  170. * @param separator
  171. * used to join
  172. * @return a String with all the joined parts
  173. */
  174. public static String join(Collection<String> parts, String separator) {
  175. return StringUtils.join(parts, separator, separator);
  176. }
  177. /**
  178. * Join a collection of Strings together using the specified separator and a
  179. * lastSeparator which is used for joining the second last and the last
  180. * part.
  181. *
  182. * @param parts
  183. * Strings to join
  184. * @param separator
  185. * separator used to join all but the two last elements
  186. * @param lastSeparator
  187. * separator to use for joining the last two elements
  188. * @return a String with all the joined parts
  189. */
  190. public static String join(Collection<String> parts, String separator,
  191. String lastSeparator) {
  192. StringBuilder sb = new StringBuilder();
  193. int i = 0;
  194. int lastIndex = parts.size() - 1;
  195. for (String part : parts) {
  196. sb.append(part);
  197. if (i == lastIndex - 1) {
  198. sb.append(lastSeparator);
  199. } else if (i != lastIndex) {
  200. sb.append(separator);
  201. }
  202. i++;
  203. }
  204. return sb.toString();
  205. }
  206. private StringUtils() {
  207. // Do not create instances
  208. }
  209. }