The reason seems to be the number of memory allocations. In order to create the union of 100 lists we have 99 memory allocations. The first needs the space for the first two lists, the second the space for the first three lists, and so on. We can reduce the number of allocations drastically (in many cases to one) by leveraging the fact that many of the lists were already sorted, non-overlapping and increasing, so that we can simply concatenate them.
84 lines
2.5 KiB
Java
84 lines
2.5 KiB
Java
package org.lucares.utils;
|
|
|
|
import java.text.MessageFormat;
|
|
import java.util.Objects;
|
|
|
|
public class Preconditions {
|
|
public static void checkEven(final long value, final String message) {
|
|
if (value % 2 != 0) {
|
|
throw new IllegalStateException(message + ". Was: " + value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param a
|
|
* @param b
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args
|
|
*/
|
|
public static void checkGreater(final long a, final long b, final String message, final Object... args) {
|
|
|
|
if (a <= b) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args) + " Expected: " + a + " > " + b);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param a
|
|
* @param b
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args
|
|
* @throws IllegalStateException if {@code a} is not greater or equal to
|
|
* {@code b}
|
|
*/
|
|
public static void checkGreaterOrEqual(final long a, final long b, final String message, final Object... args) {
|
|
if (a < b) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args) + " Expected: " + a + " >= " + b);
|
|
}
|
|
}
|
|
|
|
public static void checkEqual(final Object actual, final Object expected) {
|
|
checkEqual(actual, expected, "expected {0} is equal to {1}", actual, expected);
|
|
}
|
|
|
|
/**
|
|
* Check that the given values are equal. The check is done with
|
|
* {@link Objects#equals(Object, Object)}
|
|
*
|
|
* @param actual the actual value
|
|
* @param expected the expected value
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args arguments for the message
|
|
* @throws IllegalStateException if {@code actual} is not equal to
|
|
* {@code expected}
|
|
*/
|
|
public static void checkEqual(final Object actual, final Object expected, final String message,
|
|
final Object... args) {
|
|
if (!Objects.equals(actual, expected)) {
|
|
throw new IllegalStateException(
|
|
MessageFormat.format(message, args) + " Expected: " + actual + " equals " + expected);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check that the given value is true.
|
|
*
|
|
* @param actual must be true
|
|
* @param message formatted with {@link MessageFormat}
|
|
* @param args arguments for the message
|
|
* @throws IllegalStateException if {@code actual} is not true
|
|
*/
|
|
public static void checkTrue(final boolean actual, final String message, final Object... args) {
|
|
checkEqual(actual, true, message, args);
|
|
}
|
|
|
|
public static void checkNull(final Object actual, final String message, final Object... args) {
|
|
if (actual != null) {
|
|
throw new IllegalStateException(MessageFormat.format(message, args));
|
|
}
|
|
}
|
|
|
|
}
|