public final class arrays extends StaticObject
Modifier and Type | Method and Description |
---|---|
static boolean |
equals(Seq<?> seq,
Object obj)
Unified method for compare to sequences for equality.
|
static <T,R> void |
forEach(Iterable<? extends T> values,
Function<? super T,? extends R> f)
Iterates over all elements of the given
values |
static <T,R> void |
forEach(T[] array,
Function<? super T,? extends R> f)
Iterates over all elements of the given
array as long as the
predicate returns true (which means continue) and
returns the index the iteration has been interrupted. |
static int |
hashCode(Seq<?> seq)
Unified method for calculating the hash code of every
Seq
implementation. |
static <T extends Object & Comparable<? super T>> |
isSorted(Seq<T> seq)
Test whether the given array is sorted in ascending order.
|
static <T> boolean |
isSorted(Seq<T> seq,
Comparator<? super T> comparator)
Test whether the given array is sorted in ascending order.
|
static <A,B> B[] |
map(A[] a,
B[] b,
Function<? super A,? extends B> converter)
Map the array from type A to an other array of type B.
|
static int[] |
partition(int size,
int parts)
Return a array with the indexes of the partitions of an array with the
given size.
|
static <T extends Object & Comparable<? super T>> |
sort(MSeq<T> array)
Calls the sort method on the
Arrays class. |
public static int hashCode(Seq<?> seq)
Seq
implementation. The hash code is defined as followed:
int hashCode = 1;
final Iterator<E> it = seq.iterator();
while (it.hasNext()) {
final E obj = it.next();
hashCode = 31*hashCode + (obj == null ? 0 : obj.hashCode());
}
seq
- the sequence to calculate the hash code for.Seq.hashCode()
,
List.hashCode()
public static boolean equals(Seq<?> seq, Object obj)
seq
- the sequence to test for equality.obj
- the object to test for equality with the sequence.true
if the given objects are sequences and contain the
same objects in the same order, false
otherwise.Seq.equals(Object)
public static <T extends Object & Comparable<? super T>> MSeq<T> sort(MSeq<T> array)
Arrays
class.T
- the array element typearray
- the array to sortNullPointerException
- if the give array is null
.UnsupportedOperationException
- if the array is sealed
(array.isSealed() == true
).public static <T extends Object & Comparable<? super T>> boolean isSorted(Seq<T> seq)
T
- the array element typeseq
- the array to test.true
if the given array
is sorted in ascending
order, false
otherwise.NullPointerException
- if the given array or one of it's element is
null
.public static <T> boolean isSorted(Seq<T> seq, Comparator<? super T> comparator)
T
- the array element typeseq
- the array to test.comparator
- the comparator which defines the order.true
if the given array
is sorted in ascending
order, false
otherwise.NullPointerException
- if the given array or one of it's element or
the comparator is null
.public static int[] partition(int size, int parts)
min(size, prts) + 1
.
Some examples:
partition(10, 3): [0, 3, 6, 10] partition(15, 6): [0, 2, 4, 6, 9, 12, 15] partition(5, 10): [0, 1, 2, 3, 4, 5]The following examples prints the start index (inclusive) and the end index (exclusive) of the
partition(15, 6)
.
int[] parts = partition(15, 6);
for (int i = 0; i < parts.length - 1; ++i) {
System.out.println(i + ": " + parts[i] + "\t" + parts[i + 1]);
}
0: 0 2 1: 2 4 2: 4 6 3: 6 9 4: 9 12 5: 12 15This example shows how this can be used in an concurrent environment:
try (final Concurrency c = Concurrency.start()) {
final int[] parts = arrays.partition(population.size(), _maxThreads);
for (int i = 0; i < parts.length - 1; ++i) {
final int part = i;
c.execute(new Runnable() { @Override public void run() {
for (int j = parts[part + 1]; --j >= parts[part];) {
population.get(j).evaluate();
}
}});
}
}
size
- the size of the array to partition.parts
- the number of parts the (virtual) array should be partitioned.min(size, parts) + 1
.IllegalArgumentException
- if size
or p
is less than one.public static <T,R> void forEach(T[] array, Function<? super T,? extends R> f)
array
as long as the
predicate
returns true
(which means continue) and
returns the index the iteration has been interrupted. -1 is returned if
all elements were visited.
Can be used to check all array elements for nullness.
public void foo(final Integer[] values) {
arrays.forEach(values, new Validator.NonNull());
...
}
T
- the array element typeR
- the returned type of the applied functionarray
- the array to iterate.f
- the function to apply to every element.NullPointerException
- if one of the elements are null
.public static <T,R> void forEach(Iterable<? extends T> values, Function<? super T,? extends R> f)
values
T
- the element typeR
- the returned type of the applied functionvalues
- the values to iterate.f
- the function to apply to each element.NullPointerException
- if one of the elements are null
.public static <A,B> B[] map(A[] a, B[] b, Function<? super A,? extends B> converter)
A
- the source type.B
- the target type.a
- the source array.b
- the target array. If the given array is to short a new array
with the right size is created, mapped and returned. If the given
array is long enough this array is returned.converter
- the converter needed for mapping from type A to type B.b
is long enough b
is
returned otherwise a new created array.NullPointerException
- if one of the arguments is null
.© 2007-2014 Franz Wilhelmstötter (2014-10-03 19:44)