/** * Executes example. * * @param args Command line arguments, none required. */ public static void main(String[] args) { // Typedefs: // --------- // G -> GridFactory // CI1 -> GridInClosure // CO -> GridOutClosure // CA -> GridAbsClosure // F -> GridFunc // Data initialisation. Random rand = new Random(); final int size = 20; Collection<Integer> nums = new ArrayList<Integer>(size); // Generate list of random integers. for (int i = 0; i < size; i++) { nums.add(rand.nextInt(size)); } // Print generated list. X.println("Generated list:"); F.forEach(nums, F.<Integer>print("", " ")); // Retain all elements which value low than half generated list size. Collection<Integer> res = F.retain( nums, true, new P1<Integer>() { @Override public boolean apply(Integer i) { return i < size / 2; } }); // Print result. X.println("\nResult list:"); F.forEach(res, F.<Integer>print("", " ")); // Retain first half of result list. F.retain(res, false, res.size() / 2); // Print result. X.println("\nResult list:"); F.forEach(res, F.<Integer>print("", " ")); }
/** {@inheritDoc} */ @Override public <T extends GridEvent> Collection<T> localEvents(GridPredicate<T> p) { A.notNull(p, "p"); cleanupQueue(); return F.retain((Collection<T>) evts, true, p); }
/** @throws Exception Thrown if test failed. */ public void testA() throws Exception { Collection<Integer> set = new GridConcurrentWeakHashSet<>(); Integer i = 1; assert set.add(i); assert !set.add(i); assert set.contains(i); assert set.size() == 1; Collection<Integer> c = F.asList(2, 3, 4, 5); assert set.addAll(c); assert !set.addAll(c); assert set.containsAll(c); assert set.size() == 1 + c.size(); assert set.remove(i); assert !set.remove(i); assert !set.contains(i); assert set.size() == c.size(); assert set.removeAll(c); assert !set.removeAll(c); assert !set.containsAll(c); assert set.isEmpty(); Collection<Integer> c1 = Arrays.asList(1, 3, 5, 7, 9); int cnt = 0; for (Iterator<Integer> iter = set.iterator(); iter.hasNext(); cnt++) c1.contains(iter.next()); assert set.size() == cnt; assert set.size() == set.toArray().length; assert set.addAll(c1); assert set.retainAll(c); assert !set.retainAll(c); Collection<Integer> c2 = F.retain(c1, true, c); assert set.containsAll(c2); assert !set.containsAll(c1); assert !set.containsAll(c); assert set.size() == c2.size(); set.clear(); assert set.isEmpty(); try { set.iterator().next(); assert false; } catch (NoSuchElementException ignored) { assert true; } try { set.add(null); assert false; } catch (NullPointerException ignored) { assert true; } }