
hm.binkley.util.Lists Maven / Gradle / Ivy
The newest version!
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to .
*/
package hm.binkley.util;
import javax.annotation.Nonnull;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntSupplier;
/**
* {@code Lists} has methods on {@code java.util.List}.
*
* @author B. K. Oxley (binkley)
*/
public final class Lists {
/**
* Partitions the given list into n buckets as
* evenly as possible. Buckets are contiguous sublists of
* list.
*
* @param list the list to partition, never missing
* @param n the bucket count, always positive
* @param the list item type
*
* @return the list of buckets, never missing
*/
@Nonnull
public static List> partition(@Nonnull final List list,
final int n) {
if (1 > n)
throw new IllegalArgumentException(
"Non-positive bucket count: " + n);
final int size = list.size();
final int div = size / n;
final int mod = size % n;
int start = 0;
final List> buckets = new ArrayList<>(n);
for (int i = 0; i < mod; ++i) {
final int end = start + div + 1;
buckets.add(list.subList(start, end));
start = end;
}
for (int i = mod; i < n; ++i) {
final int end = start + div;
buckets.add(list.subList(start, end));
start = end;
}
return buckets;
}
@Nonnull
public static List list(@Nonnull final FromIntFunction get,
@Nonnull final IntSupplier size) {
return new ListList<>(get, size);
}
private Lists() {
}
@FunctionalInterface
public interface FromIntFunction {
T apply(final int i);
}
private static final class ListList
extends AbstractList {
private final FromIntFunction get;
private final IntSupplier size;
public ListList(final FromIntFunction get,
final IntSupplier size) {
this.get = get;
this.size = size;
}
@Override
public T get(final int index) {
return get.apply(index);
}
@Override
public int size() {
return size.getAsInt();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy