Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.76 KB | None | 0 0
  1. package dc.j8common.statics;
  2.  
  3. import dc.j8common.func.TriConsumer;
  4.  
  5. import java.util.*;
  6. import java.util.function.*;
  7. import java.util.stream.Stream;
  8. import java.util.stream.StreamSupport;
  9.  
  10. import static java.util.stream.Collectors.toList;
  11. import static java.util.stream.Collectors.toMap;
  12.  
  13. /** Collections */
  14. public final class Cs {
  15. //region Collections
  16. public static <A> int indexOf(Iterable<A> iter, Predicate<A> predicate) {
  17. int i = 0;
  18. for (A a : iter) {
  19. if (predicate.test(a)) {
  20. return i;
  21. }
  22. i++;
  23. }
  24. return -1;
  25. }
  26.  
  27. public static <B, A extends Collection<B>> A raze(A in, A out, int count) {
  28. for (B b : in) {
  29. if (count-- < 0) {
  30. return out;
  31. }
  32. out.add(b);
  33. }
  34. return out;
  35. }
  36.  
  37. public static <T, A extends Collection<T>> A add(A col, T elem) {
  38. col.add(elem);
  39. return col;
  40. }
  41.  
  42. public static <T, A extends Collection<T>> A remove(A col, T elem) {
  43. col.remove(elem);
  44. return col;
  45. }
  46.  
  47. public static <T, A extends Collection<T>, B extends Collection<T>> A addAll(A col1, B col2) {
  48. col1.addAll(col2);
  49. return col1;
  50. }
  51.  
  52. public static <T, A extends Collection<T>, B extends Collection<T>> A addAll(A col1, B... cols) {
  53. for (B col : cols) {
  54. col1.addAll(col);
  55. }
  56. return col1;
  57. }
  58.  
  59. public static <T, A extends Collection<T>, B extends Collection<T>> A removeAll(A col1, B col2) {
  60. col1.removeAll(col2);
  61. return col1;
  62. }
  63.  
  64. public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
  65. boolean wasModified;
  66. for (wasModified = false; iterator.hasNext(); wasModified |= addTo.add(iterator.next())) {
  67. ;
  68. }
  69.  
  70. return wasModified;
  71. }
  72.  
  73. public static <A extends Collection> boolean isNullOrEmpty(A collection) {
  74. return !(collection != null && collection.size() != 0);
  75. }
  76. //endregion
  77.  
  78. //region Lists
  79. public static <T, A extends List<T>> A sort(A col1, Comparator<T> comp) {
  80. col1.sort(comp);
  81. return col1;
  82. }
  83.  
  84. public static <T extends Comparable<T>, A extends List<T>> A sort(A col1) {
  85. col1.sort(Comparable::compareTo);
  86. return col1;
  87. }
  88.  
  89. public static <A, B> List<B> map(List<A> lst, Function<A, B> f) {
  90. return lst.stream().map(f).collect(toList());
  91. }
  92.  
  93. public static <A> Optional<A> last(List<A> lst) {
  94. if (lst.size() == 0) {
  95. return Optional.empty();
  96. }
  97. return Optional.ofNullable(lst.get(lst.size() - 1));
  98. }
  99.  
  100. public static <A> List<A> list(Iterable<? extends A> iterable) {
  101. return Cs.stream(iterable).collect(toList());
  102. }
  103. //endregion
  104.  
  105. //region Maps
  106.  
  107. public static <K, V> Map<K, V> filter(Map<K, V> map, BiPredicate<K, V> filter) {
  108. return map.entrySet().stream()
  109. .filter(kvEntry -> filter.test(kvEntry.getKey(), kvEntry.getValue()))
  110. .collect(toMap(kvEntry -> kvEntry.getKey(), kvEntry -> kvEntry.getValue()));
  111. }
  112.  
  113. /** if no value is contained in map - add the value provided by a supplier, otherwise use remapping */
  114. public static <A, B> B compute(Map<A, B> map, A key, BiFunction<A, B, B> remapping, Supplier<B> startingValue) {
  115. return map.compute(key, (a, b) -> b == null ? startingValue.get() : remapping.apply(a, b));
  116. }
  117.  
  118. /** if no value is contained in map - add the value provided by a supplier, but in any case use remapping */
  119. public static <A, B> B computeAndApply(Map<A, B> map, A key, BiFunction<A, B, B> remap,
  120. Supplier<B> startingValueThenRemap) {
  121. return map.compute(key, (a, b) -> b == null ? remap.apply(a, startingValueThenRemap.get()) : remap.apply(a, b));
  122. }
  123.  
  124. /** invoke consumer if value exists */
  125. public static <A, B> void doIfExist(Map<A, B> map, A key, Consumer<B> doIfExist) {
  126. B b = map.get(key);
  127. if (b != null) {
  128. doIfExist.accept(b);
  129. }
  130. }
  131.  
  132. /** invoke one consumer if value exists and another otherwise */
  133. public static <A, B> void doInBothCases(Map<A, B> map, A key, Consumer<B> doIfExist, Runnable doIfNotExist) {
  134. B b = map.get(key);
  135. if (b != null) {
  136. doIfExist.accept(b);
  137. } else {
  138. doIfNotExist.run();
  139. }
  140. }
  141.  
  142. public static <T, X, A extends Map<T, X>, B extends Map<T, X>> A putAll(A collector, B map) {
  143. collector.putAll(map);
  144. return collector;
  145. }
  146.  
  147. public static <T, X, A extends Map<T, X>> A put(A map, T key, X val) {
  148. map.put(key, val);
  149. return map;
  150. }
  151.  
  152. public static <T, A extends Map<T, ?>> A remove(A map, T elem) {
  153. map.remove(elem);
  154. return map;
  155. }
  156. //endregion
  157.  
  158. //region Iteration and mapping: index, synchro, eachWithEach
  159.  
  160. /** index in iteration */
  161. public static <A> void forEachWithIndex(Iterable<A> iter, BiConsumer<A, Integer> consumer) {
  162. int i = 0;
  163. for (A next : iter) {
  164. consumer.accept(next, i++);
  165. }
  166. }
  167.  
  168. /** each element from one collection is applied to each element from another collection */
  169. public static <A, B> void forEachWithEach(Iterable<A> t1, Iterable<B> t2, BiConsumer<A, B> consumer) {
  170. for (A a : t1) {
  171. for (B b : t2) {
  172. consumer.accept(a, b);
  173. }
  174. }
  175. }
  176.  
  177. /** each element from one collection is applied to the element with the same index in another collection */
  178. public static <A, B> void forEachSynchro(Iterable<A> t1, Iterable<B> t2, BiConsumer<A, B> consumer) {
  179. forEachSynchro(t1, t2, (a, b, i) -> consumer.accept(a, b));
  180. }
  181.  
  182. /** each element from one collection is applied to the element with the same index in another collection */
  183. public static <A, B> void forEachSynchro(Iterable<A> t1, Iterable<B> t2, TriConsumer<A, B, Integer> consumerWithIndex) {
  184. Iterator<A> it1 = t1.iterator();
  185. Iterator<B> it2 = t2.iterator();
  186. int i = 0;
  187. while (it1.hasNext() || it2.hasNext()) {
  188. A a = (it1.hasNext()) ? it1.next() : null;
  189. B b = (it2.hasNext()) ? it2.next() : null;
  190. consumerWithIndex.accept(a, b, i++);
  191. }
  192. }
  193.  
  194. public static <A, B> Collection<B> mapEachWithIndex(Iterable<A> iter, BiFunction<A, Integer, B> function) {
  195. int i = 0;
  196. Collection<B> toRet = new ArrayList<>();
  197. for (A next : iter) {
  198. toRet.add(function.apply(next, i++));
  199. }
  200. return toRet;
  201. }
  202.  
  203. public static <A, B, C> Collection<C> mapSyncro(Iterable<A> t1, Iterable<B> t2, BiFunction<A, B, C> consumer) {
  204. Collection<C> result = new ArrayList<>();
  205. Cs.forEachSynchro(t1, t2, (a, b) -> result.add(consumer.apply(a, b)));
  206. return result;
  207. }
  208.  
  209. public static <A, B, C> Collection<C> mapEachWithEach(Iterable<A> t1, Iterable<B> t2,
  210. BiFunction<A, B, C> consumer) {
  211. Collection<C> result = new ArrayList<>();
  212. Cs.forEachWithEach(t1, t2, (a, b) -> result.add(consumer.apply(a, b)));
  213. return result;
  214. }
  215. //endregion
  216.  
  217. //region String joiners
  218. public static <A, B extends Iterable<A>> List<String> str(B a) {
  219. return str(a, Objects::toString);
  220. }
  221.  
  222. public static <A, B extends Iterable<A>> List<String> str(B a, Function<A, String> toStringer) {
  223. return StreamSupport.stream(a.spliterator(), false).map(toStringer::apply).collect(toList());
  224. }
  225.  
  226. public static <A, B extends Iterable<A>> String join(B a) {
  227. return join(",", a);
  228. }
  229.  
  230. public static <A, B extends Iterable<A>> String join(String delimeter, B a) {
  231. return join(delimeter, a, Objects::toString);
  232. }
  233.  
  234. public static <A, B extends Iterable<A>> String join(B a, Function<A, String> toStringer) {
  235. return join(",", a, toStringer);
  236. }
  237.  
  238. public static <A, B extends Iterable<A>> String join(String delimeter, B a, Function<A, String> toStringer) {
  239. return String.join(delimeter, str(a, toStringer));
  240. }
  241.  
  242. public static <A, B extends Stream<A>> String join(B a) {
  243. return join(",", a);
  244. }
  245.  
  246. public static <A, B extends Stream<A>> String join(String delimeter, B a) {
  247. return join(delimeter, a, Objects::toString);
  248. }
  249.  
  250. public static <A, B extends Stream<A>> String join(B a, Function<A, String> toStringer) {
  251. return join(",", a, toStringer);
  252. }
  253.  
  254. public static <A, B extends Stream<A>> String join(String delimeter, B a, Function<A, String> toStringer) {
  255. return String.join(delimeter, str(a.collect(toList()), toStringer));
  256. }
  257.  
  258. public static <K, V, B extends Map<K, V>> String
  259. joinMap(B a) {
  260. return joinMap(a, (k, v) -> k.toString(), (k, v) -> v.toString());
  261. }
  262.  
  263. public static <K, V, B extends Map<K, V>> String
  264. joinMap(B a, BiFunction<K, V, String> toStringerKey, BiFunction<K, V, String> toStringerValue) {
  265. return joinMap("|", ",", a, toStringerKey, toStringerValue);
  266. }
  267.  
  268. public static <K, V, B extends Map<K, V>> String
  269. joinMap(String itemDelimeter, String keyValDelimeter, B a) {
  270. return joinMap(itemDelimeter, keyValDelimeter, a, (k, v) -> k.toString(), (k, v) -> v.toString());
  271. }
  272.  
  273. public static <K, V, B extends Map<K, V>> String
  274. joinMap(String itemDelimeter, String keyValDelimeter,
  275. B a, BiFunction<K, V, String> toStringerKey, BiFunction<K, V, String> toStringerValue) {
  276. return join(itemDelimeter, a.entrySet(),
  277. o -> toStringerKey.apply(o.getKey(), o.getValue()) + keyValDelimeter +
  278. toStringerValue.apply(o.getKey(), o.getValue()));
  279. }
  280.  
  281. //endregion
  282.  
  283. //region Creators
  284. @SafeVarargs
  285. public static <T> List<T> of(T... ts) {
  286. return new ArrayList<>(java.util.Arrays.asList(ts));
  287. }
  288.  
  289. @SafeVarargs
  290. public static <T> T[] ofA(T... ts) {
  291. return ts;
  292. }
  293.  
  294. @SafeVarargs
  295. public static <T> Queue<T> ofQ(T... ts) {
  296. return new LinkedList<>(java.util.Arrays.asList(ts));
  297. }
  298.  
  299. @SafeVarargs
  300. public static <T> Set<T> ofS(T... ts) {
  301. return new HashSet<>(java.util.Arrays.asList(ts));
  302. }
  303.  
  304. @SafeVarargs
  305. public static <T> Set<T> ofTS(T... ts) {
  306. return new TreeSet<>(java.util.Arrays.asList(ts));
  307. }
  308.  
  309. /**
  310. * odd - key, even - value
  311. */
  312. public static <K, V> Map<K, V> ofM(Object... ts) {
  313. Map<K, V> ret = new HashMap<>();
  314. return fillMap(ret, ts);
  315. }
  316.  
  317. /**
  318. * odd - key, even - value
  319. */
  320. public static <K, V> Map<K, V> ofTM(Object... ts) {
  321. Map<K, V> ret = new TreeMap<>();
  322. return fillMap(ret, ts);
  323. }
  324.  
  325. private static <K, V> Map<K, V> fillMap(Map<K, V> ret, Object[] ts) {
  326. int step = 0;
  327. K key = null;
  328. V val = null;
  329. for (Object t : ts) {
  330. if (step == 0) {
  331. key = (K) t;
  332. step++;
  333. } else if (step == 1) {
  334. val = (V) t;
  335. ret.put(key, val);
  336. step = 0;
  337. }
  338. }
  339. if (step != 0) {
  340. throw new RuntimeException("Map unfilled!");
  341. }
  342. return ret;
  343. }
  344.  
  345. //endregion
  346.  
  347. //region Streams
  348. public static <T> Stream<T> stream(Iterable<T> iterable) {
  349. return StreamSupport.stream(iterable.spliterator(), false);
  350. }
  351.  
  352. public static <T> Stream<T> stream(T... arr) {
  353. return Stream.of(arr);
  354. }
  355.  
  356. @SuppressWarnings("unchecked")
  357. public static <T> Stream<T> addStream(Stream<T> stream1, Stream<T> stream2) {
  358. return Stream.concat(stream1, stream2);
  359. }
  360.  
  361. @SuppressWarnings("unchecked")
  362. public static <T> Stream<T> addStream(Stream<T> stream, T... items) {
  363. return Stream.concat(stream, Stream.of(items));
  364. }
  365.  
  366. public static <T> Stream<T> addStream(Stream<T> stream, Collection<T> items) {
  367. return Stream.concat(stream, items.stream());
  368. }
  369. //endregion
  370.  
  371. //region Enumeration
  372. public static <E> Iterable<E> iterable(final Enumeration<E> enumeration) {
  373. Objects.requireNonNull(enumeration);
  374. return () -> new Iterator<E>() {
  375. public boolean hasNext() {
  376. return enumeration.hasMoreElements();
  377. }
  378.  
  379. public E next() {
  380. return enumeration.nextElement();
  381. }
  382.  
  383. public void remove() {
  384. throw new UnsupportedOperationException();
  385. }
  386. };
  387. }
  388. //endregion
  389.  
  390. //region Private
  391. private Cs() {
  392. }
  393. //endregion
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement