Example usage for java.util Queue isEmpty

List of usage examples for java.util Queue isEmpty

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this collection contains no elements.

Usage

From source file:org.kuali.rice.krad.uif.util.ComponentUtils.java

/**
 * places a key, value pair in each context map of a list of components
 *
 * @param elements the list of elements/*ww w .  jav a 2s.  c o m*/
 * @param contextName a value to be used as a key to retrieve the object
 * @param contextValue the value to be placed in the context
 */
public static void pushObjectToContext(Collection<? extends LifecycleElement> elements, String contextName,
        Object contextValue) {
    if (elements == null || elements.isEmpty()) {
        return;
    }

    Queue<LifecycleElement> elementQueue = new LinkedList<LifecycleElement>();

    try {
        elementQueue.addAll(elements);
        while (!elementQueue.isEmpty()) {
            LifecycleElement currentElement = elementQueue.poll();

            if (currentElement == null) {
                continue;
            }

            if (currentElement instanceof Component) {
                ((Component) currentElement).pushObjectToContext(contextName, contextValue);
            }

            elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
        }
    } finally {
        elementQueue.clear();
        RecycleUtils.recycle(elementQueue);
    }
}

From source file:com.android.repository.util.InstallerUtil.java

/**
 * Compute the complete list of packages that need to be installed to meet the dependencies of
 * the given list (including the requested packages themselves, if they are not already
 * installed). Returns {@code null} if we were unable to compute a complete list of dependencies
 * due to not being able to find required packages of the specified version.
 *
 * Packages are returned in install order (that is, if we request A which depends on B, the
 * result will be [B, A]). If a dependency cycle is encountered the order of the returned
 * results at or below the cycle is undefined. For example if we have A -> [B, C], B -> D, and D
 * -> B then the returned list will be either [B, D, C, A] or [D, B, C, A].
 *
 * Note that we assume installed packages already have their dependencies met.
 *//*from  w w  w.  ja  va2s. c  om*/
@Nullable
public static List<RemotePackage> computeRequiredPackages(@NonNull Collection<RemotePackage> requests,
        @NonNull RepositoryPackages packages, @NonNull ProgressIndicator logger) {
    Set<RemotePackage> requiredPackages = Sets.newHashSet();
    Map<String, UpdatablePackage> consolidatedPackages = packages.getConsolidatedPkgs();

    Set<String> seen = Sets.newHashSet();
    Multimap<String, Dependency> allDependencies = HashMultimap.create();
    Set<RemotePackage> roots = Sets.newHashSet();
    Queue<RemotePackage> current = Lists.newLinkedList();
    for (RemotePackage request : requests) {
        UpdatablePackage updatable = consolidatedPackages.get(request.getPath());
        if (updatable == null) {
            logger.logWarning(String.format("No package with key %s found!", request.getPath()));
            return null;
        }
        if (!updatable.hasLocal() || updatable.isUpdate()) {
            current.add(request);
            roots.add(request);
            requiredPackages.add(request);
            seen.add(request.getPath());
        }
    }

    while (!current.isEmpty()) {
        RemotePackage currentPackage = current.remove();

        Collection<Dependency> currentDependencies = currentPackage.getAllDependencies();
        for (Dependency d : currentDependencies) {
            String dependencyPath = d.getPath();
            UpdatablePackage updatableDependency = consolidatedPackages.get(dependencyPath);
            if (updatableDependency == null) {
                logger.logWarning(String.format("Dependant package with key %s not found!", dependencyPath));
                return null;
            }
            LocalPackage localDependency = updatableDependency.getLocal();
            Revision requiredMinRevision = null;
            RevisionType r = d.getMinRevision();
            if (r != null) {
                requiredMinRevision = r.toRevision();
            }
            if (localDependency != null && (requiredMinRevision == null
                    || requiredMinRevision.compareTo(localDependency.getVersion()) <= 0)) {
                continue;
            }
            if (seen.contains(dependencyPath)) {
                allDependencies.put(dependencyPath, d);
                continue;
            }
            seen.add(dependencyPath);
            RemotePackage remoteDependency = updatableDependency.getRemote();
            if (remoteDependency == null || (requiredMinRevision != null
                    && requiredMinRevision.compareTo(remoteDependency.getVersion()) > 0)) {
                logger.logWarning(String.format("Package \"%1$s\" with revision at least %2$s not available.",
                        updatableDependency.getRepresentative().getDisplayName(), requiredMinRevision));
                return null;
            }

            requiredPackages.add(remoteDependency);
            allDependencies.put(dependencyPath, d);
            current.add(remoteDependency);
            // We had a dependency on it, so it can't be a root.
            roots.remove(remoteDependency);
        }
    }

    List<RemotePackage> result = Lists.newArrayList();

    while (!roots.isEmpty()) {
        RemotePackage root = roots.iterator().next();
        roots.remove(root);
        result.add(root);
        for (Dependency d : root.getAllDependencies()) {
            Collection<Dependency> nodeDeps = allDependencies.get(d.getPath());
            if (nodeDeps.size() == 1) {
                UpdatablePackage newRoot = consolidatedPackages.get(d.getPath());
                if (newRoot == null) {
                    logger.logWarning(String.format("Package with key %s not found!", d.getPath()));
                    return null;
                }

                roots.add(newRoot.getRemote());
            }
            nodeDeps.remove(d);
        }
    }

    if (result.size() != requiredPackages.size()) {
        logger.logInfo("Failed to sort dependencies, returning partially-sorted list.");
        for (RemotePackage p : result) {
            requiredPackages.remove(p);
        }
        result.addAll(requiredPackages);
    }

    return Lists.reverse(result);
}

From source file:org.intellij.erlang.psi.impl.ErlangPsiImplUtil.java

private static boolean processDeclarationRecursive(ErlangCompositeElement o, PsiScopeProcessor processor,
        ResolveState state) {//from w  w w  .j  ava2  s  . com
    Queue<ErlangCompositeElement> queue = new LinkedList<ErlangCompositeElement>();
    queue.add(o);
    while (!queue.isEmpty()) {
        ErlangCompositeElement top = queue.remove();
        if (!processor.execute(top, state))
            return false;
        queue.addAll(PsiTreeUtil.getChildrenOfTypeAsList(top, ErlangCompositeElement.class));
    }
    return true;
}

From source file:org.wicketopia.mapping.ClassBasedTypeMapping.java

public String getTypeName(Class<?> propertyType) {
    final Queue<Class<?>> typeQueue = createTypeQueue(propertyType);
    while (!typeQueue.isEmpty()) {
        Class<?> type = typeQueue.remove();
        final String editorType = typeMap.get(type.getName());
        if (editorType != null) {
            return editorType;
        }//from   w ww . j a  va 2  s .  c o  m
    }
    return defaultType;
}

From source file:org.kuali.rice.krad.uif.util.ComponentUtils.java

public static void prefixBindingPathNested(Component component, String addBindingPrefix) {
    @SuppressWarnings("unchecked")
    Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(LinkedList.class);
    elementQueue.offer(component);/*w w  w . j  a  v  a 2 s  .  c  o  m*/

    try {
        while (!elementQueue.isEmpty()) {
            LifecycleElement currentElement = elementQueue.poll();
            if (currentElement == null) {
                continue;
            }

            if (currentElement instanceof DataBinding) {
                if (LOG.isDebugEnabled()) {
                    LOG.info("setting nested binding prefix '" + addBindingPrefix + "' on " + currentElement);
                }
                prefixBindingPath((DataBinding) currentElement, addBindingPrefix);
            }

            elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
        }
    } finally {
        elementQueue.clear();
        RecycleUtils.recycle(elementQueue);
    }
}

From source file:org.kuali.rice.krad.uif.util.ComponentUtils.java

/**
 * Replace all IDs from a component and its children with new generated ID values.
 *
 * <p>If there are features that depend on a static id of this
 * component, this call may cause errors.</p>
 *
 * @param components A list of component to clear all IDs from.
 * @see AssignIdsTask For a complete description of the algorithm.
 *///from  w w  w .  jav  a  2 s. c  o m
public static void clearAndAssignIds(List<? extends Component> components) {
    if (components == null || components.isEmpty()) {
        return;
    }

    int hash = 1;
    @SuppressWarnings("unchecked")
    Queue<LifecycleElement> toClear = RecycleUtils.getInstance(LinkedList.class);
    toClear.addAll(components);
    try {
        while (!toClear.isEmpty()) {
            LifecycleElement element = toClear.poll();

            hash = generateId(element, hash);

            for (LifecycleElement nested : ViewLifecycleUtils.getElementsForLifecycle(element).values()) {
                if (nested != null) {
                    toClear.add(nested);
                }
            }

            if (element instanceof Component) {
                List<Component> propertyReplacerComponents = ((Component) element)
                        .getPropertyReplacerComponents();
                if (propertyReplacerComponents == null) {
                    continue;
                }

                for (Component nested : propertyReplacerComponents) {
                    if (nested != null) {
                        toClear.add(nested);
                    }
                }
            }
        }
    } finally {
        toClear.clear();
        RecycleUtils.recycle(toClear);
    }
}

From source file:org.kuali.rice.krad.uif.util.ComponentUtils.java

/**
 * places a all entries from a map into each context map of a list of components
 *
 * @param components The list components.
 * @param sourceContext The source context map.
 *///from   w  w w .  j  ava 2s . co  m
public static void pushAllToContext(List<? extends Component> components, Map<String, Object> sourceContext) {
    if (components == null || components.isEmpty()) {
        return;
    }

    @SuppressWarnings("unchecked")
    Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(LinkedList.class);
    try {
        elementQueue.addAll(components);
        while (!elementQueue.isEmpty()) {
            LifecycleElement currentElement = elementQueue.poll();

            if (currentElement == null) {
                continue;
            }

            if (currentElement instanceof Component) {
                ((Component) currentElement).pushAllToContext(sourceContext);
            }

            elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
        }
    } finally {
        elementQueue.clear();
        RecycleUtils.recycle(elementQueue);
    }
}

From source file:org.kuali.rice.krad.uif.util.ComponentUtils.java

/**
 * Get all nested children of a given component.
 *
 * @param component The component to search.
 * @return All nested children of the component.
 * @see ViewLifecycleUtils#getElementsForLifecycle(LifecycleElement)
 *///from w  w  w . ja v a  2 s  .c o m
public static List<Component> getAllNestedComponents(Component component) {
    if (component == null) {
        return Collections.emptyList();
    }

    List<Component> components = Collections.emptyList();
    @SuppressWarnings("unchecked")
    Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(LinkedList.class);
    elementQueue.offer(component);

    try {
        while (!elementQueue.isEmpty()) {
            LifecycleElement currentElement = elementQueue.poll();

            if (currentElement == null) {
                continue;
            }

            if (currentElement instanceof Component && currentElement != component) {
                if (components.isEmpty()) {
                    components = new ArrayList<Component>();
                }

                components.add((Component) currentElement);
            }

            elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());
        }
    } finally {
        elementQueue.clear();
        RecycleUtils.recycle(elementQueue);
    }

    return components;
}

From source file:org.kuali.rice.krad.uif.util.ComponentUtils.java

/**
 * Traverse a component tree, setting a property on all components for which the property is writable.
 *
 * @param <T> component type//  ww  w  .  j  a v a 2 s  . c  o m
 * @param <T> component type
 * @param components The components to traverse.
 * @param propertyPath The property path to set.
 * @param propertyValue The property value to set.
 * @see ObjectPropertyUtils#isWritableProperty(Object, String)
 * @see ObjectPropertyUtils#setPropertyValue(Object, String, Object)
 */
public static <T extends Component> void setComponentsPropertyDeep(List<T> components, String propertyPath,
        Object propertyValue) {
    if (components == null || components.isEmpty()) {
        return;
    }

    Set<Class<?>> skipTypes = null;
    @SuppressWarnings("unchecked")
    Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(LinkedList.class);
    elementQueue.addAll(components);

    try {
        while (!elementQueue.isEmpty()) {
            LifecycleElement currentElement = elementQueue.poll();
            if (currentElement == null) {
                continue;
            }

            elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(currentElement).values());

            Class<?> componentClass = currentElement.getClass();
            if (skipTypes != null && skipTypes.contains(componentClass)) {
                continue;
            }

            if (!ObjectPropertyUtils.isWritableProperty(currentElement, propertyPath)) {
                if (skipTypes == null) {
                    skipTypes = new HashSet<Class<?>>();
                }
                skipTypes.add(componentClass);
                continue;
            }

            ObjectPropertyUtils.setPropertyValue(currentElement, propertyPath, propertyValue, true);
        }
    } finally {
        elementQueue.clear();
        RecycleUtils.recycle(elementQueue);
    }
}

From source file:com.dbay.apns4j.impl.ApnsResender.java

public void resend(String name, Queue<PushNotification> queue) {
    IApnsService service = ApnsServiceImpl.getCachedService(name);
    if (service != null) {
        while (!queue.isEmpty()) {
            service.sendNotification(queue.poll());
        }/*from  w w w  .  j ava 2 s.c  o  m*/
    } else {
        logger.error("Cached service is null. name: " + name);
    }
}