Example usage for com.google.common.collect Iterables any

List of usage examples for com.google.common.collect Iterables any

Introduction

In this page you can find the example usage for com.google.common.collect Iterables any.

Prototype

public static <T> boolean any(Iterable<T> iterable, Predicate<? super T> predicate) 

Source Link

Document

Returns true if any element in iterable satisfies the predicate.

Usage

From source file:com.fluidops.iwb.widget.TripleEditorWidget.java

/**
 * Returns a list containing the preferred type(s) for objects of this
 * predicate. Entries to the list are the {@link InputTypeDetails}. 
 * Preferred types are based on rdfs:range,
 * owl:DatatypeProperty, owl:ObjectProperty. If no preferred type can be
 * determined, null is returned instead of an empty map.<br/>
 * <br/>//  w w w  .  java 2s .c  o m
 * Literals are understood as either untyped literals or XSD typed literals.
 * We understand most (but not all) built-in basic XSD literal types (see
 * http://www.w3.org/TR/xmlschema-2/#built-in-datatypes). Non-basic (e.g.,
 * xsd:int as a sub type of xsd:integer) could only be supported through
 * database level inference. If the XSD type of a literal is not supported
 * and there are no other supported range types of the predicates (e.g.,
 * through inference) we handle the type a an untyped literal as a fallback.<br/>
 * <br/>
 * XSD types anyURI and QName should ideally not be used, as their semantics
 * vary slightly from the notion used in RDF/SPARQL. If they are still used
 * they will be interpreted as enforcing an object property, which could
 * always link to any resource (including blank nodes).<br/>
 * XSD types float and double are being unified as generalized floating
 * numbers.<br/>
 * Binary types and incomplete date/time types (i.e., those that encode only
 * partial aspects of an absolute time stamp) are not supported.
 * 
 * @param pred
 *            Predicate URI.
 * @return Map of preferred basic types mapping to their details, or null
 */
public List<InputTypeDetails> getAutoPreferredTypes(URI pred) {
    List<InputTypeDetails> resList = new ArrayList<InputTypeDetails>();

    // we need to cache literals were we don't understand the specific
    // type to insert later as general/untyped literal IFF no other types
    // were detected or the general/untyped literal is already among the
    // otherwise detected types (otherwise, suppress unwanted relaxation
    // of accepted types by NOT adding those types at all: most likely they
    // are more special [and NOT more general] sub types of one of the the
    // detected types):
    ArrayList<URI> unknownLiteralTypes = new ArrayList<URI>();

    PropertyInfo propInf = dm.getPropertyInfo(pred);

    // iterating known ranges
    for (URI range : propInf.getRan()) {
        if (range.equals(RDFS.LITERAL) || range.equals(RDF.XMLLITERAL))
            addDetailsToBasicInputType(resList, Datatype.RDFS_LITERAL, range);
        else if (range.toString().startsWith("http://www.w3.org/2001/XMLSchema#")) {
            if (range.equals(XMLSchema.INTEGER))
                // subsumes all integer types (i.e., could be inferred)
                addDetailsToBasicInputType(resList, Datatype.XSD_INTEGER, range);
            else if (range.equals(XMLSchema.FLOAT) || range.equals(XMLSchema.DOUBLE))
                // float & double
                addDetailsToBasicInputType(resList, Datatype.XSD_DOUBLE, range);
            else if (range.equals(XMLSchema.BOOLEAN))
                // bool
                addDetailsToBasicInputType(resList, Datatype.XSD_BOOLEAN, range);
            else if (range.equals(XMLSchema.DURATION))
                // duration
                addDetailsToBasicInputType(resList, Datatype.XSD_DURATION, range);
            else if (range.equals(XMLSchema.DATE))
                // date & time
                addDetailsToBasicInputType(resList, Datatype.XSD_DATE, range);
            else if (range.equals(XMLSchema.STRING))
                // string
                addDetailsToBasicInputType(resList, Datatype.XSD_STRING, range);
            else if (range.equals(XMLSchema.ANYURI) || range.equals(XMLSchema.QNAME))
                // URI
                addDetailsToBasicInputType(resList, Datatype.RDFS_RESOURCE, range);
            else
                // don't know this literal type, but it sure is a literal
                unknownLiteralTypes.add(range);
        } else
            // range is URI
            addDetailsToBasicInputType(resList, Datatype.RDFS_RESOURCE, range);
    }

    // iterating types for known facts about URI vs. Literal input
    for (Resource t : propInf.getTypes())
        if (t.equals(OWL.DATATYPEPROPERTY)) {
            // for data types, only if nothing more specific is known
            if (resList.size() == 0)
                addDetailsToBasicInputType(resList, Datatype.RDFS_LITERAL, RDFS.LITERAL);
        } else if (t.equals(OWL.OBJECTPROPERTY))
            addDetailsToBasicInputType(resList, Datatype.RDFS_RESOURCE, RDFS.RESOURCE);

    // if we have some literal ranges but don't understand which ones, allow
    // untyped literals unless we already have configured more specific
    // types:
    if (unknownLiteralTypes.size() > 0
            && (resList.size() == 0 || Iterables.any(resList, new Predicate<InputTypeDetails>() {
                @Override
                public boolean apply(InputTypeDetails input) {
                    return input.getDattype() == Datatype.RDFS_LITERAL;
                }
            }))) {
        for (URI range : unknownLiteralTypes)
            addDetailsToBasicInputType(resList, Datatype.RDFS_LITERAL, range);
    }

    if (resList.size() > 0)
        return resList;
    else
        return null;
}

From source file:com.eucalyptus.autoscaling.backend.AutoScalingBackendService.java

private static boolean isReserved(final String text) {
    return Iterables.any(reservedPrefixes, Strings.isPrefixOf(text));
}

From source file:com.netxforge.netxstudio.common.model.StudioUtils.java

/**
 * Get all {@link NetXResource resource }objects for a {@link NodeType }.
 * //  w  w w. j ava 2 s.com
 * @param nodeTypes
 * @return
 */
public static List<NetXResource> resourcesFromNodeTypes(CDOView view, final List<NodeType> nodeTypes) {

    Iterable<NetXResource> filter = Iterables.filter(resources(view), new Predicate<NetXResource>() {

        public boolean apply(NetXResource input) {
            try {
                final Node nodeFor = nodeFor(input.getComponentRef());
                if (nodeFor != null && nodeFor.eIsSet(OperatorsPackage.Literals.NODE__NODE_TYPE)) {

                    if (Iterables.any(nodeTypes, new Predicate<NodeType>() {

                        public boolean apply(NodeType input) {
                            return nodeFor.getNodeType().getName().equals(input.getName());

                        }
                    })) {
                        return true;
                    }
                }

            } catch (ObjectNotFoundException onfe) {
                System.out.println("error processing: " + onfe.getID());
            }
            return false;
        }
    });

    return Lists.newArrayList(filter);
}

From source file:org.eclipse.xtend.core.validation.XtendValidator.java

@Check
public void checkDispatchFunctions(XtendClass clazz) {
    JvmGenericType type = associations.getInferredType(clazz);
    if (type != null) {
        Multimap<DispatchHelper.DispatchSignature, JvmOperation> dispatchMethods = dispatchHelper
                .getDeclaredOrEnhancedDispatchMethods(type);
        checkDispatchNonDispatchConflict(clazz, dispatchMethods);
        for (DispatchHelper.DispatchSignature signature : dispatchMethods.keySet()) {
            Collection<JvmOperation> dispatchOperations = dispatchMethods.get(signature);
            JvmOperation syntheticDispatchMethod = dispatchHelper.getDispatcherOperation(type, signature);
            if (syntheticDispatchMethod != null) {
                JvmOperation overriddenOperation = overrideHelper
                        .findOverriddenOperation(syntheticDispatchMethod);
                Boolean expectStatic = null;
                if (overriddenOperation != null) {
                    if (isMorePrivateThan(syntheticDispatchMethod.getVisibility(),
                            overriddenOperation.getVisibility())) {
                        String msg = "Synthetic dispatch method reduces visibility of overridden method "
                                + overriddenOperation.getIdentifier();
                        addDispatchError(type, dispatchOperations, msg, null, OVERRIDE_REDUCES_VISIBILITY);
                    }//ww w  .  ja  va  2s  .co m
                    expectStatic = overriddenOperation.isStatic();
                }
                LightweightTypeReference dispatchMethodReturnType = getActualType(clazz,
                        syntheticDispatchMethod);
                if (dispatchOperations.size() == 1) {
                    JvmOperation singleOp = dispatchOperations.iterator().next();
                    XtendFunction function = associations.getXtendFunction(singleOp);
                    addIssue("Single dispatch method.", function, XTEND_MEMBER__MODIFIERS,
                            function.getModifiers().indexOf("dispatch"), SINGLE_DISPATCH_FUNCTION);
                } else {
                    Multimap<List<JvmType>, JvmOperation> signatures = HashMultimap.create();
                    boolean[] allPrimitive = new boolean[signature.getArity()];
                    Arrays.fill(allPrimitive, true);
                    boolean isFirstLocalOperation = true;
                    JvmVisibility commonVisibility = null;
                    Boolean commonStatic = null;
                    for (JvmOperation jvmOperation : dispatchOperations) {
                        signatures.put(getParamTypes(jvmOperation, true), jvmOperation);
                        for (int i = 0; i < jvmOperation.getParameters().size(); i++) {
                            JvmFormalParameter parameter = jvmOperation.getParameters().get(i);
                            if (!(parameter.getParameterType().getType() instanceof JvmPrimitiveType)) {
                                allPrimitive[i] = false;
                            }
                        }
                        if (jvmOperation.getDeclaringType() == type) {
                            if (expectStatic != null) {
                                if (expectStatic && !jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must be static because the dispatch methods in the superclass are static.";
                                    addDispatchError(jvmOperation, msg, "static",
                                            DISPATCH_FUNCTIONS_STATIC_EXPECTED);
                                }
                                if (!expectStatic && jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must not be static because the dispatch methods in the superclass are not static.";
                                    addDispatchError(jvmOperation, msg, "static",
                                            DISPATCH_FUNCTIONS_NON_STATIC_EXPECTED);
                                }
                            }
                            if (isFirstLocalOperation) {
                                commonVisibility = jvmOperation.getVisibility();
                                commonStatic = jvmOperation.isStatic();
                                isFirstLocalOperation = false;
                            } else {
                                if (jvmOperation.getVisibility() != commonVisibility) {
                                    commonVisibility = null;
                                }
                                if (commonStatic != null && commonStatic != jvmOperation.isStatic()) {
                                    commonStatic = null;
                                }
                            }
                            // TODO move validation to type computation
                            if (dispatchMethodReturnType != null) {
                                XtendFunction function = associations.getXtendFunction(jvmOperation);
                                if (function != null) {
                                    LightweightTypeReference operationType = getActualType(
                                            function.getExpression(), jvmOperation);
                                    if (!dispatchMethodReturnType.isAssignableFrom(operationType)) {
                                        error("Incompatible return type of dispatch method. Expected "
                                                + dispatchMethodReturnType.getHumanReadableName() + " but was "
                                                + operationType.getHumanReadableName(), function,
                                                XtendPackage.Literals.XTEND_FUNCTION__RETURN_TYPE,
                                                ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
                                                INCOMPATIBLE_RETURN_TYPE);
                                    }
                                }
                            }
                        }
                    }
                    if (commonVisibility == null) {
                        addDispatchError(type, dispatchOperations,
                                "All local dispatch methods must have the same visibility.", null,
                                DISPATCH_FUNCTIONS_WITH_DIFFERENT_VISIBILITY);
                    }
                    if (expectStatic == null && commonStatic == null) {
                        addDispatchError(type, dispatchOperations,
                                "Static and non-static dispatch methods can not be mixed.", "static",
                                DISPATCH_FUNCTIONS_MIXED_STATIC_AND_NON_STATIC);
                    }
                    for (final List<JvmType> paramTypes : signatures.keySet()) {
                        Collection<JvmOperation> ops = signatures.get(paramTypes);
                        if (ops.size() > 1) {
                            if (Iterables.any(ops, new Predicate<JvmOperation>() {
                                @Override
                                public boolean apply(JvmOperation input) {
                                    return !getParamTypes(input, false).equals(paramTypes);
                                }
                            })) {
                                for (JvmOperation jvmOperation : ops) {
                                    XtendFunction function = associations.getXtendFunction(jvmOperation);
                                    error("Duplicate dispatch methods. Primitives cannot overload their wrapper types in dispatch methods.",
                                            function, null, DUPLICATE_METHOD);
                                }
                            }
                        }
                    }
                    for (int i = 0; i < allPrimitive.length; i++) {
                        if (allPrimitive[i]) {
                            Iterator<JvmOperation> operationIter = dispatchOperations.iterator();
                            JvmType paramType1 = operationIter.next().getParameters().get(i).getParameterType()
                                    .getType();
                            while (operationIter.hasNext()) {
                                JvmType paramType2 = operationIter.next().getParameters().get(i)
                                        .getParameterType().getType();
                                if (!paramType2.equals(paramType1)) {
                                    for (JvmOperation jvmOperation : dispatchOperations) {
                                        XtendFunction function = associations.getXtendFunction(jvmOperation);
                                        addIssue(
                                                "Dispatch methods have arguments with different primitive types.",
                                                function, XTEND_EXECUTABLE__PARAMETERS, i,
                                                DISPATCH_FUNCTIONS_DIFFERENT_PRIMITIVE_ARGS);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.netxforge.netxstudio.common.model.StudioUtils.java

/**
 * Gets the {@link Component components } with the feature
 * {@link LibraryPackage.Literals#COMPONENT__METRIC_REFS } referencing the
 * provided {@link Metric} which can occur in the provided non filtered
 * collection./*from ww  w  .ja v  a  2 s  .  c  o  m*/
 * 
 * @return
 */
public static Iterable<Component> componentsForMetric(List<Component> unfiltered, final Metric metric) {

    Iterable<Component> filter = Iterables.filter(unfiltered, new Predicate<Component>() {

        public boolean apply(Component c) {
            // return c.getMetricRefs().contains(metric);
            // Metric in path doesn't work.
            final List<Metric> metricsInPath = Lists.newArrayList();
            metricsInPath(metricsInPath, c);
            return Iterables.any(metricsInPath, new CDO.CDOObjectEqualsPredicate(metric));
        }

    });

    // We still have many results, look for a component in the left.
    if (Iterables.size(filter) > 1) {
        {
            Iterable<Component> narrowFilter = Iterables.filter(unfiltered, new Predicate<Component>() {

                public boolean apply(Component c) {
                    return c.getMetricRefs().contains(metric);
                }
            });
            int resultSize = Iterables.size(narrowFilter);
            if (resultSize == 1) {
                return narrowFilter;
            }
        }
    }

    return filter;
}