Example usage for org.apache.commons.collections15 ExtendedCollectionUtils isEmpty

List of usage examples for org.apache.commons.collections15 ExtendedCollectionUtils isEmpty

Introduction

In this page you can find the example usage for org.apache.commons.collections15 ExtendedCollectionUtils isEmpty.

Prototype

public static final boolean isEmpty(Collection<?> c) 

Source Link

Usage

From source file:org.apache.sshd.server.PublickeyAuthenticatorUtils.java

/**
 * @param entries A {@link Collection} of {@link CryptoKeyEntry}-ies
 * @return A {@link PublickeyAuthenticator} that matches the received encoded
 * public key bytes to one of the authorized keys published by the user
 * @see CryptoKeyEntry#readAuthorizedKeys(File)
 *//* ww  w .j a  v a  2 s  .  c om*/
public static final PublickeyAuthenticator authorizedKeysAuthenticator(
        Collection<? extends CryptoKeyEntry> entries) {
    final Map<String, ? extends Collection<CryptoKeyEntry>> keysMap = ExtendedMapUtils.mapCollectionMultiValues(
            CryptoKeyEntry.USERNAME_EXTRACTOR, ExtendedCollectionUtils.<CryptoKeyEntry>linkedListFactory(),
            entries);
    return new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            Collection<CryptoKeyEntry> keySet = keysMap.get(username);
            if (ExtendedCollectionUtils.isEmpty(keySet)) {
                return false;
            }

            final byte[] keyBytes = key.getEncoded();
            if (ArrayUtils.isEmpty(keyBytes)) {
                return false; // TODO consider throwing an exception
            }

            CryptoKeyEntry entry = CollectionUtils.find(keySet,
                    new AbstractExtendedPredicate<CryptoKeyEntry>(CryptoKeyEntry.class) {
                        @Override
                        public boolean evaluate(CryptoKeyEntry e) {
                            byte[] entryBytes = e.getKeyData();
                            if (Arrays.equals(keyBytes, entryBytes)) {
                                return true;
                            } else {
                                return false; // debug breakpoint;
                            }
                        }
                    });
            if (entry == null) {
                return false;
            } else {
                return true;
            }
        }
    };
}

From source file:org.springframework.core.convert.converter.ExtendedConverterRegistryUtils.java

public static final void addConverters(ConverterRegistry registry,
        Collection<? extends ExtendedConverter<?, ?>> converters) {
    if (ExtendedCollectionUtils.isEmpty(converters)) {
        return;//from  ww w. java  2  s .c o  m
    }

    for (ExtendedConverter<?, ?> c : converters) {
        registry.addConverter(c.getSourceType(), c.getTargetType(), c);
    }
}

From source file:org.springframework.jdbc.repo.impl.AbstractIdentifiedEntityRepo.java

@Override
public List<E> findEntities(Collection<String> idsList) {
    if (ExtendedCollectionUtils.isEmpty(idsList)) {
        return Collections.emptyList();
    }//from  w w  w.  j  a  v  a  2 s.  c om

    final List<E> result = new ArrayList<E>(idsList.size());
    Set<String> idSet = (idsList instanceof Set) ? (Set<String>) idsList : new LinkedHashSet<>(idsList);
    CollectionUtils.forAllDo(idSet, new AbstractExtendedClosure<String>(String.class) {
        @Override
        public void execute(String id) {
            if (StringUtils.isEmpty(id)) {
                return;
            }

            E entity = findEntityById(id);
            if (entity == null) {
                return;
            }

            result.add(entity);
        }
    });
    return result;
}

From source file:org.springframework.jdbc.repo.impl.AbstractIdentifiedEntityRepo.java

@Override
public List<E> removeAll(Collection<String> idsList) {
    if (ExtendedCollectionUtils.isEmpty(idsList)) {
        return Collections.emptyList();
    }// w  w  w.j  a  va  2s.  c  o  m

    final List<E> result = new ArrayList<E>(idsList.size());
    CollectionUtils.forAllDo(idsList, new AbstractExtendedClosure<String>(String.class) {
        @Override
        public void execute(String id) {
            if (StringUtils.isEmpty(id)) {
                return;
            }

            E entity = removeEntity(id);
            if (entity == null) {
                return;
            }

            result.add(entity);
        }
    });
    return result;
}

From source file:org.springframework.jdbc.repo.impl.AbstractRawPropertiesRepoImpl.java

public List<E> toEntitiesList(Transformer<Map<String, ?>, ? extends E> transformer,
        Collection<String> idsList) {
    if (ExtendedCollectionUtils.isEmpty(idsList)) {
        return Collections.emptyList();
    }//  www  .ja v  a2 s.c  om

    List<E> result = new ArrayList<E>(idsList.size());
    for (String id : idsList) {
        E entity = getEntity(id, transformer);
        if (entity == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("toEntitiesList(" + id + ") no longer persisted");
            }
            continue; // maybe someone beat us to it and deleted it
        }

        result.add(entity);
    }

    return result;
}

From source file:org.springframework.jdbc.repo.impl.jdbc.RawPropertiesRepoImpl.java

/**
 * @param id The entity ID/* ww  w  . j av  a 2s  .  co  m*/
 * @return A {@link Pair} whose left hand holds the internal identifier
 * and left hand the properties {@link Map} - <code>null</code> if cannot
 * locate internal identifier value
 */
Pair<Object, Map<String, Serializable>> resolveProperties(String id) {
    Object internalId = findInternalId(id);
    if (internalId == null) {
        return null;
    }

    List<Pair<String, Serializable>> propVals = jdbcAccessor.query(
            "SELECT * FROM " + ENTITY_PROPERTIES_TABLE + " WHERE " + PROP_OWNER_COL + " = :" + INTERNAL_ID_COL,
            Collections.singletonMap(INTERNAL_ID_COL, internalId), valueMapper);
    if (ExtendedCollectionUtils.isEmpty(propVals)) {
        return Pair.of(internalId, Collections.<String, Serializable>emptyMap());
    }

    Map<String, Serializable> propsMap = new TreeMap<String, Serializable>(String.CASE_INSENSITIVE_ORDER);
    for (Pair<String, Serializable> p : propVals) {
        String propName = p.getKey();
        Serializable propValue = p.getValue();
        if (propValue == null) {
            continue; // ignore null(s)
        }

        Serializable prevValue = propsMap.put(propName, propValue);
        if (prevValue != null) {
            throw new IllegalStateException("getProperties(" + getEntityClass().getSimpleName() + ")[" + id
                    + "]" + " multiple values for property=" + propName + ": " + propValue + ", " + prevValue);
        }
    }

    return Pair.of(internalId, propsMap);
}

From source file:org.springframework.jdbc.repo.impl.jdbc.RawPropertiesRepoImpl.java

@Override
@Transactional(readOnly = true)//  ww  w .  j  a v a 2 s  . c om
public List<String> findEntities(final String propName, final Predicate<? super Serializable> predicate) {
    if (StringUtils.isEmpty(propName)) {
        throw new IllegalArgumentException(
                "findEntities(" + getEntityClass().getSimpleName() + ") no property specified");
    }

    if (predicate == null) {
        throw new IllegalArgumentException(
                "findEntities(" + getEntityClass().getSimpleName() + ")[" + propName + "] no predicate");
    }

    final Set<String> internalIdsSet = new HashSet<String>();
    final Set<String> matchingSet = jdbcAccessor.query("SELECT " + PROPS_TABLE_ALIAS + "." + PROP_OWNER_COL
            + " AS " + PROP_OWNER_COL + " ," + PROPS_TABLE_ALIAS + "." + PROP_NAME_COL + " AS " + PROP_NAME_COL
            + " ," + PROPS_TABLE_ALIAS + "." + PROP_TYPE_COL + " AS " + PROP_TYPE_COL + " ," + PROPS_TABLE_ALIAS
            + "." + PROP_VALUE_COL + " AS " + PROP_VALUE_COL + " ," + ENTITIES_TABLE_ALIAS + "." + ENTITY_ID_COL
            + " AS " + ENTITY_ID_COL + " FROM " + ENTITY_PROPERTIES_TABLE + " AS " + PROPS_TABLE_ALIAS
            + " INNER JOIN " + IDENTIFIED_ENTITIES_TABLE + " AS " + ENTITIES_TABLE_ALIAS + " ON "
            + ENTITIES_TABLE_ALIAS + "." + INTERNAL_ID_COL + " = " + PROPS_TABLE_ALIAS + "." + PROP_OWNER_COL
            + " AND " + ENTITIES_TABLE_ALIAS + "." + ENTITY_TYPE_COL + " = :" + ENTITY_TYPE_COL + " AND "
            + PROPS_TABLE_ALIAS + "." + PROP_NAME_COL + " = :" + PROP_NAME_COL,
            new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER) {
                private static final long serialVersionUID = 1L;

                {
                    put(ENTITY_TYPE_COL, getEntityClass().getSimpleName());
                    put(PROP_NAME_COL, propName);
                }
            }, new ResultSetExtractor<Set<String>>() {
                @Override
                @SuppressWarnings("synthetic-access")
                public Set<String> extractData(ResultSet rs) throws SQLException, DataAccessException {
                    while (rs.next()) {
                        int rowNum = rs.getRow();
                        Pair<String, Serializable> rowValue = valueMapper.mapRow(rs, rowNum);
                        String rowName = rowValue.getLeft();
                        String ownerId = rs.getString(ENTITY_ID_COL);
                        // just for safety
                        if (ExtendedStringUtils.safeCompare(propName, rowName, false) != 0) {
                            logger.warn("findEntities(" + getEntityClass().getSimpleName() + ")[" + propName
                                    + "]" + " mismatched property name at row " + rowNum + " for owner="
                                    + ownerId + ": " + rowName);
                            continue;
                        }

                        Serializable propValue = rowValue.getRight();
                        if (!predicate.evaluate(propValue)) {
                            if (logger.isTraceEnabled()) {
                                logger.trace("findEntities(" + getEntityClass().getSimpleName() + ")["
                                        + propName + "]" + " skip row " + rowNum + " value=" + propValue
                                        + " for owner=" + ownerId);
                            }
                            continue;
                        }

                        if (StringUtils.isEmpty(ownerId)) {
                            throw new SQLException("findEntities(" + getEntityClass().getSimpleName() + ")["
                                    + propName + "]" + " no owner for row " + rowNum + " on matching value="
                                    + propValue);
                        }

                        if (logger.isTraceEnabled()) {
                            logger.trace("findEntities(" + getEntityClass().getSimpleName() + ")[" + propName
                                    + "]" + " matched row " + rowNum + " value=" + propValue + " for owner="
                                    + ownerId);
                        }

                        if (!internalIdsSet.add(ownerId)) {
                            continue; // debug breakpoint
                        }
                    }

                    return internalIdsSet;
                }
            });
    if (ExtendedCollectionUtils.isEmpty(matchingSet)) {
        return Collections.emptyList();
    } else {
        return new ArrayList<String>(matchingSet);
    }
}

From source file:org.springframework.jdbc.repo.impl.jdbc.RawPropertiesRepoImpl.java

@Override
protected <R> List<R> removeAll(Class<R> elementType,
        Transformer<Pair<String, Map<String, ?>>, ? extends R> transformer, Collection<String> ids) {
    Assert.notNull(elementType, "No removal result element type specified");
    if (ExtendedCollectionUtils.isEmpty(ids)) {
        return Collections.emptyList();
    }/*ww  w  . ja v  a2s.  co  m*/

    List<R> result = new ArrayList<>(ids.size());
    for (String id : ids) {
        Map<String, ?> props = removeProperties(id);
        if (ExtendedMapUtils.isEmpty(props)) {
            continue;
        }

        R element = transformer.transform(Pair.<String, Map<String, ?>>of(id, props));
        if (element == null) {
            continue;
        }

        result.add(element);
    }

    return result;
}

From source file:org.springframework.jdbc.repo.impl.jdbc.RawPropertiesRepoImpl.java

private Object findInternalId(final String id) {
    List<?> candidates = jdbcAccessor.query(
            "SELECT " + INTERNAL_ID_COL + " FROM " + IDENTIFIED_ENTITIES_TABLE + " WHERE " + ENTITY_TYPE_COL
                    + " = :" + ENTITY_TYPE_COL + " AND " + ENTITY_ID_COL + " =:" + ENTITY_ID_COL,
            new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER) {
                private static final long serialVersionUID = 1L;

                {/*w w w. j av a 2s. c o  m*/
                    put(ENTITY_TYPE_COL, getEntityClass().getSimpleName());
                    put(ENTITY_ID_COL, id);
                }
            }, JdbcOperationsUtils.AS_OBJECT_MAPPER);
    if (ExtendedCollectionUtils.isEmpty(candidates)) {
        return null;
    }

    if (candidates.size() != 1) {
        throw new IllegalStateException("findInternalId(" + getEntityClass().getSimpleName() + ")[" + id
                + "] multiple matches: " + candidates);
    }

    Object internalId = candidates.get(0);
    if (internalId == null) {
        throw new IllegalStateException(
                "findInternalId(" + getEntityClass().getSimpleName() + ")[" + id + "] null/empty internal ID");
    }

    return internalId;
}

From source file:org.springframework.util.AggregatedExtendedPlaceholderResolver.java

public AggregatedExtendedPlaceholderResolver(Collection<? extends PlaceholderResolver> resList) {
    this.resolvers = ExtendedCollectionUtils.isEmpty(resList) ? new ArrayList<PlaceholderResolver>()
            : new ArrayList<PlaceholderResolver>(resList);
}