package org.stianloader.mrjmania; import java.util.Arrays; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Range; /** * Polyfills for array-related methods. * *
Mostly methods that are/should be in the {@link Arrays} class. */ public class MRJArrays { private MRJArrays() { throw new UnsupportedOperationException(); } /** * Assert that the two array sections, which should be of equal length, are equal to each other in content. * * @param a The array {@code a} * @param aFromIndex The first index in array {@code a} of the section for that array * @param aToIndex The last index in array {@code a} of the section for that array * @param b The array {@code b} * @param bFromIndex The first index in array {@code b} of the section for that array * @param bToIndex The last index in array {@code b} of the section for that array * @return {@code true} if the array section contents are equal, {@code false} otherwise. */ @Contract(pure = true) public static final boolean equals(byte @NotNull[] a, @Range(from = 0, to = Integer.MAX_VALUE) int aFromIndex, @Range(from = 0, to = Integer.MAX_VALUE) int aToIndex, byte @NotNull[] b, @Range(from = 0, to = Integer.MAX_VALUE) int bFromIndex, @Range(from = 0, to = Integer.MAX_VALUE) int bToIndex) { return Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex); } /** * Return the index where the two arrays first diverge from each other. * *
If the arrays do not diverge (i.e. are equal to each), {@code -1} is returned. * * @param a The array {@code a} * @param b The array {@code b} * @return The index where the two arrays first converge between each other, or {@code -1} if the two arrays are equal. */ @Range(from = -1, to = Integer.MAX_VALUE) @Contract(pure = true) public static final int mismatch(byte @NotNull[] a, byte @NotNull[] b) { return Arrays.mismatch(a, b); } /** * Return the index where the two arrays first converge between each other in a given interval. * *
If the length of the two array sections don't match, then the two arrays are not considered * equal and the length of the smallest section is returned, provided there isn't an earlier mismatch. * *
If the array sections do not diverge and are of equal length (i.e. are equal to each), then {@code -1} is returned. * *
The returned value is in relative to aFromIndex/bFromIndex! As such, it will never be larger * than either value. * * @param a The array {@code a} * @param aFromIndex The first index in array {@code a} of the section for that array * @param aToIndex The last index in array {@code a} of the section for that array * @param b The array {@code b} * @param bFromIndex The first index in array {@code b} of the section for that array * @param bToIndex The last index in array {@code b} of the section for that array * @return The index of the first mismatch, relative from aFromIndex/bFromIndex. */ @Range(from = -1, to = Integer.MAX_VALUE) @Contract(pure = true) public static final int mismatch(byte @NotNull[] a, @Range(from = 0, to = Integer.MAX_VALUE) int aFromIndex, @Range(from = 0, to = Integer.MAX_VALUE) int aToIndex, byte @NotNull[] b, @Range(from = 0, to = Integer.MAX_VALUE) int bFromIndex, @Range(from = 0, to = Integer.MAX_VALUE) int bToIndex) { return Arrays.mismatch(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex); } }