aboutsummaryrefslogtreecommitdiff
path: root/guava/src/com/google/common/primitives/Chars.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava/src/com/google/common/primitives/Chars.java')
-rw-r--r--guava/src/com/google/common/primitives/Chars.java54
1 files changed, 52 insertions, 2 deletions
diff --git a/guava/src/com/google/common/primitives/Chars.java b/guava/src/com/google/common/primitives/Chars.java
index 4a2e3a344..728c6e5e9 100644
--- a/guava/src/com/google/common/primitives/Chars.java
+++ b/guava/src/com/google/common/primitives/Chars.java
@@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
-import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import java.io.Serializable;
@@ -258,7 +257,6 @@ public final class Chars {
* @throws IllegalArgumentException if {@code min > max}
* @since 21.0
*/
- @Beta
public static char constrainToRange(char value, char min, char max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return value < min ? min : value < max ? value : max;
@@ -489,6 +487,56 @@ public final class Chars {
}
/**
+ * Performs a right rotation of {@code array} of "distance" places, so that the first element is
+ * moved to index "distance", and the element at index {@code i} ends up at index {@code (distance
+ * + i) mod array.length}. This is equivalent to {@code Collections.rotate(Chars.asList(array),
+ * distance)}, but is considerably faster and avoids allocation and garbage collection.
+ *
+ * <p>The provided "distance" may be negative, which will rotate left.
+ *
+ * @since 32.0.0
+ */
+ public static void rotate(char[] array, int distance) {
+ rotate(array, distance, 0, array.length);
+ }
+
+ /**
+ * Performs a right rotation of {@code array} between {@code fromIndex} inclusive and {@code
+ * toIndex} exclusive. This is equivalent to {@code
+ * Collections.rotate(Chars.asList(array).subList(fromIndex, toIndex), distance)}, but is
+ * considerably faster and avoids allocations and garbage collection.
+ *
+ * <p>The provided "distance" may be negative, which will rotate left.
+ *
+ * @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or
+ * {@code toIndex > fromIndex}
+ * @since 32.0.0
+ */
+ public static void rotate(char[] array, int distance, int fromIndex, int toIndex) {
+ // See Ints.rotate for more details about possible algorithms here.
+ checkNotNull(array);
+ checkPositionIndexes(fromIndex, toIndex, array.length);
+ if (array.length <= 1) {
+ return;
+ }
+
+ int length = toIndex - fromIndex;
+ // Obtain m = (-distance mod length), a non-negative value less than "length". This is how many
+ // places left to rotate.
+ int m = -distance % length;
+ m = (m < 0) ? m + length : m;
+ // The current index of what will become the first element of the rotated section.
+ int newFirstIndex = m + fromIndex;
+ if (newFirstIndex == fromIndex) {
+ return;
+ }
+
+ reverse(array, fromIndex, newFirstIndex);
+ reverse(array, newFirstIndex, toIndex);
+ reverse(array, fromIndex, toIndex);
+ }
+
+ /**
* Returns a fixed-size list backed by the specified array, similar to {@link
* Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any attempt to
* set a value to {@code null} will result in a {@link NullPointerException}.
@@ -497,6 +545,8 @@ public final class Chars {
* written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for
* the returned list is unspecified.
*
+ * <p>The returned list is serializable.
+ *
* @param backingArray the array to back the list
* @return a list view of the array
*/