Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Emulate {@code [...].every(element => <boolean value check>)}
- * <p>Trivial using new Streams.
- */
- public static <T> boolean every (final T[] array, final java.util.function.Predicate<T> predicate) {
- if ((array == null) || (array.length == 0) || (predicate == null)) {
- return false;
- }
- return Arrays.stream(array).allMatch(predicate);
- }
- /**
- * Emulate {@code [...].some(element => <boolean value check>)}
- * <p>Trivial using new Streams.
- */
- public static <T> boolean some (final T[] array, final java.util.function.Predicate<T> predicate) {
- if ((array == null) || (array.length == 0) || (predicate == null)) {
- return false;
- }
- return Arrays.stream(array).anyMatch(predicate);
- }
- /**
- * Emulate {@code [...].includes(value)}
- * <p>Trivial using Lists.
- * <p>If you need to make use of a java.util.Comparator or java.util.function.Predicate function instead, use #some above.
- */
- public static <T> boolean includes (final T[] array, final T search) {
- if ((array == null) || (array.length == 0)) {
- return false;
- }
- return Arrays.asList(array).contains(search);
- }
- /**
- * Emulate {@code [...].indexOf(value)}
- * <p>This is like the #includes method above, but instead of a truth value, you want the index to the first matching value in the
- * array. Returns -1 if no matching element found.
- * <p>Trivial using Lists.
- */
- public static <T> int indexOf (final T[] array, final T search) {
- if ((array == null) || (array.length == 0)) {
- return -1;
- }
- return ((java.util.List<T>) Arrays.asList(array)).indexOf(search);
- }
- /**
- * Emulate {@code [...].findIndex(element => <boolean value check>)}
- * <p>Like above, but uses a predicate to find the index to the first matching element, rather than straight value comparison.
- * <p>There's no equivalent method in Java already.
- */
- public static <T> int findIndex (final T[] array, final java.util.function.Predicate<T> predicate) {
- for (int index = 0, len = (((array == null) || (predicate == null)) ? 0 : array.length); index < len; index++) {
- if (predicate.test(array[index])) {
- return index;
- }
- }
- return -1;
- }
- /**
- * Emulate {@code [...].find(element => <boolean value check>)}
- * <p>Like above, but returns the first matching element, rather than the index to it within the array.
- * <p>There's no equivalent method out there, so going to reuse the previous method.
- */
- public static <T> T find (final T[] array, final java.util.function.Predicate<T> predicate) {
- final int index = findIndex(array, predicate);
- if (index < 0) {
- return null;
- }
- return array[index];
- }
- /**
- * Emulate {@code [...].filter(element => <boolean value check>)}
- * <p>Somewhat trivial using new Streams.
- */
- @SuppressWarnings("unchecked")
- public static <T> T[] filter (final T[] array, final java.util.function.Predicate<T> predicate) {
- if ((array == null) || (array.length == 0) || (predicate == null)) {
- return Collections.emptyList();
- }
- return (T[]) Arrays.stream(array).filter(predicate).collect(Collectors.toList()).toArray();
- }
- /**
- * Emulate {@code [...].map(element => avalue)}
- * <p>Somewhat trivial using new Streams.
- */
- @SuppressWarnings("unchecked")
- public static <S, T> S[] map (final T[] array, final java.util.function.Function<T> mapper) {
- if ((array == null) || (array.length == 0) || (mapper == null)) {
- return false;
- }
- return (S[]) Arrays.stream(array).map(mapper).collect(Collectors.toList()).toArray();
- }
- /**
- * Emulate {@code [...].reduce((accumulator, element) => accumulator, accumulator)}
- * <p>Somewhat trivial using new Streams.
- */
- public static <S, T> S reduce (final T[] array, final java.util.function.BiFunction<S, T, S> reducer, S accumulator) {
- if ((array == null) || (array.length == 0) || (reducer == null)) {
- return false;
- }
- return Arrays.stream(array).reduce(accumulator, reducer, (acc, s) -> s);
- }
- /**
- * Emulate {@code [...].forEach(element => {...})}
- * <p>Trivial using anything in Java, but here you are anyway, with a happily shorter name, and with chainability.
- */
- public static <T> T[] each (final T[] array, final java.util.function.Consumer<T> action) {
- if ((array != null) && (action != null)) {
- Arrays.stream(array).forEach(action);
- }
- return array;
- }
- /**
- * Emulate {@code [...].sort(value)}
- * <p>Trivial using Arrays or Lists, but here you are anyway.
- */
- public static <T> T[] sort (final T[] array, ...) {
- // incomplete
- }
- /**
- * Emulate {@code [...].reverse(value)}
- * <p>Trivial using Lists.
- */
- public static <T> T[] reverse (final T[] array, ...) {
- // incomplete
- }
- /**
- * Emulate {@code [...].slice(value)}
- * <p>Trivial using Lists.
- */
- public static <T> T[] slice (final T[] array, ...) {
- // incomplete
- }
- /**
- * Emulate {@code [...].join()}
- */
- public static <T> String join (final T[] array, ...) {
- // incomplete
- }
- /**
- * Other Array methods that manipulate the array contents, such as removing or adding elements dynamically cannot be done in Java,
- * because arrays are statically-sized on creation. If you need this functionality, you simply cannot use arrays. Instead, use
- * java.util.ArrayList. Note that you will have to specifically create a new ArrayList; if you use java.util.Arrays.asList(...),
- * you will have a statically-sized List, so you will again be unable to add or remove elements. I prefer Google Guava for creating
- * lists, rather than using constructors (a la com.google.common.collect.Lists.newArrayList(...)).
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement