org.apache.commons.collections4.Predicate Java Examples

The following examples show how to use org.apache.commons.collections4.Predicate. 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 File: WiFiDataTest.java    From WiFiAnalyzer with 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 #2
Source File: EqualPredicateTest.java    From feilong-core with 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 File: children.java    From commerce-cif-connector with 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 #4
Source File: MainLogicImpl.java    From icure-backend with 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 #5
Source File: ResourceReleaseRule.java    From sakai with 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 #6
Source File: DataViewMessageBodyWriter.java    From ameba with 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 #7
Source File: WiFiDataTest.java    From WiFiAnalyzer with 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 #8
Source File: CCFilter.java    From ant-ivy with 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 #9
Source File: FilterUtils.java    From icure-backend with 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 #10
Source File: TribeUtils.java    From dsworkbench with 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 #11
Source File: FindWithPredicateTest.java    From feilong-core with 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 #12
Source File: DEPFilterTableModel.java    From dsworkbench with 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 #13
Source File: SumArrayPredicateTest.java    From feilong-core with 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 #14
Source File: ResourceReleaseRule.java    From sakai with 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 #15
Source File: CartesianProduct.java    From tcases with 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 #16
Source File: ChannelRatingAdapterTest.java    From WiFiAnalyzer with GNU General Public License v3.0 6 votes vote down vote up
@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 #17
Source File: JobState.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * To get the task as an array list and filtered by a given tag.
 * @param tag the used to filter the tasks.
 * @return the set of filtered task states.
 */
public List<TaskState> getTasksByTag(final String tag) {
    List<TaskState> tasks = this.getTasks();
    return (List<TaskState>) CollectionUtils.select(tasks, (Predicate) object -> {
        String taskTag = ((TaskState) object).getTag();
        return (taskTag != null) && (taskTag.equals(tag));
    });
}
 
Example #18
Source File: TAPTargetTableModel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
public void decreaseRowCount(final Village pVillage) {
    Object result = CollectionUtils.find(elements, new Predicate() {

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

    if (result != null) {
        TAPAttackTargetElement resultElem = (TAPAttackTargetElement) result;
        resultElem.removeAttack();
        fireTableDataChanged();
    }
}
 
Example #19
Source File: WiFiDataTest.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testIsConfiguredNetwork() {
    // setup
    Predicate<WiFiDetail> predicate = new WiFiBandPredicate(WiFiBand.GHZ2);
    withVendorNames();
    // execute
    List<WiFiDetail> actual = fixture.getWiFiDetails(predicate, SortBy.STRENGTH, GroupBy.SSID);
    // validate
    assertEquals(4, actual.size());

    assertEquals(SSID_1, actual.get(2).getSSID());
    assertEquals(SSID_3, actual.get(3).getSSID());
    verifyVendorNames();
}
 
Example #20
Source File: StandardAttackManager.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
public boolean containsElementByIcon(final int pIcon) {
    Object result = CollectionUtils.find(getAllElements(), new Predicate() {

        @Override
        public boolean evaluate(Object o) {
            return ((StandardAttack) o).getIcon() == pIcon;
        }
    });

    return result != null;
}
 
Example #21
Source File: SOSManager.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
public SOSRequest getRequest(final Tribe pTribe) {
  if (pTribe == null) {
    return null;
  }

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

    @Override
    public boolean evaluate(Object o) {
      return ((SOSRequest) o).getDefender().equals(pTribe);
    }
  });

  return (SOSRequest) result;
}
 
Example #22
Source File: TimeGraphView.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void update(@NonNull WiFiData wiFiData) {
    Settings settings = MainContext.INSTANCE.getSettings();
    Predicate<WiFiDetail> predicate = FilterPredicate.makeOtherPredicate(settings);
    List<WiFiDetail> wiFiDetails = wiFiData.getWiFiDetails(predicate, settings.getSortBy());
    Set<WiFiDetail> newSeries = dataManager.addSeriesData(graphViewWrapper, wiFiDetails, settings.getGraphMaximumY());
    graphViewWrapper.removeSeries(newSeries);
    graphViewWrapper.updateLegend(settings.getTimeGraphLegend());
    graphViewWrapper.setVisibility(isSelected() ? View.VISIBLE : View.GONE);
}
 
Example #23
Source File: SumPredicateTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test sum 3111.
 */
@Test
public void testSum3111(){
    User zhangfei = new User(100L);
    zhangfei.setName("张飞");
    zhangfei.setAge(null);

    List<User> list = toList(zhangfei);

    Predicate<User> notPredicate = PredicateUtils.notPredicate(BeanPredicateUtil.equalPredicate("name", "张飞"));
    Map<String, BigDecimal> map = AggregateUtil.sum(list, toArray("id", "age"), notPredicate);

    assertEquals(true, isNullOrEmpty(map));
}
 
Example #24
Source File: WiFiDataTest.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGetWiFiDetailsWithSSID() {
    // setup
    Predicate<WiFiDetail> predicate = new WiFiBandPredicate(WiFiBand.GHZ2);
    withVendorNames();
    // execute
    List<WiFiDetail> actual = fixture.getWiFiDetails(predicate, SortBy.STRENGTH, GroupBy.SSID);
    // validate
    assertEquals(4, actual.size());
    assertEquals(SSID_2, actual.get(0).getSSID());
    assertEquals(SSID_4, actual.get(1).getSSID());
    assertEquals(SSID_1, actual.get(2).getSSID());
    assertEquals(SSID_3, actual.get(3).getSSID());
    verifyVendorNames();
}
 
Example #25
Source File: DEPSourceTableModel.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
public void addRow(final Village pVillage, int pSupports) {
    Object existing = CollectionUtils.find(elements, new Predicate() {

        @Override
        public boolean evaluate(Object o) {
            return ((SupportSourceElement) o).getVillage().equals(pVillage);
        }
    });
    if (existing == null) {
        elements.add(new SupportSourceElement(pVillage, pSupports));
    }
}
 
Example #26
Source File: GroupWithTransformerAndPredicateTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test group not predicate.
 */
@Test
public void testGroupNotPredicate(){
    List<User> list = toList(new User("张飞", 10), new User("刘备", 10));
    Predicate<User> comparatorPredicate = BeanPredicateUtil.comparatorPredicate("age", 20, Criterion.EQUAL);
    assertEquals(emptyMap(), CollectionsUtil.group(list, comparatorPredicate, TransformerUtils.<User, Integer> constantTransformer(5)));
}
 
Example #27
Source File: MailingTrackingPointsDataSet.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #28
Source File: SumArrayPredicateTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test sum blank property name.
 */
@Test(expected = IllegalArgumentException.class)
public void testSumBlankPropertyName(){
    assertEquals(null, AggregateUtil.sum(ConvertUtil.<User> toList(), " ", new Predicate<User>(){

        @Override
        public boolean evaluate(User user){
            return user.getId() > 10L;
        }
    }));
}
 
Example #29
Source File: EnumUtilsTest.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFindUsingPredicate() {
    // setup
    final TestObject expected = TestObject.VALUE3;
    Predicate<TestObject> predicate = PredicateUtils.equalPredicate(expected);
    // execute
    TestObject actual = EnumUtils.find(TestObject.class, predicate, TestObject.VALUE2);
    // validate
    assertEquals(expected, actual);
}
 
Example #30
Source File: FilterPredicateTest.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testGetPredicateWithAllValuesIsTruePredicate() {
    // setup
    when(settings.getSSIDs()).thenReturn(Collections.emptySet());
    when(settings.getWiFiBands()).thenReturn(EnumUtils.values(WiFiBand.class));
    when(settings.getStrengths()).thenReturn(EnumUtils.values(Strength.class));
    when(settings.getSecurities()).thenReturn(EnumUtils.values(Security.class));

    fixture = FilterPredicate.makeAccessPointsPredicate(settings);
    // execute
    Predicate<WiFiDetail> actual = ((FilterPredicate) fixture).getPredicate();
    // validate
    assertTrue(actual instanceof TruePredicate);
}