Skip to main content
summaryrefslogtreecommitdiffstats
blob: 889ba927a3174dbb696f82bf20c2b5689ce634de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author David Diepenbrock
 */
public class Collections {
   public static Object[] EMPTY_ARRAY = new Object[0];

   public static Collection<String> fromString(String string, String seperator) {
      return Arrays.asList(string.split(seperator));
   }

   /**
    * A flexible alternative for converting a Collection to a String.
    * 
    * @param items The Collection to convert to a String
    * @param prefix The String to place at the beginning of the returned String
    * @param separator The String to place in between elements of the Collection c.
    * @param suffix The String to place at the end of the returned String
    * @return A String which starts with 'start', followed by the elements in the Collection c separated by 'separator',
    * ending with 'end'.
    */
   public static String toString(Collection<?> items, String prefix, String separator, String suffix) {
      StringBuilder strB = new StringBuilder();

      if (prefix != null) {
         strB.append(prefix);
      }

      boolean first = true;
      for (Object item : items) {
         if (first) {
            first = false;
         } else {
            strB.append(separator);
         }
         strB.append(String.valueOf(item));
      }

      if (suffix != null) {
         strB.append(suffix);
      }

      return strB.toString();
   }

   public static String toString(String separator, Object... items) {
      return toString(separator, Arrays.asList(items));
   }

   public static String toString(String separator, Collection<?> c) {
      return toString(c, null, separator, null);
   }

   public static <A> List<Collection<A>> subDivide(List<A> collection, int size) {
      List<Collection<A>> result = new ArrayList<Collection<A>>();
      for (int i = 0; i < collection.size() / size + 1; i++) {
         int maxLength;
         if (i * size + size > collection.size()) {
            maxLength = collection.size();
         } else {
            maxLength = i * size + size;
         }
         List<A> sublist = new ArrayList<A>();
         for (int j = i * size; j < maxLength; j++) {
            sublist.add(collection.get(j));
         }
         result.add(sublist);
      }
      return result;
   }

   public static <A> Collection<A> unique(Collection<A> collection) {
      Set<A> result = new HashSet<A>();
      result.addAll(collection);
      return result;
   }

   /**
    * <p>
    * Meaning:
    * <li>elements outside of B
    * <li>all elements unique to A
    * </p>
    * 
    * @return relative set complement of B in A.
    * <p>
    * <br/>
    * Noted:<br/>
    * A - B
    * </p>
    * <p>
    * <br/>
    * Examples:<br/>
    * { 1, 2, 3 } - { 1, 4, 3 } = { 2 } <br/>
    * <br/>
    * { 1, 4, 3 } - { 1, 2, 3 } = { 4 }
    * </p>
    */
   public static <T> List<T> setComplement(Collection<T> set_A, Collection<T> set_B) {
      ArrayList<T> complement = new ArrayList<T>(set_A.size());
      for (T obj : set_A) {
         if (!set_B.contains(obj)) {
            complement.add(obj);
         }
      }
      return complement;
   }

   /**
    * @return The intersection of two sets A and B is the set of elements common to A and B
    */
   public static <T> ArrayList<T> setIntersection(Collection<T> listA, Collection<T> listB) {
      ArrayList<T> intersection = new ArrayList<T>(listA.size());

      for (T obj : listA) {
         if (listB.contains(obj)) {
            intersection.add(obj);
         }
      }
      return intersection;
   }

   /**
    * @return union of unique elements from the given lists
    */
   public static <T> Set<T> setUnion(Collection<T>... lists) {
      Set<T> union = new HashSet<T>(lists[0].size() * 2);

      for (int x = 0; x < lists.length; x++) {
         union.addAll(lists[x]);
      }
      return union;
   }

   /**
    * Return true if same objects exist in listA and listB
    */
   public static <T> boolean isEqual(Collection<T> listA, Collection<T> listB) {
      if (listA.size() != listB.size()) {
         return false;
      }
      return listA.size() == setIntersection(listA, listB).size();
   }

   public static <T> Set<T> toSet(Collection<T> collection) {
      Set<T> set = null;
      if (collection instanceof Set) {
         set = (Set<T>) collection;
      } else {
         set = new LinkedHashSet<T>();
         set.addAll(collection);
      }
      return set;
   }

   /**
    * Convert an aggregate list of objects into a List
    */
   public static <T> List<T> getAggregate(T... objects) {
      List<T> objs = new ArrayList<T>();
      if (objects != null) {
         for (T obj : objects) {
            objs.add(obj);
         }
      }
      return objs;
   }

   public static List<Object> getAggregateTree(List<Object> items, int maxPerList) {
      if (items == null) {
         throw new IllegalArgumentException("items can not be null");
      }
      if (maxPerList < 2) {
         throw new IllegalArgumentException("maxPerList can not be less than 2");
      }

      if (items.size() > maxPerList) {
         return recursiveAggregateTree(items, maxPerList);
      } else {
         return new ArrayList<Object>(items);
      }
   }

   private static ArrayList<Object> recursiveAggregateTree(List<Object> items, int maxPerList) {
      if (items.size() > maxPerList) {
         ArrayList<Object> aggregateList = new ArrayList<Object>(maxPerList);
         ArrayList<Object> childList = null;

         for (Object item : items) {
            if (childList == null || childList.size() == maxPerList) {
               childList = new ArrayList<Object>(maxPerList);
               aggregateList.add(childList);
            }
            childList.add(item);
         }
         childList.trimToSize();

         aggregateList = recursiveAggregateTree(aggregateList, maxPerList);

         aggregateList.trimToSize();

         return aggregateList;
      } else {
         // This is a safe blind cast since only subsequent calls of this method will end up here
         // and this method always uses ArrayList<Object>
         return (ArrayList<Object>) items;
      }
   }

   public static enum CastOption {
      MATCHING,
      ALL
   };

   /**
    * Cast objects to clazz
    * 
    * @param castOption if ALL, cast all and throw exception if cast fails; if MATCHING, only cast those of type clazz
    */
   @SuppressWarnings("unchecked")
   private static <A extends Object> List<A> cast(Class<A> clazz, Collection<? extends Object> objects, CastOption castOption) {
      List<A> results = new ArrayList<A>(objects.size());
      for (Object object : objects) {
         if (castOption == CastOption.ALL || castOption == CastOption.MATCHING && clazz.isAssignableFrom(object.getClass())) {
            results.add((A) object);
         }
      }
      return results;
   }

   /**
    * Cast objects to clazz
    */
   @SuppressWarnings("unchecked")
   public static <A> List<A> castAll(Collection<?> objects) {
      List<A> results = new ArrayList<A>(objects.size());
      for (Object object : objects) {
         results.add((A) object);
      }
      return results;
   }

   /**
    * Unchecked cast objects to clazz; CastClassException will occur when object sent in does not match clazz<br>
    * <br>
    * Use when all objects are expected to be of type class and exception is desired if not
    */
   public static <A extends Object> List<A> castAll(Class<A> clazz, Collection<? extends Object> objects) {
      return cast(clazz, objects, CastOption.ALL);
   }

   /**
    * Cast objects matching class, ignore rest; no ClassCastException will occur<br>
    * <br>
    * Use when objects may contain classes that are not desired
    */
   public static <A extends Object> List<A> castMatching(Class<A> clazz, Collection<? extends Object> objects) {
      return cast(clazz, objects, CastOption.MATCHING);
   }

   public static <A extends Object> boolean moveItem(List<A> currentOrder, A itemToAdd, A targetItem, boolean insertAfter) {
      int newIndex = currentOrder.indexOf(targetItem);
      if (newIndex < 0 || newIndex > currentOrder.size() - 1) {
         return false;
      }
      int oldIndex = currentOrder.indexOf(itemToAdd);
      if (oldIndex < 0 || oldIndex > currentOrder.size() - 1) {
         return false;
      }

      currentOrder.remove(itemToAdd);
      if (insertAfter) {
         newIndex = newIndex > oldIndex ? newIndex : newIndex + 1;
      } else {
         newIndex = newIndex > oldIndex ? newIndex - 1 : newIndex;
      }
      if (newIndex > currentOrder.size()) {
         currentOrder.add(itemToAdd);
      } else {
         currentOrder.add(newIndex, itemToAdd);
      }
      return true;
   }
}

Back to the top