Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. /**
  2. * Emulate {@code [...].every(element => <boolean value check>)}
  3. * <p>Trivial using new Streams.
  4. */
  5. public static <T> boolean every (final T[] array, final java.util.function.Predicate<T> predicate) {
  6. if ((array == null) || (array.length == 0) || (predicate == null)) {
  7. return false;
  8. }
  9. return Arrays.stream(array).allMatch(predicate);
  10. }
  11.  
  12. /**
  13. * Emulate {@code [...].some(element => <boolean value check>)}
  14. * <p>Trivial using new Streams.
  15. */
  16. public static <T> boolean some (final T[] array, final java.util.function.Predicate<T> predicate) {
  17. if ((array == null) || (array.length == 0) || (predicate == null)) {
  18. return false;
  19. }
  20. return Arrays.stream(array).anyMatch(predicate);
  21. }
  22.  
  23. /**
  24. * Emulate {@code [...].includes(value)}
  25. * <p>Trivial using Lists.
  26. * <p>If you need to make use of a java.util.Comparator or java.util.function.Predicate function instead, use #some above.
  27. */
  28. public static <T> boolean includes (final T[] array, final T search) {
  29. if ((array == null) || (array.length == 0)) {
  30. return false;
  31. }
  32. return Arrays.asList(array).contains(search);
  33. }
  34.  
  35. /**
  36. * Emulate {@code [...].indexOf(value)}
  37. * <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
  38. * array. Returns -1 if no matching element found.
  39. * <p>Trivial using Lists.
  40. */
  41. public static <T> int indexOf (final T[] array, final T search) {
  42. if ((array == null) || (array.length == 0)) {
  43. return -1;
  44. }
  45. return ((java.util.List<T>) Arrays.asList(array)).indexOf(search);
  46. }
  47.  
  48. /**
  49. * Emulate {@code [...].findIndex(element => <boolean value check>)}
  50. * <p>Like above, but uses a predicate to find the index to the first matching element, rather than straight value comparison.
  51. * <p>There's no equivalent method in Java already.
  52. */
  53. public static <T> int findIndex (final T[] array, final java.util.function.Predicate<T> predicate) {
  54. for (int index = 0, len = (((array == null) || (predicate == null)) ? 0 : array.length); index < len; index++) {
  55. if (predicate.test(array[index])) {
  56. return index;
  57. }
  58. }
  59. return -1;
  60. }
  61.  
  62. /**
  63. * Emulate {@code [...].find(element => <boolean value check>)}
  64. * <p>Like above, but returns the first matching element, rather than the index to it within the array.
  65. * <p>There's no equivalent method out there, so going to reuse the previous method.
  66. */
  67. public static <T> T find (final T[] array, final java.util.function.Predicate<T> predicate) {
  68. final int index = findIndex(array, predicate);
  69. if (index < 0) {
  70. return null;
  71. }
  72. return array[index];
  73. }
  74.  
  75. /**
  76. * Emulate {@code [...].filter(element => <boolean value check>)}
  77. * <p>Somewhat trivial using new Streams.
  78. */
  79. @SuppressWarnings("unchecked")
  80. public static <T> T[] filter (final T[] array, final java.util.function.Predicate<T> predicate) {
  81. if ((array == null) || (array.length == 0) || (predicate == null)) {
  82. return Collections.emptyList();
  83. }
  84. return (T[]) Arrays.stream(array).filter(predicate).collect(Collectors.toList()).toArray();
  85. }
  86.  
  87. /**
  88. * Emulate {@code [...].map(element => avalue)}
  89. * <p>Somewhat trivial using new Streams.
  90. */
  91. @SuppressWarnings("unchecked")
  92. public static <S, T> S[] map (final T[] array, final java.util.function.Function<T> mapper) {
  93. if ((array == null) || (array.length == 0) || (mapper == null)) {
  94. return false;
  95. }
  96. return (S[]) Arrays.stream(array).map(mapper).collect(Collectors.toList()).toArray();
  97. }
  98.  
  99. /**
  100. * Emulate {@code [...].reduce((accumulator, element) => accumulator, accumulator)}
  101. * <p>Somewhat trivial using new Streams.
  102. */
  103. public static <S, T> S reduce (final T[] array, final java.util.function.BiFunction<S, T, S> reducer, S accumulator) {
  104. if ((array == null) || (array.length == 0) || (reducer == null)) {
  105. return false;
  106. }
  107. return Arrays.stream(array).reduce(accumulator, reducer, (acc, s) -> s);
  108. }
  109.  
  110. /**
  111. * Emulate {@code [...].forEach(element => {...})}
  112. * <p>Trivial using anything in Java, but here you are anyway, with a happily shorter name, and with chainability.
  113. */
  114. public static <T> T[] each (final T[] array, final java.util.function.Consumer<T> action) {
  115. if ((array != null) && (action != null)) {
  116. Arrays.stream(array).forEach(action);
  117. }
  118. return array;
  119. }
  120.  
  121. /**
  122. * Emulate {@code [...].sort(value)}
  123. * <p>Trivial using Arrays or Lists, but here you are anyway.
  124. */
  125. public static <T> T[] sort (final T[] array, ...) {
  126. // incomplete
  127. }
  128.  
  129. /**
  130. * Emulate {@code [...].reverse(value)}
  131. * <p>Trivial using Lists.
  132. */
  133. public static <T> T[] reverse (final T[] array, ...) {
  134. // incomplete
  135. }
  136.  
  137. /**
  138. * Emulate {@code [...].slice(value)}
  139. * <p>Trivial using Lists.
  140. */
  141. public static <T> T[] slice (final T[] array, ...) {
  142. // incomplete
  143. }
  144.  
  145. /**
  146. * Emulate {@code [...].join()}
  147. */
  148. public static <T> String join (final T[] array, ...) {
  149. // incomplete
  150. }
  151.  
  152. /**
  153. * Other Array methods that manipulate the array contents, such as removing or adding elements dynamically cannot be done in Java,
  154. * because arrays are statically-sized on creation. If you need this functionality, you simply cannot use arrays. Instead, use
  155. * java.util.ArrayList. Note that you will have to specifically create a new ArrayList; if you use java.util.Arrays.asList(...),
  156. * you will have a statically-sized List, so you will again be unable to add or remove elements. I prefer Google Guava for creating
  157. * lists, rather than using constructors (a la com.google.common.collect.Lists.newArrayList(...)).
  158. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement