Example usage for com.google.common.base Predicates instanceOf

List of usage examples for com.google.common.base Predicates instanceOf

Introduction

In this page you can find the example usage for com.google.common.base Predicates instanceOf.

Prototype

@GwtIncompatible("Class.isInstance")
public static Predicate<Object> instanceOf(Class<?> clazz) 

Source Link

Document

Returns a predicate that evaluates to true if the object being tested is an instance of the given class.

Usage

From source file:com.tinspx.util.example.Collect.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void selector() {
    Function<Object, String> search = CollectUtils.selector()
            //select value mapped to "a" in a Map or Multimap
            .key("a")
            //select the first value that is a Multimap
            .value(Predicates.instanceOf(Multimap.class))
            //select value mapped to "b" in a Map or Multimap
            .key("b")
            //select the value at index 1 (the second element)
            .value(1)//w  w  w. ja  v a  2 s. c  o  m
            //build the Function expecting a String result
            .get(String.class);

    //create the object to traverse, this can contain any hierarchy
    //of Collection, List, Set, Multiset, Map, Multimap, Table
    Map map = MapUtils.newMutableSingletonMap("a",
            Arrays.asList(null, ImmutableListMultimap.of("b", "b-1", "b", "b-2")));

    String result = search.apply(map);
    System.out.println("map: " + map);
    System.out.println("result: " + result);
    //prints
    //map: {a=[null, {b=[b-1, b-2]}]}
    //result: b-2

    //now slightly modify the map so the search will fail
    map.put("a", Arrays.asList(null, ImmutableListMultimap.of("b", "b-1",
            //second value mapped to "b" is an Integer instead
            //of a String so the search will fail
            "b", 1)));

    result = search.apply(map);
    System.out.println("map: " + map);
    System.out.println("result: " + result);
    //prints
    //{a=[null, {b=[b-1, 1]}]}
    //null
}

From source file:org.opendaylight.protocol.pcep.auto.bandwidth.extension.PcRptMessageCodec.java

@Override
protected Reports getValidReports(final List<Object> objects, final List<Message> errors) {
    final Optional<Object> find = Iterables.tryFind(objects, Predicates.instanceOf(BandwidthUsage.class));
    final Object object;
    if (find.isPresent()) {
        object = find.get();/*from w w w. j a  v  a 2s  .com*/
        objects.remove(object);
    } else {
        object = null;
    }
    final Reports validReports = super.getValidReports(objects, errors);
    if (object != null && validReports != null) {
        final Path path = validReports.getPath();
        if (path != null) {
            return new ReportsBuilder(validReports).setPath(new PathBuilder(path)
                    .setBandwidth(setBandwidthUsage(path.getBandwidth(), (BandwidthUsage) object)).build())
                    .build();
        }
    }
    return validReports;
}

From source file:org.jclouds.abiquo.binders.cloud.BindNetworkConfigurationRefToPayload.java

@Override
public <R extends HttpRequest> R bindToRequest(final R request, final Object input) {
    checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest,
            "this binder is only valid for GeneratedHttpRequests");
    checkArgument(checkNotNull(input, "input") instanceof VLANNetworkDto,
            "this binder is only valid for VLANNetworkDto");
    GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request;
    checkState(gRequest.getInvocation().getArgs() != null, "args should be initialized at this point");

    VLANNetworkDto network = (VLANNetworkDto) input;
    VirtualMachineDto vm = (VirtualMachineDto) Iterables.find(gRequest.getInvocation().getArgs(),
            Predicates.instanceOf(VirtualMachineDto.class));

    RESTLink configLink = checkNotNull(vm.searchLink("configurations"), "missing required link");

    LinksDto dto = new LinksDto();
    dto.addLink(new RESTLink("network_configuration", configLink.getHref() + "/" + network.getId()));

    return super.bindToRequest(request, dto);
}

From source file:org.eclipse.buildship.ui.util.workbench.WorkingSetUtils.java

/**
 * Returns the selected {@link org.eclipse.ui.IWorkingSet} instances or an empty List, if the
 * selection does not contain any {@link org.eclipse.ui.IWorkingSet}.
 *
 * @param selection the selection//www. j av a2 s .co m
 * @return the selected working sets
 */
public static List<IWorkingSet> getSelectedWorkingSets(IStructuredSelection selection) {
    if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
        return ImmutableList.of();
    }

    List<?> elements = selection.toList();
    @SuppressWarnings("unchecked")
    List<IWorkingSet> workingSets = (List<IWorkingSet>) FluentIterable.from(elements)
            .filter(Predicates.instanceOf(IWorkingSet.class)).toList();
    return workingSets;
}

From source file:org.nuxeo.runtime.test.runner.AnnotationScanner.java

@SuppressWarnings("unchecked")
public <T extends Annotation> List<T> getAnnotations(Class<?> clazz, Class<T> annotationType) {
    if (!visitedClasses.contains(clazz)) {
        scan(clazz);//from  ww  w . ja v a 2  s .c  om
    }
    return (List<T>) ImmutableList
            .copyOf(Iterables.filter(classes.get(clazz), Predicates.instanceOf(annotationType)));
}

From source file:org.eclipse.sirius.diagram.sequence.ui.tool.internal.edit.validator.CreateMessageCreationValidator.java

/**
 * Check that there is not {@link ISequenceEvent} on the target lifeline
 * with range's lowerbound lower than the firstClickLocation.y .
 * /*from www  . j av  a 2s.  com*/
 * @return true if not {@link ISequenceEvent} lower than
 *         firstClickLocation.y
 */
private boolean checkNotEventAtLowerTimeInSameLifeline() {
    boolean valid = true;

    SequenceDiagram sequenceDiagram = sequenceElementSource.getDiagram();
    SequenceDiagramQuery sequenceDiagramQuery = new SequenceDiagramQuery(sequenceDiagram);
    for (ISequenceEvent sequenceEvent : Iterables.filter(
            sequenceDiagramQuery.getAllSequenceEventsLowerThan(firstClickLocation.y),
            Predicates.not(Predicates.instanceOf(Lifeline.class)))) {
        if (isSequenceEventOfLifeline(sequenceEvent, sequenceElementTarget.getLifeline())
                || isMessageTargeting(sequenceEvent, sequenceElementTarget.getLifeline()) || isCreateMessageFor(
                        sequenceEvent, sequenceElementTarget.getLifeline().get().getInstanceRole())) {
            valid = false;
            break;
        }
    }
    return valid;
}

From source file:org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp.java

@Override
protected TestApplication rebind(RebindOptions options) throws Exception {
    if (options.applicationChooserOnRebind == null) {
        // Some sub-classes will have added additional apps before rebind. We must return an
        // app of type "TestApplication"; otherwise we'll get a class-cast exception.
        options = RebindOptions.create(options);
        options.applicationChooserOnRebind(new Function<Collection<Application>, Application>() {
            @Override/*from  w w  w  .ja  va2s.c o  m*/
            public Application apply(Collection<Application> input) {
                return Iterables.find(input, Predicates.instanceOf(TestApplication.class));
            }
        });
    }
    return super.rebind(options);
}

From source file:org.eclipse.papyrus.uml.diagram.activity.activitygroup.ui.IntegrateViewToConfigureComposite.java

public Iterable<IGroupNotifier> getSelectedNotifier() {
    Object[] selection = checkboxTableViewer.getCheckedElements();
    Iterable<Object> groupNotifiers = Iterables.filter(Lists.newArrayList(selection),
            Predicates.instanceOf(IGroupNotifier.class));
    return Iterables.transform(groupNotifiers, new Function<Object, IGroupNotifier>() {

        public IGroupNotifier apply(Object arg0) {
            return (IGroupNotifier) arg0;
        }//from   ww  w .  ja v  a  2s.  c  om
    });
}

From source file:edu.mit.streamjit.util.bytecode.insts.PhiInst.java

public FluentIterable<Value> incomingValues() {
    return operands().filter(Predicates.not(Predicates.instanceOf(BasicBlock.class)));
}

From source file:org.eclipse.sirius.common.xtext.internal.XtextSessionManagerListener.java

protected boolean containsXtextResources(TransactionalEditingDomain ted) {
    return ted.getResourceSet() != null && Iterators.any(ted.getResourceSet().getResources().iterator(),
            Predicates.instanceOf(XtextResource.class));
}