001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.primitives;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkElementIndex;
021import static com.google.common.base.Preconditions.checkNotNull;
022import static com.google.common.base.Preconditions.checkPositionIndexes;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.annotations.GwtIncompatible;
026
027import java.io.Serializable;
028import java.util.AbstractList;
029import java.util.Arrays;
030import java.util.Collection;
031import java.util.Collections;
032import java.util.Comparator;
033import java.util.List;
034import java.util.RandomAccess;
035
036/**
037 * Static utility methods pertaining to {@code char} primitives, that are not
038 * already found in either {@link Character} or {@link Arrays}.
039 *
040 * <p>All the operations in this class treat {@code char} values strictly
041 * numerically; they are neither Unicode-aware nor locale-dependent.
042 *
043 * <p>See the Guava User Guide article on <a href=
044 * "http://code.google.com/p/guava-libraries/wiki/PrimitivesExplained">
045 * primitive utilities</a>.
046 *
047 * @author Kevin Bourrillion
048 * @since 1.0
049 */
050@GwtCompatible(emulated = true)
051public final class Chars {
052  private Chars() {}
053
054  /**
055   * The number of bytes required to represent a primitive {@code char}
056   * value.
057   */
058  public static final int BYTES = Character.SIZE / Byte.SIZE;
059
060  /**
061   * Returns a hash code for {@code value}; equal to the result of invoking
062   * {@code ((Character) value).hashCode()}.
063   *
064   * @param value a primitive {@code char} value
065   * @return a hash code for the value
066   */
067  public static int hashCode(char value) {
068    return value;
069  }
070
071  /**
072   * Returns the {@code char} value that is equal to {@code value}, if possible.
073   *
074   * @param value any value in the range of the {@code char} type
075   * @return the {@code char} value that equals {@code value}
076   * @throws IllegalArgumentException if {@code value} is greater than {@link
077   *     Character#MAX_VALUE} or less than {@link Character#MIN_VALUE}
078   */
079  public static char checkedCast(long value) {
080    char result = (char) value;
081    if (result != value) {
082      // don't use checkArgument here, to avoid boxing
083      throw new IllegalArgumentException("Out of range: " + value);
084    }
085    return result;
086  }
087
088  /**
089   * Returns the {@code char} nearest in value to {@code value}.
090   *
091   * @param value any {@code long} value
092   * @return the same value cast to {@code char} if it is in the range of the
093   *     {@code char} type, {@link Character#MAX_VALUE} if it is too large,
094   *     or {@link Character#MIN_VALUE} if it is too small
095   */
096  public static char saturatedCast(long value) {
097    if (value > Character.MAX_VALUE) {
098      return Character.MAX_VALUE;
099    }
100    if (value < Character.MIN_VALUE) {
101      return Character.MIN_VALUE;
102    }
103    return (char) value;
104  }
105
106  /**
107   * Compares the two specified {@code char} values. The sign of the value
108   * returned is the same as that of {@code ((Character) a).compareTo(b)}.
109   *
110   * <p><b>Note for Java 7 and later:</b> this method should be treated as
111   * deprecated; use the equivalent {@link Character#compare} method instead.
112   *
113   * @param a the first {@code char} to compare
114   * @param b the second {@code char} to compare
115   * @return a negative value if {@code a} is less than {@code b}; a positive
116   *     value if {@code a} is greater than {@code b}; or zero if they are equal
117   */
118  public static int compare(char a, char b) {
119    return a - b; // safe due to restricted range
120  }
121
122  /**
123   * Returns {@code true} if {@code target} is present as an element anywhere in
124   * {@code array}.
125   *
126   * @param array an array of {@code char} values, possibly empty
127   * @param target a primitive {@code char} value
128   * @return {@code true} if {@code array[i] == target} for some value of {@code
129   *     i}
130   */
131  public static boolean contains(char[] array, char target) {
132    for (char value : array) {
133      if (value == target) {
134        return true;
135      }
136    }
137    return false;
138  }
139
140  /**
141   * Returns the index of the first appearance of the value {@code target} in
142   * {@code array}.
143   *
144   * @param array an array of {@code char} values, possibly empty
145   * @param target a primitive {@code char} value
146   * @return the least index {@code i} for which {@code array[i] == target}, or
147   *     {@code -1} if no such index exists.
148   */
149  public static int indexOf(char[] array, char target) {
150    return indexOf(array, target, 0, array.length);
151  }
152
153  // TODO(kevinb): consider making this public
154  private static int indexOf(
155      char[] array, char target, int start, int end) {
156    for (int i = start; i < end; i++) {
157      if (array[i] == target) {
158        return i;
159      }
160    }
161    return -1;
162  }
163
164  /**
165   * Returns the start position of the first occurrence of the specified {@code
166   * target} within {@code array}, or {@code -1} if there is no such occurrence.
167   *
168   * <p>More formally, returns the lowest index {@code i} such that {@code
169   * java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
170   * the same elements as {@code target}.
171   *
172   * @param array the array to search for the sequence {@code target}
173   * @param target the array to search for as a sub-sequence of {@code array}
174   */
175  public static int indexOf(char[] array, char[] target) {
176    checkNotNull(array, "array");
177    checkNotNull(target, "target");
178    if (target.length == 0) {
179      return 0;
180    }
181
182    outer:
183    for (int i = 0; i < array.length - target.length + 1; i++) {
184      for (int j = 0; j < target.length; j++) {
185        if (array[i + j] != target[j]) {
186          continue outer;
187        }
188      }
189      return i;
190    }
191    return -1;
192  }
193
194  /**
195   * Returns the index of the last appearance of the value {@code target} in
196   * {@code array}.
197   *
198   * @param array an array of {@code char} values, possibly empty
199   * @param target a primitive {@code char} value
200   * @return the greatest index {@code i} for which {@code array[i] == target},
201   *     or {@code -1} if no such index exists.
202   */
203  public static int lastIndexOf(char[] array, char target) {
204    return lastIndexOf(array, target, 0, array.length);
205  }
206
207  // TODO(kevinb): consider making this public
208  private static int lastIndexOf(
209      char[] array, char target, int start, int end) {
210    for (int i = end - 1; i >= start; i--) {
211      if (array[i] == target) {
212        return i;
213      }
214    }
215    return -1;
216  }
217
218  /**
219   * Returns the least value present in {@code array}.
220   *
221   * @param array a <i>nonempty</i> array of {@code char} values
222   * @return the value present in {@code array} that is less than or equal to
223   *     every other value in the array
224   * @throws IllegalArgumentException if {@code array} is empty
225   */
226  public static char min(char... array) {
227    checkArgument(array.length > 0);
228    char min = array[0];
229    for (int i = 1; i < array.length; i++) {
230      if (array[i] < min) {
231        min = array[i];
232      }
233    }
234    return min;
235  }
236
237  /**
238   * Returns the greatest value present in {@code array}.
239   *
240   * @param array a <i>nonempty</i> array of {@code char} values
241   * @return the value present in {@code array} that is greater than or equal to
242   *     every other value in the array
243   * @throws IllegalArgumentException if {@code array} is empty
244   */
245  public static char max(char... array) {
246    checkArgument(array.length > 0);
247    char max = array[0];
248    for (int i = 1; i < array.length; i++) {
249      if (array[i] > max) {
250        max = array[i];
251      }
252    }
253    return max;
254  }
255
256  /**
257   * Returns the values from each provided array combined into a single array.
258   * For example, {@code concat(new char[] {a, b}, new char[] {}, new
259   * char[] {c}} returns the array {@code {a, b, c}}.
260   *
261   * @param arrays zero or more {@code char} arrays
262   * @return a single array containing all the values from the source arrays, in
263   *     order
264   */
265  public static char[] concat(char[]... arrays) {
266    int length = 0;
267    for (char[] array : arrays) {
268      length += array.length;
269    }
270    char[] result = new char[length];
271    int pos = 0;
272    for (char[] array : arrays) {
273      System.arraycopy(array, 0, result, pos, array.length);
274      pos += array.length;
275    }
276    return result;
277  }
278
279  /**
280   * Returns a big-endian representation of {@code value} in a 2-element byte
281   * array; equivalent to {@code
282   * ByteBuffer.allocate(2).putChar(value).array()}.  For example, the input
283   * value {@code '\\u5432'} would yield the byte array {@code {0x54, 0x32}}.
284   *
285   * <p>If you need to convert and concatenate several values (possibly even of
286   * different types), use a shared {@link java.nio.ByteBuffer} instance, or use
287   * {@link com.google.common.io.ByteStreams#newDataOutput()} to get a growable
288   * buffer.
289   */
290  @GwtIncompatible("doesn't work")
291  public static byte[] toByteArray(char value) {
292    return new byte[] {
293        (byte) (value >> 8),
294        (byte) value};
295  }
296
297  /**
298   * Returns the {@code char} value whose big-endian representation is
299   * stored in the first 2 bytes of {@code bytes}; equivalent to {@code
300   * ByteBuffer.wrap(bytes).getChar()}. For example, the input byte array
301   * {@code {0x54, 0x32}} would yield the {@code char} value {@code '\\u5432'}.
302   *
303   * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that
304   * library exposes much more flexibility at little cost in readability.
305   *
306   * @throws IllegalArgumentException if {@code bytes} has fewer than 2
307   *     elements
308   */
309  @GwtIncompatible("doesn't work")
310  public static char fromByteArray(byte[] bytes) {
311    checkArgument(bytes.length >= BYTES,
312        "array too small: %s < %s", bytes.length, BYTES);
313    return fromBytes(bytes[0], bytes[1]);
314  }
315
316  /**
317   * Returns the {@code char} value whose byte representation is the given 2
318   * bytes, in big-endian order; equivalent to {@code Chars.fromByteArray(new
319   * byte[] {b1, b2})}.
320   *
321   * @since 7.0
322   */
323  @GwtIncompatible("doesn't work")
324  public static char fromBytes(byte b1, byte b2) {
325    return (char) ((b1 << 8) | (b2 & 0xFF));
326  }
327
328  /**
329   * Returns an array containing the same values as {@code array}, but
330   * guaranteed to be of a specified minimum length. If {@code array} already
331   * has a length of at least {@code minLength}, it is returned directly.
332   * Otherwise, a new array of size {@code minLength + padding} is returned,
333   * containing the values of {@code array}, and zeroes in the remaining places.
334   *
335   * @param array the source array
336   * @param minLength the minimum length the returned array must guarantee
337   * @param padding an extra amount to "grow" the array by if growth is
338   *     necessary
339   * @throws IllegalArgumentException if {@code minLength} or {@code padding} is
340   *     negative
341   * @return an array containing the values of {@code array}, with guaranteed
342   *     minimum length {@code minLength}
343   */
344  public static char[] ensureCapacity(
345      char[] array, int minLength, int padding) {
346    checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
347    checkArgument(padding >= 0, "Invalid padding: %s", padding);
348    return (array.length < minLength)
349        ? copyOf(array, minLength + padding)
350        : array;
351  }
352
353  // Arrays.copyOf() requires Java 6
354  private static char[] copyOf(char[] original, int length) {
355    char[] copy = new char[length];
356    System.arraycopy(original, 0, copy, 0, Math.min(original.length, length));
357    return copy;
358  }
359
360  /**
361   * Returns a string containing the supplied {@code char} values separated
362   * by {@code separator}. For example, {@code join("-", '1', '2', '3')} returns
363   * the string {@code "1-2-3"}.
364   *
365   * @param separator the text that should appear between consecutive values in
366   *     the resulting string (but not at the start or end)
367   * @param array an array of {@code char} values, possibly empty
368   */
369  public static String join(String separator, char... array) {
370    checkNotNull(separator);
371    int len = array.length;
372    if (len == 0) {
373      return "";
374    }
375
376    StringBuilder builder
377        = new StringBuilder(len + separator.length() * (len - 1));
378    builder.append(array[0]);
379    for (int i = 1; i < len; i++) {
380      builder.append(separator).append(array[i]);
381    }
382    return builder.toString();
383  }
384
385  /**
386   * Returns a comparator that compares two {@code char} arrays
387   * lexicographically. That is, it compares, using {@link
388   * #compare(char, char)}), the first pair of values that follow any
389   * common prefix, or when one array is a prefix of the other, treats the
390   * shorter array as the lesser. For example,
391   * {@code [] < ['a'] < ['a', 'b'] < ['b']}.
392   *
393   * <p>The returned comparator is inconsistent with {@link
394   * Object#equals(Object)} (since arrays support only identity equality), but
395   * it is consistent with {@link Arrays#equals(char[], char[])}.
396   *
397   * @see <a href="http://en.wikipedia.org/wiki/Lexicographical_order">
398   *     Lexicographical order article at Wikipedia</a>
399   * @since 2.0
400   */
401  public static Comparator<char[]> lexicographicalComparator() {
402    return LexicographicalComparator.INSTANCE;
403  }
404
405  private enum LexicographicalComparator implements Comparator<char[]> {
406    INSTANCE;
407
408    @Override
409    public int compare(char[] left, char[] right) {
410      int minLength = Math.min(left.length, right.length);
411      for (int i = 0; i < minLength; i++) {
412        int result = Chars.compare(left[i], right[i]);
413        if (result != 0) {
414          return result;
415        }
416      }
417      return left.length - right.length;
418    }
419  }
420
421  /**
422   * Copies a collection of {@code Character} instances into a new array of
423   * primitive {@code char} values.
424   *
425   * <p>Elements are copied from the argument collection as if by {@code
426   * collection.toArray()}.  Calling this method is as thread-safe as calling
427   * that method.
428   *
429   * @param collection a collection of {@code Character} objects
430   * @return an array containing the same values as {@code collection}, in the
431   *     same order, converted to primitives
432   * @throws NullPointerException if {@code collection} or any of its elements
433   *     is null
434   */
435  public static char[] toArray(Collection<Character> collection) {
436    if (collection instanceof CharArrayAsList) {
437      return ((CharArrayAsList) collection).toCharArray();
438    }
439
440    Object[] boxedArray = collection.toArray();
441    int len = boxedArray.length;
442    char[] array = new char[len];
443    for (int i = 0; i < len; i++) {
444      // checkNotNull for GWT (do not optimize)
445      array[i] = (Character) checkNotNull(boxedArray[i]);
446    }
447    return array;
448  }
449
450  /**
451   * Returns a fixed-size list backed by the specified array, similar to {@link
452   * Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)},
453   * but any attempt to set a value to {@code null} will result in a {@link
454   * NullPointerException}.
455   *
456   * <p>The returned list maintains the values, but not the identities, of
457   * {@code Character} objects written to or read from it.  For example, whether
458   * {@code list.get(0) == list.get(0)} is true for the returned list is
459   * unspecified.
460   *
461   * @param backingArray the array to back the list
462   * @return a list view of the array
463   */
464  public static List<Character> asList(char... backingArray) {
465    if (backingArray.length == 0) {
466      return Collections.emptyList();
467    }
468    return new CharArrayAsList(backingArray);
469  }
470
471  @GwtCompatible
472  private static class CharArrayAsList extends AbstractList<Character>
473      implements RandomAccess, Serializable {
474    final char[] array;
475    final int start;
476    final int end;
477
478    CharArrayAsList(char[] array) {
479      this(array, 0, array.length);
480    }
481
482    CharArrayAsList(char[] array, int start, int end) {
483      this.array = array;
484      this.start = start;
485      this.end = end;
486    }
487
488    @Override public int size() {
489      return end - start;
490    }
491
492    @Override public boolean isEmpty() {
493      return false;
494    }
495
496    @Override public Character get(int index) {
497      checkElementIndex(index, size());
498      return array[start + index];
499    }
500
501    @Override public boolean contains(Object target) {
502      // Overridden to prevent a ton of boxing
503      return (target instanceof Character)
504          && Chars.indexOf(array, (Character) target, start, end) != -1;
505    }
506
507    @Override public int indexOf(Object target) {
508      // Overridden to prevent a ton of boxing
509      if (target instanceof Character) {
510        int i = Chars.indexOf(array, (Character) target, start, end);
511        if (i >= 0) {
512          return i - start;
513        }
514      }
515      return -1;
516    }
517
518    @Override public int lastIndexOf(Object target) {
519      // Overridden to prevent a ton of boxing
520      if (target instanceof Character) {
521        int i = Chars.lastIndexOf(array, (Character) target, start, end);
522        if (i >= 0) {
523          return i - start;
524        }
525      }
526      return -1;
527    }
528
529    @Override public Character set(int index, Character element) {
530      checkElementIndex(index, size());
531      char oldValue = array[start + index];
532      // checkNotNull for GWT (do not optimize)
533      array[start + index] = checkNotNull(element);
534      return oldValue;
535    }
536
537    @Override public List<Character> subList(int fromIndex, int toIndex) {
538      int size = size();
539      checkPositionIndexes(fromIndex, toIndex, size);
540      if (fromIndex == toIndex) {
541        return Collections.emptyList();
542      }
543      return new CharArrayAsList(array, start + fromIndex, start + toIndex);
544    }
545
546    @Override public boolean equals(Object object) {
547      if (object == this) {
548        return true;
549      }
550      if (object instanceof CharArrayAsList) {
551        CharArrayAsList that = (CharArrayAsList) object;
552        int size = size();
553        if (that.size() != size) {
554          return false;
555        }
556        for (int i = 0; i < size; i++) {
557          if (array[start + i] != that.array[that.start + i]) {
558            return false;
559          }
560        }
561        return true;
562      }
563      return super.equals(object);
564    }
565
566    @Override public int hashCode() {
567      int result = 1;
568      for (int i = start; i < end; i++) {
569        result = 31 * result + Chars.hashCode(array[i]);
570      }
571      return result;
572    }
573
574    @Override public String toString() {
575      StringBuilder builder = new StringBuilder(size() * 3);
576      builder.append('[').append(array[start]);
577      for (int i = start + 1; i < end; i++) {
578        builder.append(", ").append(array[i]);
579      }
580      return builder.append(']').toString();
581    }
582
583    char[] toCharArray() {
584      // Arrays.copyOfRange() is not available under GWT
585      int size = size();
586      char[] result = new char[size];
587      System.arraycopy(array, start, result, 0, size);
588      return result;
589    }
590
591    private static final long serialVersionUID = 0;
592  }
593}