Java Code Examples for org.apache.commons.collections4.Predicate

The following examples show how to use org.apache.commons.collections4.Predicate. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: commerce-cif-connector   Source File: children.java    License: Apache License 2.0 6 votes vote down vote up
private static Transformer createTransformer(final String itemRT, final Predicate predicate) {
    return new Transformer() {
        public Object transform(Object o) {
            Resource r = ((Resource) o);

            return new PredicatedResourceWrapper(r, predicate) {
                @Override
                public String getResourceType() {
                    if (itemRT == null) {
                        return super.getResourceType();
                    }
                    return itemRT;
                }
            };
        }
    };
}
 
Example 2
Source Project: feilong-core   Source File: EqualPredicateTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test find2.
 */
@Test
@SuppressWarnings("static-method")
public void testFind2(){
    User guanyu30 = new User("关羽", 30);
    List<User> list = toList(//
                    new User("张飞", 23),
                    new User("关羽", 24),
                    new User("刘备", 25),
                    guanyu30);

    Predicate<User> predicate = PredicateUtils
                    .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30));

    assertEquals(guanyu30, CollectionsUtil.find(list, predicate));
}
 
Example 3
Source Project: icure-backend   Source File: MainLogicImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public <E> int getEntitiesCount(Class<E> entityClass, Predicate<E> predicate) {
	if (log.isTraceEnabled()) {
		log.trace("Counting " + getEntityClassName(entityClass, null) + " using predicate : " + predicate);
	}

	EntityPersister<E, ?> entityPersister = getEntityPersister(entityClass);
	List<E> entities = entityPersister.getAllEntities();
	CollectionUtils.filter(entities, predicate);
	int entitiesCount = entities.size();

	if (log.isTraceEnabled()) {
		log.trace("Counted " + entitiesCount + " " + getEntityClassName(entityClass, entitiesCount > 1));
	}
	return entitiesCount;
}
 
Example 4
Source Project: WiFiAnalyzer   Source File: WiFiDataTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Test
    public void testGetWiFiDetailsWithVendorName() {
        // setup
        Predicate<WiFiDetail> predicate = new WiFiBandPredicate(WiFiBand.GHZ2);
        withVendorNames();
        // execute
        List<WiFiDetail> actual = fixture.getWiFiDetails(predicate, SortBy.STRENGTH, GroupBy.SSID);
        // validate
        assertEquals(VENDOR_NAME + BSSID_2, actual.get(0).getWiFiAdditional().getVendorName());
        assertEquals(VENDOR_NAME + BSSID_4, actual.get(1).getWiFiAdditional().getVendorName());
        assertEquals(VENDOR_NAME + BSSID_1, actual.get(2).getWiFiAdditional().getVendorName());
        assertEquals(VENDOR_NAME + BSSID_3, actual.get(3).getWiFiAdditional().getVendorName());

//        verify(vendorService, times(7)).findVendorName(anyString());
        verifyVendorNames();
    }
 
Example 5
Source Project: icure-backend   Source File: FilterUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Predicate<T> getPredicateAny(List<Filter> filters) {
	Predicate<T> finalPredicate = null;

	if (filters != null) {
		List<Predicate<T>> predicates = new ArrayList<>();
		for (Filter filter : filters) {
			Predicate<T> predicate = getPredicate(filter);
			if (predicate != null) {
				predicates.add(predicate);
			}
		}
		if (!predicates.isEmpty()) {
			finalPredicate = new AnyPredicate<>(predicates.toArray(new Predicate[predicates.size()]));
		}
	}

	return finalPredicate;
}
 
Example 6
Source Project: feilong-core   Source File: FindWithPredicateTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test find2.
 */
@Test
@SuppressWarnings("static-method")
public void testFind2(){
    User guanyu30 = new User("关羽", 30);
    List<User> list = toList(//
                    new User("张飞", 23),
                    new User("关羽", 24),
                    new User("刘备", 25),
                    guanyu30);

    Map<String, ?> map = toMap("name", "关羽", "age", 30);

    Predicate<User> equalPredicate = BeanPredicateUtil.equalPredicate(map);
    assertEquals(guanyu30, CollectionsUtil.find(list, equalPredicate));
}
 
Example 7
Source Project: tcases   Source File: CartesianProduct.java    License: MIT License 6 votes vote down vote up
/**
 * Creates a new iterator for the Cartesian product of the given sets, ignoring any resulting
 * list that does not satisfy the given {@link Predicate}.
 */
private CartesianProduct( List<T> addedTo, List<? extends Set<T>> sets, Predicate<List<T>> filter)
  {
  addedTo_ = addedTo;
  filter_ = filter;
  
  int setCount = sets.size();

  Set<T> firstSet =
    setCount == 0
    ? Collections.<T>emptySet()
    : sets.get(0);

  firstSetIterator_ = firstSet.iterator();

  otherSets_ =
    setCount < 2
    ? Collections.<Set<T>>emptyList()
    : sets.subList( 1, setCount);
  }
 
Example 8
@Test
public void testUpdate() {
    // setup
    String expected = mainActivity.getResources().getText(R.string.channel_rating_best_none).toString();
    WiFiData wiFiData = new WiFiData(Collections.emptyList(), WiFiConnection.EMPTY);
    Predicate<WiFiDetail> predicate = new WiFiBandPredicate(WiFiBand.GHZ5);
    List<WiFiDetail> wiFiDetails = wiFiData.getWiFiDetails(predicate, SortBy.STRENGTH);
    when(settings.getWiFiBand()).thenReturn(WiFiBand.GHZ5);
    when(settings.getCountryCode()).thenReturn(Locale.US.getCountry());
    // execute
    fixture.update(wiFiData);
    // validate
    assertEquals(expected, bestChannels.getText());
    verify(channelRating).setWiFiDetails(wiFiDetails);
    verify(settings).getWiFiBand();
    verify(settings).getCountryCode();
}
 
Example 9
Source Project: sakai   Source File: ResourceReleaseRule.java    License: Educational Community License v2.0 6 votes vote down vote up
public void toXml(Element el) {
	el.setAttribute("resourceId", this.resourceId);
	
	if(this.conj == Rule.Conjunction.OR) {
		el.setAttribute("conjunction", "OR");
	} else if(this.conj == Rule.Conjunction.AND) {
		el.setAttribute("conjunction", "AND");
	}
	
	Element predicates = el.getOwnerDocument().createElement("predicates");
	el.appendChild(predicates);
	for (Predicate p : this.predicates) {
		Element predicateElement = el.getOwnerDocument().createElement("predicate");
		predicateElement.setAttribute("class", p.getClass().getName());
		predicateElement.setAttribute("receiver", ((BooleanExpression)p).getReceiver());
		predicateElement.setAttribute("method", ((BooleanExpression)p).getMethod());
		predicateElement.setAttribute("operator", ((BooleanExpression)p).getOperator());
		Object argument = ((BooleanExpression)p).getArgument();
		if (argument == null) {
			argument = "";
		}
		predicateElement.setAttribute("argument", argument.toString());
		predicates.appendChild(predicateElement);
	}

}
 
Example 10
Source Project: feilong-core   Source File: SumArrayPredicateTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test sum no match predicate.
 */
@Test
public void testSumNoMatchPredicate(){
    List<User> list = toList(//
                    new User(2L),
                    new User(50L),
                    new User(50L));

    assertEquals(null, AggregateUtil.sum(list, "id", new Predicate<User>(){

        @Override
        public boolean evaluate(User user){
            return user.getId() > 100L;
        }
    }));
}
 
Example 11
Source Project: dsworkbench   Source File: DEPFilterTableModel.java    License: Apache License 2.0 6 votes vote down vote up
public void addRow(final SupportSourceElement pElement, boolean pCheck) {

        Object result = CollectionUtils.find(elements, new Predicate() {

            @Override
            public boolean evaluate(Object o) {
                return ((SupportSourceElement) o).getVillage().equals(pElement.getVillage());
            }
        });

        if (result == null) {
            elements.add(pElement);
        }
        if (pCheck) {
            fireTableDataChanged();
        }
    }
 
Example 12
Source Project: dsworkbench   Source File: TribeUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static List<Tribe> filterTribes(List<Tribe> input, final String pFilter, Comparator<Tribe> pComparator) {
    if (pFilter == null) {
        return new ArrayList<>();
    }
    final String filter = pFilter.toLowerCase();

    if (filter.length() > 0) {
        CollectionUtils.filter(input, new Predicate() {
            @Override
            public boolean evaluate(Object o) {
                return ((Tribe) o).getName().toLowerCase().contains(filter);
            }
        });
    }

    if (pComparator != null) {
        Collections.sort(input, pComparator);
    }
    return input;
}
 
Example 13
Source Project: ant-ivy   Source File: CCFilter.java    License: Apache License 2.0 6 votes vote down vote up
public String[] filter(String[] values, final String prefix) {
    if (values == null) {
        return null;
    }
    if (prefix == null) {
        return values;
    }

    List<String> result = new ArrayList<>(Arrays.asList(values));
    CollectionUtils.filter(result, new Predicate<String>() {
        public boolean evaluate(String string) {
            return string != null && string.startsWith(prefix);
        }
    });
    return result.toArray(new String[result.size()]);
}
 
Example 14
Source Project: WiFiAnalyzer   Source File: WiFiDataTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetWiFiDetailsWithChildren() {
    // setup
    Predicate<WiFiDetail> predicate = new WiFiBandPredicate(WiFiBand.GHZ2);
    withVendorNames();
    // execute
    List<WiFiDetail> actual = fixture.getWiFiDetails(predicate, SortBy.STRENGTH, GroupBy.SSID);
    // validate
    WiFiDetail wiFiDetail = actual.get(0);
    List<WiFiDetail> children = wiFiDetail.getChildren();
    assertEquals(3, children.size());
    assertEquals(BSSID_2 + "_2", children.get(0).getBSSID());
    assertEquals(BSSID_2 + "_3", children.get(1).getBSSID());
    assertEquals(BSSID_2 + "_1", children.get(2).getBSSID());
    verifyVendorNames();
}
 
Example 15
Source Project: sakai   Source File: ResourceReleaseRule.java    License: Educational Community License v2.0 6 votes vote down vote up
public void toXml(Element el) {
	el.setAttribute("resourceId", this.resourceId);
	
	if(this.conj == Rule.Conjunction.OR) {
		el.setAttribute("conjunction", "OR");
	} else if(this.conj == Rule.Conjunction.AND) {
		el.setAttribute("conjunction", "AND");
	}
	
	Element predicates = el.getOwnerDocument().createElement("predicates");
	el.appendChild(predicates);
	for (Predicate p : this.predicates) {
		Element predicateElement = el.getOwnerDocument().createElement("predicate");
		predicateElement.setAttribute("class", p.getClass().getName());
		predicateElement.setAttribute("receiver", ((BooleanExpression)p).getReceiver());
		predicateElement.setAttribute("method", ((BooleanExpression)p).getMethod());
		predicateElement.setAttribute("operator", ((BooleanExpression)p).getOperator());
		Object argument = ((BooleanExpression)p).getArgument();
		if (argument == null) {
			argument = "";
		}
		predicateElement.setAttribute("argument", argument.toString());
		predicates.appendChild(predicateElement);
	}

}
 
Example 16
Source Project: ameba   Source File: DataViewMessageBodyWriter.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
                           final MediaType mediaType) {
    String[] p;
    return !dataViewDisabled
            && -1 != ListUtils.indexOf(requestProvider.get().getAcceptableMediaTypes(),
            this::isSupportMediaType)
            && ((p = TemplateHelper.getProduces(annotations)) == null
            || -1 != ArrayUtils.indexOf(p,
            (Predicate<String>) stringType -> {
                if (stringType.equals(MediaType.WILDCARD)) return true;

                MediaType mediaType1 = MediaType.valueOf(stringType);
                return isSupportMediaType(mediaType1);
            }));
}
 
Example 17
private Collection<TrackingPointDef> extractTrackingPointsDefByType(Collection<TrackingPointDef> inputCollection, final int trackingPointType) {
	Predicate<TrackingPointDef> predicate = new Predicate<TrackingPointDef>() {
		@Override
		public boolean evaluate(TrackingPointDef trackingPointDef) {
			return trackingPointDef.getType() == trackingPointType ? true : false;
		}
	};
	return CollectionUtils.select(inputCollection, predicate);
}
 
Example 18
private Collection<TrackingPointDef> extractTrackingPointsDefByType(Collection<TrackingPointDef> inputCollection, final int trackingPointType) {
	Predicate<Object> predicate = new Predicate<Object>() {
		@Override
		public boolean evaluate(Object arg0) {
			TrackingPointDef trackingPointDef = (TrackingPointDef) arg0;
			return trackingPointDef.getType() == trackingPointType ? true : false;
		}
	};
	return CollectionUtils.select(inputCollection, predicate);
}
 
Example 19
Source Project: feilong-core   Source File: EqualIgnoreCasePredicateTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test equal ignore case predicate.
 */
@Test
@SuppressWarnings("static-method")
public void testEqualIgnoreCasePredicate(){
    User user = new User("guanYu", 30);
    Predicate<User> equalPredicate = BeanPredicateUtil.equalIgnoreCasePredicate("name", "GUANYU");
    assertEquals(true, equalPredicate.evaluate(user));
}
 
Example 20
Source Project: commerce-cif-connector   Source File: children.java    License: Apache License 2.0 5 votes vote down vote up
private static Predicate createPredicate(String filter) {
    if (FILTER_CATEGORY.equals(filter)) {
        return new CategoryPredicate();
    } else {
        return new FolderOrCategoryPredicate();
    }
}
 
Example 21
Source Project: codenjoy   Source File: TestUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void assertContainsPlot(final int x, final int y, final Elements color, List<Plot> plots) {
    Object foundPlot = CollectionUtils.find(plots, (Predicate) object -> {
        Plot plot = (Plot) object;
        return plot.getColor() == color && plot.getX() == x && plot.getY() == y;
    });
    assertNotNull("Plot with coordinates (" + x + "," + y + ") color: " + color + " not found", foundPlot);
}
 
Example 22
Source Project: feilong-core   Source File: ContainsPredicateListTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test equal predicate.
 */
@Test
public void testContainsPredicate(){
    User user = new User(2L);
    Predicate<User> containsPredicate = BeanPredicateUtil.containsPredicate("id", toList(2L));
    assertEquals(true, containsPredicate.evaluate(user));
}
 
Example 23
@Override
public boolean evaluate(T o) {
	for (Predicate<T> subPredicate : subPredicates) {
		if (subPredicate.evaluate(o)) {
			return true;
		}
	}
	return false;
}
 
Example 24
Source Project: feilong-core   Source File: ContainsPredicateListTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testContainsPredicate12(){
    User guanyu30 = new User("关羽", 30);

    Predicate<User> predicate = PredicateUtils.andPredicate(
                    BeanPredicateUtil.containsPredicate("name", toList("关羽", "刘备")),
                    BeanPredicateUtil.containsPredicate("age", toList(30)));

    assertEquals(true, predicate.evaluate(guanyu30));
}
 
Example 25
Source Project: icure-backend   Source File: FilterUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Predicate<T> getPredicateAllExist(List<Filter> filters) {
	List<Predicate<T>> convertedSubpredicates = (List<Predicate<T>>) CollectionUtils.collect(filters, new Transformer<Filter, Predicate<T>>() {
		@Override
		public Predicate<T> transform(Filter filter) {
			return getPredicate(filter);
		}
	});
	return new AllExistsPredicate<>(convertedSubpredicates.toArray(new Predicate[]{}));
}
 
Example 26
Source Project: javaccPlugin   Source File: JjdocProgramInvoker.java    License: MIT License 5 votes vote down vote up
private Predicate<String> outputTextPredicate() {
    return new Predicate<String>() {

        @Override
        public boolean evaluate(String argument) {
            return "-text=true".equals(argument.toLowerCase());
        }
    };
}
 
Example 27
Source Project: feilong-core   Source File: SumArrayPredicateTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test sum null collection.
 */
@Test
public void testSumNullCollection(){
    assertEquals(null, AggregateUtil.sum(null, "id", new Predicate<User>(){

        @Override
        public boolean evaluate(User user){
            return user.getId() > 10L;
        }
    }));
}
 
Example 28
/**
 * Test group null property name.
 */
//*****
@Test(expected = NullPointerException.class)
public void testGroupNullPropertyName(){
    List<User> list = toList(new User("张飞", 10), new User("刘备", 10));
    Predicate<User> comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 20, Criterion.LESS);
    CollectionsUtil.group(list, null, comparatorPredicate);
}
 
Example 29
private void updateButtons(@NonNull WiFiData wiFiData, Collection<Pair<WiFiChannel, WiFiChannel>> visible) {
    if (visible.size() > 1) {
        MainContext mainContext = MainContext.INSTANCE;
        Configuration configuration = mainContext.getConfiguration();
        Settings settings = mainContext.getSettings();
        Predicate<WiFiDetail> predicate = FilterPredicate.makeOtherPredicate(settings);
        Pair<WiFiChannel, WiFiChannel> selectedWiFiChannelPair = configuration.getWiFiChannelPair();
        List<WiFiDetail> wiFiDetails = wiFiData.getWiFiDetails(predicate, settings.getSortBy());
        IterableUtils.forEach(ids.keySet(), new ButtonClosure(visible, selectedWiFiChannelPair, wiFiDetails));
    }
}
 
Example 30
Source Project: WiFiAnalyzer   Source File: EnumUtilsTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testPredicateExpectsTruePredicateWithAllValues() {
    // setup
    Set<TestObject> inputs = EnumUtils.values(TestObject.class);
    // execute
    Predicate<TestObject> actual = EnumUtils.predicate(TestObject.class, inputs, new TestObjectTransformer());
    // validate
    assertTrue(actual instanceof TruePredicate);
}