/** * Consulta transições com o consumo de um símbolo específico, em vazio ou * com chamadas de submáquina a partir do estado de origem informado. * @param state Estado de origem. * @param symbol Símbolo a ser consumido. * @return Transições com o consumo de um símbolo específico, em vazio ou * com chamadas de submáquina a partir do estado de origem informado. */ public List<Transition> withSymbolEpsilonOrSubmachineFromSourceState( State state, Symbol symbol) { Predicate[] chain = { new EpsilonPredicate(), new SubmachineCallPredicate(), new SymbolPredicate(symbol) }; Collection<Transition> result = CollectionUtils.select( CollectionUtils.select( transitions, new SourceStatePredicate(state)), PredicateUtils.anyPredicate(chain) ); return new ArrayList<>(result); }
/** * 获得 <code>klass</code> 排除某些 <code>excludeFieldNames</code> 之后的字段list. * * <h3>说明:</h3> * <blockquote> * <ol> * <li>是所有字段的值(<b>不是属性</b>)</li> * <li>自动过滤私有并且静态的字段,比如 LOGGER serialVersionUID</li> * </ol> * </blockquote> * * @param klass * the klass * @param excludeFieldNames * 需要排除的field names,如果传递过来是null或者empty 那么不会判断 * @return 如果 <code>obj</code> 是null,抛出 {@link NullPointerException}<br> * 如果 <code>excludeFieldNames</code> 是null或者empty,解析所有的field<br> * 如果 {@link FieldUtils#getAllFieldsList(Class)} 是null或者empty,返回 {@link Collections#emptyList()}<br> * 如果 <code>obj</code>没有字段或者字段都被参数 <code>excludeFieldNames</code> 排除掉了,返回 {@link Collections#emptyMap()}<br> * @see FieldUtils#getAllFieldsList(Class) * @since 1.7.1 */ public static List<Field> getAllFieldList(final Class<?> klass,String...excludeFieldNames){ Validate.notNull(klass, "klass can't be null!"); //获得给定类的所有声明字段 {@link Field},包括所有的parents,包括 public/protect/private/inherited... List<Field> fieldList = FieldUtils.getAllFieldsList(klass); if (isNullOrEmpty(fieldList)){ return emptyList(); } //--------------------------------------------------------------- Predicate<Field> excludeFieldPredicate = BeanPredicateUtil.containsPredicate("name", excludeFieldNames); Predicate<Field> staticPredicate = new Predicate<Field>(){ @Override public boolean evaluate(Field field){ int modifiers = field.getModifiers(); // 私有并且静态 一般是log 或者 serialVersionUID boolean isStatic = Modifier.isStatic(modifiers); String pattern = "[{}.{}],modifiers:[{}]{}"; LOGGER.trace(pattern, klass.getSimpleName(), field.getName(), modifiers, isStatic ? " [isStatic]" : EMPTY); return isStatic; } }; return selectRejected(fieldList, PredicateUtils.orPredicate(excludeFieldPredicate, staticPredicate)); }
/** * Test ignore case find 2. */ @Test public void testIgnoreCaseFind2(){ User guanyu30 = new User("guanYu", 30); List<User> list = toList(// new User("张飞", 23), new User("guanyu1", 24), new User("刘备", 25), guanyu30); Predicate<User> predicate = PredicateUtils.andPredicate( BeanPredicateUtil.equalIgnoreCasePredicate("name", "guanyu"), BeanPredicateUtil.equalPredicate("age", 30)); assertEquals(guanyu30, CollectionsUtil.find(list, predicate)); }
/** * Test sum3. */ @Test public void testSum3(){ User liubei = new User(10L); liubei.setName("刘备"); liubei.setAge(50); User guanyu = new User(20L); liubei.setName("关羽"); guanyu.setAge(50); User zhangfei = new User(100L); zhangfei.setName("张飞"); zhangfei.setAge(null); User zhaoyun = new User((Long) null); zhaoyun.setName("赵云"); zhaoyun.setAge(100); List<User> list = toList(liubei, guanyu, zhangfei, zhaoyun); Predicate<User> notPredicate = PredicateUtils.notPredicate(BeanPredicateUtil.equalPredicate("name", "张飞")); Map<String, BigDecimal> map = AggregateUtil.sum(list, toArray("id", "age"), notPredicate); assertThat(map, allOf(hasEntry("id", toBigDecimal(30)), hasEntry("age", toBigDecimal(200)))); }
/** * Consulta transições que consomem o símbolo informado a partir do estado * de origem também informado. * @param state Estado de origem. * @param symbol Símbolo a ser consumido. * @return Transições que consomem o símbolo informado a partir do estado * de origem também informado. */ public List<Transition> withSourceStateAndSymbol(State state, Symbol symbol) { Predicate[] chain = { new SourceStatePredicate(state), new SymbolPredicate(symbol) }; Collection<Transition> result = CollectionUtils.select( transitions, PredicateUtils.allPredicate(chain) ); return new ArrayList<>(result); }
/** * Consulta transições que partem do estado de origem informado e seguem até * o estado de destino também informado. * @param state1 Estado de origem. * @param state2 Estado de destino. * @return Transições que partem do estado de origem informado e seguem até * o estado de destino também informado. */ public List<Transition> withSourceAndTargetStates(State state1, State state2) { Predicate[] chain = { new SourceStatePredicate(state1), new TargetStatePredicate(state2) }; Collection<Transition> result = CollectionUtils.select( transitions, PredicateUtils.allPredicate(chain) ); return new ArrayList<>(result); }
/** * Consulta quais transições partem de um estado de origem, consomem o * símbolo e chegam até um estado de destino. * @param state1 Estado de origem. * @param symbol Símbolo a ser consumido. * @param state2 Estado de destino. * @return Transições partem de um estado de origem, consomem o símbolo e * chegam até um estado de destino. */ public List<Transition> withSourceStateSymbolAndTargetState(State state1, Symbol symbol, State state2) { Predicate[] chain = { new SourceStatePredicate(state1), new SymbolPredicate(symbol), new TargetStatePredicate(state2) }; Collection<Transition> result = CollectionUtils.select( transitions, PredicateUtils.allPredicate(chain) ); return new ArrayList<>(result); }
/** * Consulta transições que partem de um estado de origem e chamam uma * determinada submáquina. * @param state Estado de origem. * @param submachine Nome da submáquina. * @return Transições que partem de um estado de origem e chamam uma * determinada submáquina. */ public List<Transition> withSourceStateAndSumbmachine(State state, String submachine) { Predicate[] chain = { new SourceStatePredicate(state), new SubmachinePredicate(submachine) }; Collection<Transition> result = CollectionUtils.select( transitions, PredicateUtils.allPredicate(chain) ); return new ArrayList<>(result); }
/** * Consulta transições em vazio a partir do estado de origem. * @param state Estado de origem. * @return Transições em vazio a partir do estado de origem. */ public List<Transition> withEpsilonFromSourceState(State state) { Predicate[] chain = { new SourceStatePredicate(state), new EpsilonPredicate() }; Collection<Transition> result = CollectionUtils.select( transitions, PredicateUtils.allPredicate(chain) ); return new ArrayList<>(result); }
/** * Consulta transições em vazio ou com chamadas de submáquina a partir do * estado de origem informado. * @param state Estado de origem. * @return Transições em vazio ou com chamadas de submáquina a partir do * estado de origem informado. */ public List<Transition> withEpsilonOrSubmachineFromSourceState( State state) { Predicate[] chain = { new EpsilonPredicate(), new SubmachineCallPredicate() }; Collection<Transition> result = CollectionUtils.select( CollectionUtils.select( transitions, new SourceStatePredicate(state) ), PredicateUtils.anyPredicate(chain) ); return new ArrayList<>(result); }
/** * Consulta transições que consomem um determinado símbolo e chegam até o * estado de destino especificado. * @param symbol Símbolo a ser consumido. * @param state Estado de destino. * @return Transições que consomem um determinado símbolo e chegam até o * estado de destino especificado. */ public List<Transition> withSymbolAndTargetState(Symbol symbol, State state) { Predicate[] chain = { new SymbolPredicate(symbol), new TargetStatePredicate(state) }; Collection<Transition> result = CollectionUtils.select( transitions, PredicateUtils.allPredicate(chain) ); return new ArrayList<>(result); }
/** * Test find2. */ @Test 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)); }
@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)); }
@Test public void testContainsPredicate12(){ User guanyu30 = new User("关羽", 30); Predicate<User> predicate = PredicateUtils.andPredicate( BeanPredicateUtil.containsPredicate("name", "关羽", "刘备"), BeanPredicateUtil.containsPredicate("age", 30)); assertEquals(true, predicate.evaluate(guanyu30)); }
@Test public void test(){ User user = new User("feilong"); Predicate<User> predicate = new BeanPredicate<>("name", PredicateUtils.equalPredicate("feilong")); assertTrue(predicate.evaluate(user)); }
/** * 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)); }
private FilterPredicate(Settings settings, Set<WiFiBand> wiFiBands) { Predicate<WiFiDetail> ssidPredicate = makeSSIDPredicate(settings.getSSIDs()); Predicate<WiFiDetail> wiFiBandPredicate = EnumUtils.predicate(WiFiBand.class, wiFiBands, new WiFiBandTransformer()); Predicate<WiFiDetail> strengthPredicate = EnumUtils.predicate(Strength.class, settings.getStrengths(), new StrengthTransformer()); Predicate<WiFiDetail> securityPredicate = EnumUtils.predicate(Security.class, settings.getSecurities(), new SecurityTransformer()); List<Predicate<WiFiDetail>> predicates = Arrays.asList(ssidPredicate, wiFiBandPredicate, strengthPredicate, securityPredicate); this.predicate = PredicateUtils.allPredicate(CollectionUtils.select(predicates, new NoTruePredicate())); }
@NonNull @Override public List<String> findVendors(@NonNull String filter) { Predicate<String> predicate = PredicateUtils.anyPredicate(new StringContains(filter), new MacContains(filter)); return new ArrayList<>(CollectionUtils.select(getVendors().keySet(), predicate)); }
@Test public void testFindUsingPredicate() throws Exception { // 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); }
@Test public void testFindUsingPredicateWhenNothingFound() throws Exception { // setup TestObject expected = TestObject.VALUE2; Predicate<TestObject> predicate = PredicateUtils.falsePredicate(); // execute TestObject actual = EnumUtils.find(TestObject.class, predicate, expected); // validate assertEquals(expected, actual); }
/** * Obtém as transições com consumo de símbolo a partir da consulta corrente. * @param query Consulta. * @return Lista de transições. */ private List<Transition> getSymbolTransitions(List<Transition> query) { Predicate[] chain = new Predicate[]{new EpsilonPredicate(), new SubmachineCallPredicate()}; Collection<Transition> result = CollectionUtils.select(query, PredicateUtils.nonePredicate(chain)); return new ArrayList<>(result); }
@Test public void testEqualPredicateNullMap(){ assertEquals(PredicateUtils.nullPredicate(), BeanPredicateUtil.equalPredicate((User) null)); }
private Predicate<WiFiDetail> makeSSIDPredicate(Set<String> ssids) { if (ssids.isEmpty()) { return PredicateUtils.truePredicate(); } return PredicateUtils.anyPredicate(CollectionUtils.collect(ssids, new SSIDTransformer())); }
@Override public boolean evaluate(Predicate<WiFiDetail> object) { return !PredicateUtils.truePredicate().equals(object); }
public static <T extends Enum, U> Predicate<U> predicate(@NonNull Class<T> enumType, @NonNull Collection<T> input, @NonNull Transformer<T, Predicate<U>> transformer) { if (input.size() >= values(enumType).size()) { return PredicateUtils.truePredicate(); } return PredicateUtils.anyPredicate(CollectionUtils.collect(input, transformer)); }
@Override public Predicate<TestObject> transform(TestObject input) { return PredicateUtils.truePredicate(); }
@SuppressWarnings({ CompilerWarnings.UNCHECKED }) public static <T, U> FilterIterator<U> instances(Iterator<T> iterator, Class<U> clazz) { return ((FilterIterator<U>) IteratorUtils.filteredIterator(iterator, PredicateUtils.instanceofPredicate(clazz))); }
public static Entity find(List<Entity> list, Entity e) { return CollectionUtils.find(list, PredicateUtils.equalPredicate(e)); }
/** * 构造属性与一个指定对象 <code>bean</code> 的一组属性的值 <code>propertyNames</code> 都相等的判断器. * * <h3>说明:</h3> * <blockquote> * <ol> * <li> * 常用于解析集合,如 {@link CollectionsUtil#select(Iterable, Predicate) select},{@link CollectionsUtil#find(Iterable, Predicate) find}, * {@link CollectionsUtil#selectRejected(Iterable, Predicate) selectRejected}, * {@link CollectionsUtil#group(Iterable, String, Predicate) group}, * {@link AggregateUtil#groupCount(Iterable, String, Predicate) groupCount}, * {@link AggregateUtil#sum(Iterable, String, Predicate) sum} 等方法. * </li> * </ol> * </blockquote> * * <h3>示例:</h3> * * <blockquote> * * <p> * <b>场景:</b> 在list中查找 名字是 关羽,并且 年龄是30 的user * </p> * * <pre class="code"> * * User guanyu30 = new User("关羽", 30); * List{@code <User>} list = toList(// * new User("张飞", 23), * new User("关羽", 24), * new User("刘备", 25), * guanyu30); * * Predicate{@code <User>} predicate = PredicateUtils * .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30)); * * assertEquals(guanyu30, CollectionsUtil.find(list, predicate)); * </pre> * * <p> * 此时你可以优化成: * </p> * * <pre class="code"> * * User guanyu30 = new User("关羽", 30); * List{@code <User>} list = toList(// * new User("张飞", 23), * new User("关羽", 24), * new User("刘备", 25), * guanyu30); * * assertEquals(guanyu30, find(list, BeanPredicateUtil.equalPredicate(guanyu30, "name", "age"))); * </pre> * * </blockquote> * * @param <T> * the generic type * @param bean * the bean * @param propertyNames * 如果 <code>propertyNames</code> 是null或者empty 那么取所有属性值. * @return 如果 <code>bean</code> 是null,返回 {@link org.apache.commons.collections4.PredicateUtils#nullPredicate()}<br> * 否则 调用 {@link com.feilong.core.bean.PropertyUtil#describe(Object, String...)} 提取属性值,再调用 {@link #equalPredicate(Map)} * @see #equalPredicate(String, Object) * @see com.feilong.core.bean.PropertyUtil#describe(Object, String...) * @since 1.10.6 */ public static <T> Predicate<T> equalPredicate(T bean,String...propertyNames){ if (null == bean){ return PredicateUtils.nullPredicate(); } Map<String, ?> propertyNameAndPropertyValueMap = PropertyUtil.describe(bean, propertyNames); return equalPredicate(propertyNameAndPropertyValueMap); }
/** * 移除所有的null元素 * * @param coll * @param <T> * @return */ public static <T> boolean removeNull(Collection<T> coll) { return CollectionUtils.filter(coll, PredicateUtils.notNullPredicate()); }
/** * 用来指定 <code>T</code> 对象的特定属性 <code>propertyName</code> equals 指定的 <code>propertyValue</code>. * * <h3>说明:</h3> * <blockquote> * <ol> * <li> * 常用于解析集合,如 {@link CollectionsUtil#select(Iterable, Predicate) select},{@link CollectionsUtil#find(Iterable, Predicate) find}, * {@link CollectionsUtil#selectRejected(Iterable, Predicate) selectRejected}, * {@link CollectionsUtil#group(Iterable, String, Predicate) group}, * {@link AggregateUtil#groupCount(Iterable, String, Predicate) groupCount}, * {@link AggregateUtil#sum(Iterable, String, Predicate) sum} 等方法. * </li> * </ol> * </blockquote> * * <h3>示例:</h3> * * <blockquote> * * <p> * <b>场景:</b> 在list中查找 名字是 关羽,并且 年龄是30 的user * </p> * * <pre class="code"> * * User guanyu30 = new User("关羽", 30); * List{@code <User>} list = toList(// * new User("张飞", 23), * new User("关羽", 24), * new User("刘备", 25), * guanyu30); * * Predicate{@code <User>} predicate = PredicateUtils * .andPredicate(BeanPredicateUtil.equalPredicate("name", "关羽"), BeanPredicateUtil.equalPredicate("age", 30)); * * assertEquals(guanyu30, CollectionsUtil.find(list, predicate)); * * </pre> * * </blockquote> * * @param <T> * the generic type * @param <V> * the value type * @param propertyName * 泛型T对象指定的属性名称,Possibly indexed and/or nested name of the property to be modified,参见 * <a href="../../bean/BeanUtil.html#propertyName">propertyName</a> * @param propertyValue * the property value * @return 如果 <code>propertyName</code> 是null,抛出 {@link NullPointerException}<br> * 如果 <code>propertyName</code> 是blank,抛出 {@link IllegalArgumentException}<br> * @see org.apache.commons.collections4.PredicateUtils#equalPredicate(Object) */ public static <T, V> Predicate<T> equalPredicate(String propertyName,V propertyValue){ Validate.notBlank(propertyName, "propertyName can't be blank!"); return new BeanPredicate<>(propertyName, PredicateUtils.equalPredicate(propertyValue)); }
/** * find the first element in list, who equals to e * * @return the first event in list */ public static CompositeEvent find(List<CompositeEvent> list, CompositeEvent e) { return CollectionUtils.find(list, PredicateUtils.equalPredicate(e)); }
/** * find the first element in list, who equals to e * * @return the first event in list */ public static Event find(List<Event> list, Event e) { return CollectionUtils.find(list, PredicateUtils.equalPredicate(e)); }