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 MRJArrays.mismatch(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) < 0; } /** * 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 MRJArrays.mismatch(a, 0, a.length, b, 0, b.length); } /** * 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) { int aLength = aToIndex - aFromIndex; int bLength = bToIndex - bFromIndex; if (aFromIndex < 0) { throw new ArrayIndexOutOfBoundsException("aFromIndex (" + aFromIndex + ") < 0"); } else if (aToIndex > a.length) { throw new ArrayIndexOutOfBoundsException("aToIndex (" + aToIndex + ") > a.length (" + a.length + ")"); } else if (bFromIndex < 0) { throw new ArrayIndexOutOfBoundsException("bFromIndex (" + bFromIndex + ") < 0"); } else if (bToIndex > b.length) { throw new ArrayIndexOutOfBoundsException("bToIndex (" + bToIndex + ") > b.length (" + b.length + ")"); } else if (aLength < 0) { throw new IllegalArgumentException("aFromIndex (" + aFromIndex + ") smaller than aToIndex (" + aToIndex + ")"); } else if (bLength < 0) { throw new IllegalArgumentException("bFromIndex (" + bFromIndex + ") smaller than bToIndex (" + bToIndex + ")"); } int length = Math.min(aLength, bLength); for (int i = 0; i < length; i++) { if (a[aFromIndex++] != b[bFromIndex++]) { return i; } } if (aLength != bLength) { return length; } else { return -1; } } }