Example usage for java.util Set isEmpty

List of usage examples for java.util Set isEmpty

Introduction

In this page you can find the example usage for java.util Set isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this set contains no elements.

Usage

From source file:I18NUtil.java

/**
 * Searches for the nearest locale from the available options.  To match any locale, pass in
 * <tt>null</tt>.//w w w .ja  v  a  2 s . c o m
 * 
 * @param templateLocale the template to search for or <tt>null</tt> to match any locale
 * @param options the available locales to search from
 * @return Returns the best match from the available options, or the <tt>null</tt> if
 *      all matches fail
 */
public static Locale getNearestLocale(Locale templateLocale, Set<Locale> options) {
    if (options.isEmpty()) // No point if there are no options
    {
        return null;
    } else if (templateLocale == null) {
        for (Locale locale : options) {
            return locale;
        }
    } else if (options.contains(templateLocale)) // First see if there is an exact match
    {
        return templateLocale;
    }
    // make a copy of the set
    Set<Locale> remaining = new HashSet<Locale>(options);

    // eliminate those without matching languages
    Locale lastMatchingOption = null;
    String templateLanguage = templateLocale.getLanguage();
    if (templateLanguage != null && !templateLanguage.equals("")) {
        Iterator<Locale> iterator = remaining.iterator();
        while (iterator.hasNext()) {
            Locale option = iterator.next();
            if (option != null && !templateLanguage.equals(option.getLanguage())) {
                iterator.remove(); // It doesn't match, so remove
            } else {
                lastMatchingOption = option; // Keep a record of the last match
            }
        }
    }
    if (remaining.isEmpty()) {
        return null;
    } else if (remaining.size() == 1 && lastMatchingOption != null) {
        return lastMatchingOption;
    }

    // eliminate those without matching country codes
    lastMatchingOption = null;
    String templateCountry = templateLocale.getCountry();
    if (templateCountry != null && !templateCountry.equals("")) {
        Iterator<Locale> iterator = remaining.iterator();
        while (iterator.hasNext()) {
            Locale option = iterator.next();
            if (option != null && !templateCountry.equals(option.getCountry())) {
                // It doesn't match language - remove
                // Don't remove the iterator. If it matchs a langage but not the country, returns any matched language                     
                // iterator.remove();
            } else {
                lastMatchingOption = option; // Keep a record of the last match
            }
        }
    }
    /*if (remaining.isEmpty())
    {
    return null;
    }
    else */
    if (remaining.size() == 1 && lastMatchingOption != null) {
        return lastMatchingOption;
    } else {
        // We have done an earlier equality check, so there isn't a matching variant
        // Also, we know that there are multiple options at this point, either of which will do.

        // This gets any country match (there will be worse matches so we take the last the country match)
        if (lastMatchingOption != null) {
            return lastMatchingOption;
        } else {
            for (Locale locale : remaining) {
                return locale;
            }
        }
    }
    // The logic guarantees that this code can't be called
    throw new RuntimeException("Logic should not allow code to get here.");
}

From source file:com.espertech.esper.epl.lookup.EventTableIndexUtil.java

public static Pair<IndexMultiKey, EventTableIndexEntryBase> findIndexBestAvailable(
        Map<IndexMultiKey, ? extends EventTableIndexEntryBase> tablesAvailable, Set<String> keyPropertyNames,
        Set<String> rangePropertyNames, List<IndexHintInstruction> optionalIndexHintInstructions) {
    if (keyPropertyNames.isEmpty() && rangePropertyNames.isEmpty()) {
        return null;
    }//from  ww w.  ja  v a 2 s.  c  om

    // determine candidates
    List<IndexedPropDesc> hashProps = new ArrayList<IndexedPropDesc>();
    for (String keyPropertyName : keyPropertyNames) {
        hashProps.add(new IndexedPropDesc(keyPropertyName, null));
    }
    List<IndexedPropDesc> rangeProps = new ArrayList<IndexedPropDesc>();
    for (String rangePropertyName : rangePropertyNames) {
        rangeProps.add(new IndexedPropDesc(rangePropertyName, null));
    }
    Map<IndexMultiKey, EventTableIndexEntryBase> indexCandidates = (Map<IndexMultiKey, EventTableIndexEntryBase>) EventTableIndexUtil
            .findCandidates(tablesAvailable, hashProps, rangeProps);

    // handle hint
    if (optionalIndexHintInstructions != null) {
        IndexMultiKey found = EventTableIndexUtil.findByIndexHint(indexCandidates,
                optionalIndexHintInstructions);
        if (found != null) {
            return getPair(tablesAvailable, found);
        }
    }

    // no candidates
    if (indexCandidates == null || indexCandidates.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("No index found.");
        }
        return null;
    }

    return getBestCandidate(indexCandidates);
}

From source file:com.symbian.driver.core.controller.utils.ModelUtils.java

/**
 * Checks if the file is eclipsing a file in one of the parent nodes.
 * /*www  .jav a 2s . co m*/
 * @param aFile
 * @param aTaskNode
 * @return The Task node where the file is eclipsing with.
 */
public static Task isFileEclipsing(String aFile, Task aTaskNode) {
    if (aFile == null || aFile.length() < 1 || aTaskNode == null) {
        LOGGER.warning("File eclipsing check received invalid file/task name.");
        return null;
    }

    String lFile = aFile.toLowerCase();

    if (sEclipsingStack.containsKey(lFile.toLowerCase())) {
        Set<Task> lNode = sEclipsingStack.get(lFile);
        if (lNode != null && !lNode.isEmpty()) {

            for (Task lNextObject : lNode) {
                if (EcoreUtil.isAncestor(lNextObject, aTaskNode)) {
                    return lNextObject;
                }
            }

            addNode(lFile.toLowerCase(), aTaskNode, lNode);
            return null;
        }
    }

    addNode(lFile.toLowerCase(), aTaskNode, new HashSet<Task>());
    return null;
}

From source file:org.opendatakit.briefcase.util.WebUtils.java

public static final String createLinkWithProperties(String url, Map<String, String> properties) {
    StringBuilder urlBuilder = new StringBuilder();
    urlBuilder.append(url);//from w w w . j av a 2s  . c o  m
    if (properties != null) {
        Set<Map.Entry<String, String>> propSet = properties.entrySet();
        if (!propSet.isEmpty()) {
            urlBuilder.append("?");
            boolean firstParam = true;
            for (Map.Entry<String, String> property : propSet) {
                if (firstParam) {
                    firstParam = false;
                } else {
                    urlBuilder.append("&");
                }

                String value = property.getValue();
                if (value == null) {
                    value = "NULL";
                }

                String valueEncoded;
                try {
                    valueEncoded = URLEncoder.encode(value, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalStateException("unrecognized UTF-8 encoding");
                }
                urlBuilder.append(property.getKey() + "=" + valueEncoded);
            }
        }
    }
    return urlBuilder.toString();
}

From source file:com.javaid.bolaky.carpool.service.acl.pools.impl.PoolsAclTranslator.java

private static Integer getNumberOfNewRequest(Set<Passenger> passengers) {

    Integer numberOfNewRequest = null;

    if (passengers != null && !passengers.isEmpty()) {

        for (Passenger passenger : passengers) {

            if (StateStatus.PENDING.equals(passenger.getStateStatus())) {

                if (numberOfNewRequest == null) {
                    numberOfNewRequest = 0;
                }//from ww w .ja  v a2s  .  co m

                numberOfNewRequest++;
            }
        }
    }

    return numberOfNewRequest;
}

From source file:ddf.security.common.audit.SecurityLogger.java

private static String getUser(Subject subject) {
    try {//ww w.  j a  v a  2  s .  c o m
        if (subject == null) {
            subject = ThreadContext.getSubject();
        }
        if (subject == null) {
            javax.security.auth.Subject javaSubject = javax.security.auth.Subject
                    .getSubject(AccessController.getContext());
            if (javaSubject != null) {
                Set<UserPrincipal> userPrincipal = javaSubject.getPrincipals(UserPrincipal.class);
                if (userPrincipal != null && !userPrincipal.isEmpty()) {
                    return userPrincipal.toArray(new UserPrincipal[1])[0].getName();
                }
            }
        } else {
            return SubjectUtils.getName(subject, NO_USER);
        }
    } catch (Exception e) {
        // ignore and return NO_USER
    }
    return NO_USER;
}

From source file:org.zkoss.spring.security.SecurityUtil.java

/**
 * Return true if the authenticated principal is granted NONE of the roles 
 * specified in authorities.//from  w  w w. j  a  v  a2s.co  m
 * 
 * @param authorities A comma separated list of roles which the user must 
 * have been granted NONE.
 * @return true if the authenticated principal is granted authorities 
 * of NONE the specified roles.
 */
public static boolean isNoneGranted(String authorities) {
    if (null == authorities || "".equals(authorities)) {
        return false;
    }
    final Collection<? extends GrantedAuthority> granted = getPrincipalAuthorities();
    final Set grantedCopy = retainAll(granted, parseAuthoritiesString(authorities));
    return grantedCopy.isEmpty();
}

From source file:org.zkoss.spring.security.SecurityUtil.java

/**
 * Return true if the authenticated principal is granted ANY of the roles 
 * specified in authorities./*  w ww  . j a  v a2 s  . com*/
 * 
 * @param authorities A comma separated list of roles which the user must have  
 * been granted ANY.
 * @return true true if the authenticated principal is granted authorities 
 * of ALL the specified roles.
 */
public static boolean isAnyGranted(String authorities) {
    if (null == authorities || "".equals(authorities)) {
        return false;
    }
    final Collection<? extends GrantedAuthority> granted = getPrincipalAuthorities();
    final Set grantedCopy = retainAll(granted, parseAuthoritiesString(authorities));
    return !grantedCopy.isEmpty();
}

From source file:com.javaid.bolaky.carpool.service.acl.pools.impl.PoolsAclTranslator.java

public static Set<CarPoolError> convertToCarPoolErrors(Set<PoolsError> poolsErrors) {

    Set<CarPoolError> carPoolErrors = null;

    if (poolsErrors != null && !poolsErrors.isEmpty()) {

        carPoolErrors = new ListOrderedSet<CarPoolError>();

        for (PoolsError poolsError : poolsErrors) {

            CarPoolError carPoolError = CarPoolError.convertFrom(poolsError);

            if (carPoolError != null) {
                carPoolErrors.add(carPoolError);
            }/*from w w w. j  a v a  2 s  .  co m*/
        }
    }

    return carPoolErrors;
}

From source file:net.sourceforge.fenixedu.domain.Guide.java

static public Guide readLastVersionByNumberAndYear(Integer number, Integer year) {
    Set<Guide> result = new HashSet<Guide>();
    for (Guide guide : Bennu.getInstance().getGuidesSet()) {
        if (guide.getYear().equals(year) && guide.getNumber().equals(number)) {
            result.add(guide);//from w  w  w.  ja v  a  2 s.  c  om
        }
    }

    if (result.isEmpty()) {
        return null;
    }

    return Collections.max(result, Guide.COMPARATOR_BY_VERSION);
}