PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/at.tuwien.prip.common/src/at/tuwien/prip/common/utils/ListUtils.java

https://bitbucket.org/maxgoebel/tableannotator
Java | 237 lines | 198 code | 29 blank | 10 comment | 29 complexity | 23778354f7dbda58eeda0c61a73af810 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright (c) 2013 Max Göbel.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the GNU Lesser Public License v2.1
  5. * which accompanies this distribution, and is available at
  6. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  7. *
  8. * Contributors:
  9. * Max Göbel - initial API and implementation
  10. ******************************************************************************/
  11. package at.tuwien.prip.common.utils;
  12. import java.io.File;
  13. import java.lang.reflect.Array;
  14. import java.util.ArrayList;
  15. import java.util.Collection;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.LinkedHashSet;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.Set;
  22. import at.tuwien.prip.common.datastructures.Map2;
  23. import at.tuwien.prip.common.datastructures.Pair;
  24. public class ListUtils {
  25. public static <A,B> Pair<ArrayList<ArrayList<A>>, ArrayList<ArrayList<B>>>
  26. unzip(List<List<Pair<A,B>>> listListPair)
  27. {
  28. ArrayList<ArrayList<A>> listListFirst = new ArrayList<ArrayList<A>>(listListPair.size());
  29. ArrayList<ArrayList<B>> listListSecond = new ArrayList<ArrayList<B>>(listListPair.size());
  30. for (List<Pair<A, B>> listPair: listListPair) {
  31. ArrayList<A> listFirst = new ArrayList<A>(listPair.size());
  32. ArrayList<B> listSecond = new ArrayList<B>(listPair.size());
  33. for (Pair<A, B> pair : listPair) {
  34. listFirst.add(pair.getFirst());
  35. listSecond.add(pair.getSecond());
  36. }
  37. listListFirst.add(listFirst);
  38. listListSecond.add(listSecond);
  39. }
  40. return new Pair<ArrayList<ArrayList<A>>, ArrayList<ArrayList<B>>>(
  41. listListFirst, listListSecond);
  42. }
  43. public static void unique (Collection<?> list) {
  44. Collection<Object> copyList = new LinkedList<Object> ();
  45. Iterator<?> it = list.iterator();
  46. while (it.hasNext()) {
  47. Object obj = it.next();
  48. if (!copyList.contains(obj)) {
  49. copyList.add(obj);
  50. } else {
  51. it.remove();
  52. }
  53. }
  54. }
  55. public static int max (List<Integer> list) {
  56. int max = -1000;
  57. for (Integer i : list) {
  58. if (i>max) {
  59. max = i;
  60. }
  61. }
  62. return max;
  63. }
  64. public static Set<String> toSet(String[] strings) {
  65. HashSet<String> lhs = new LinkedHashSet<String>();
  66. for (int i=0; i<strings.length; i++) {
  67. lhs.add(strings[i]);
  68. }
  69. return lhs;
  70. }
  71. public static List<String> toList(String[] strings) {
  72. List<String> l = new LinkedList<String>();
  73. for (int i=0; i<strings.length; i++) {
  74. l.add(strings[i]);
  75. }
  76. return l;
  77. }
  78. public static List<Object> toList(Object[] objs) {
  79. List<Object> l = new LinkedList<Object>();
  80. for (int i=0; i<objs.length; i++) {
  81. l.add(objs[i]);
  82. }
  83. return l;
  84. }
  85. public static List<File> toList(File[] files) {
  86. List<File> l = new LinkedList<File>();
  87. for (int i=0; i<files.length; i++) {
  88. l.add(files[i]);
  89. }
  90. return l;
  91. }
  92. public static int[] range(int from, int to) {
  93. assert from<=to;
  94. int len = to-from+1;
  95. int[] l = new int[len];
  96. for (int k=0; k<len; k++) {
  97. l[k]=k+from;
  98. }
  99. return l;
  100. }
  101. public static List<Integer> rangeList(int from, int to) {
  102. List<Integer> l = new LinkedList<Integer>();
  103. for (int k=from; k<=to; k++) {
  104. l.add(k);
  105. }
  106. return l;
  107. }
  108. public static List<Integer> toList(int[] integers) {
  109. List<Integer> l = new LinkedList<Integer>();
  110. for (int i=0; i<integers.length; i++) {
  111. l.add(integers[i]);
  112. }
  113. return l;
  114. }
  115. public static <C> List<C> toList (C[] objs, Class<C> c) {
  116. List<C> l = new LinkedList<C>();
  117. for (int i=0; i<objs.length; i++) {
  118. l.add(objs[i]);
  119. }
  120. return l;
  121. }
  122. @SuppressWarnings("unchecked")
  123. public static <T> T[] toArray(Collection<T> l, Class<T> c) {
  124. T[] a = (T[]) Array.newInstance(c, l.size());
  125. return l.toArray(a);
  126. }
  127. public static <T> List<T> reverse(List<T> list) {
  128. List<T> result = new LinkedList<T>();
  129. for (int i=list.size()-1; i>=0; i--) {
  130. result.add(list.get(i));
  131. }
  132. return result;
  133. }
  134. public static <T> List<T> union(Collection<T> s1, Collection<T> s2) {
  135. List<T> u = new LinkedList<T>();
  136. if (s1!=null) u.addAll(s1);
  137. if (s2!=null) u.addAll(s2);
  138. return u;
  139. }
  140. public static <T> List<T> union(Collection<T> ... s) {
  141. List<T> u = new LinkedList<T>();
  142. for (Collection<T> collection : s) {
  143. u.addAll(collection);
  144. }
  145. return u;
  146. }
  147. public static <A,B> List<A> projectFirst(Collection<Pair<A,B>> abs) {
  148. List<A> as = new LinkedList<A>();
  149. for (Pair<A, B> ab : abs) {
  150. as.add(ab.getFirst());
  151. }
  152. return as;
  153. }
  154. public static <A,B> List<B> projectSecond(Collection<Pair<A,B>> abs) {
  155. List<B> bs = new LinkedList<B>();
  156. for (Pair<A, B> ab : abs) {
  157. bs.add(ab.getSecond());
  158. }
  159. return bs;
  160. }
  161. public static <A,B> List<A> map2projectFirst(Collection<Map2.KeyPair2<A,B>> abs) {
  162. List<A> as = new LinkedList<A>();
  163. for (Map2.KeyPair2<A, B> ab : abs) {
  164. as.add(ab.getKey1());
  165. }
  166. return as;
  167. }
  168. public static <A,B> List<B> map2projectSecond(Collection<Map2.KeyPair2<A,B>> abs) {
  169. List<B> bs = new LinkedList<B>();
  170. for (Map2.KeyPair2<A, B> ab : abs) {
  171. bs.add(ab.getKey2());
  172. }
  173. return bs;
  174. }
  175. public static <A,B,C> List<B> map2secondFromFirst(Map2<A,B,C> map, A key1) {
  176. List<B> bs = new LinkedList<B>();
  177. for (Map2.KeyPair2<A,B> ab : map.keySet()) {
  178. if (ab.getKey1().equals(key1)) {
  179. bs.add(ab.getKey2());
  180. }
  181. }
  182. return bs;
  183. }
  184. public static <A> List<A> flatten(Collection<List<A>> lla) {
  185. List<A> ret = new LinkedList<A>();
  186. Iterator<List<A>> it = lla.iterator();
  187. while (it.hasNext()) {
  188. List<A> la = (List<A>) it.next();
  189. for (A a : la) {
  190. if (!ret.contains(a)) ret.add(a);
  191. }
  192. }
  193. return ret;
  194. }
  195. public static <A> Set<A> union(Collection<? extends Collection<A>> lla) {
  196. Set<A> ret = new LinkedHashSet<A>();
  197. for (Collection<A> la : lla) {
  198. ret.addAll(la);
  199. }
  200. return ret;
  201. }
  202. public static <T> boolean equalAsSets(Collection<T> c1, Collection<T> c2)
  203. {
  204. HashSet<T> s1 = new HashSet<T>(c1);
  205. HashSet<T> s2 = new HashSet<T>(c2);
  206. return s1.equals(s2);
  207. }
  208. }