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 6.7KB

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