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.mycila.ujd.UjdSample.java

public static void main(String... args) throws Exception {
    URLClassLoader cl = new URLClassLoader(new URL[] {
            //new URL(new URL("http://repo2.maven.org/maven2/com/mycila/mycila-log/2.9/mycila-log-2.9.jar"), "jar:http://repo2.maven.org/maven2/com/mycila/mycila-log/2.9/mycila-log-2.9.jar!/"),
            //new URL("jar:http://repo2.maven.org/maven2/com/mycila/mycila-log/2.9/mycila-log-2.9.jar!/"),
            new URL("http://repo2.maven.org/maven2/log4j/log4j/1.2.15/log4j-1.2.15.jar"),
            new File("target/classes/").toURI().toURL(), new File("target/test-classes/").toURI().toURL() },
            ClassLoader.getSystemClassLoader().getParent());

    Class<?> remote1 = cl.loadClass("org.apache.log4j.Level");
    Class<?> remote2 = cl.loadClass("com.mycila.ujd.api.Loader");

    JVM jvm = new DefaultJVM();
    jvm.addClasses(remote1, remote2);/* ww w  . ja v  a  2 s.c  om*/
    for (JavaClass<?> loadedClass : jvm.getClasses()) {
        System.out.println(loadedClass);
        for (Container container : loadedClass.getLoader().getContainers())
            for (ContainedClass aClass : container.getClasses())
                System.out.println(aClass);
    }

    jvm.addClasses(UjdSample.class, Loader.class, Predicates.class);
    for (ContainedJavaClass<?> loadedClass : jvm
            .<ContainedJavaClass<?>>getClasses(Predicates.instanceOf(ContainedJavaClass.class))) {
        System.out.println(loadedClass);
        System.out.println(loadedClass.getLoader());
        System.out.println(loadedClass.getContainer());
        for (ContainedClass containedClass : loadedClass.getContainer().getClasses())
            System.out.println(containedClass);
    }
}

From source file:vazkii.botania.api.mana.spark.SparkHelper.java

public static <T> List<T> getEntitiesAround(Class<? extends T> clazz, World world, double x, double y,
        double z) {
    int r = SPARK_SCAN_RANGE;
    List entities = world.getEntitiesWithinAABB(Entity.class,
            new AxisAlignedBB(x - r, y - r, z - r, x + r, y + r, z + r), Predicates.instanceOf(clazz));
    return entities;
}

From source file:org.eclipse.sirius.tests.unit.contribution.Freezer.java

public static void freeze(EObject obj) {
    if (!Iterables.any(obj.eAdapters(), Predicates.instanceOf(Freezer.class))) {
        obj.eAdapters().add(new Freezer());
    }/*from w  ww.j  a v  a  2 s  .c  o  m*/
}

From source file:com.facebook.buck.rules.DependencyAggregationTestUtil.java

/**
 * Return dependencies of a rule, traversing through any dependency aggregations.
 */// w  ww  . j  ava  2 s  .c om
public static Iterable<BuildRule> getDisaggregatedDeps(BuildRule rule) {
    return FluentIterable.from(rule.getDeps()).filter(DependencyAggregation.class)
            .transformAndConcat(new Function<DependencyAggregation, Iterable<BuildRule>>() {
                @Override
                public Iterable<BuildRule> apply(DependencyAggregation input) {
                    return input.getDeps();
                }
            }).append(Iterables.filter(rule.getDeps(),
                    Predicates.not(Predicates.instanceOf(DependencyAggregation.class))));
}

From source file:org.fishwife.jrugged.spring.retry.ExtendedPredicates.java

/***
 * Create a predicate to see if a given object is an instance of a specific class, given that
 * all objects passed to this predicate will be a subclass of some parent of that class.
 *
 * @param superclazz The class which all objects passed to this predicate will be a subclass of
 * @param clazz The class to see if the passed in object will be an instanceof
 * @param <S> The superclass/*  w w  w  .  j  a va 2 s.c  o  m*/
 * @param <C> The class
 * @return
 */
public static <S, C extends S> Predicate<S> isInstanceOf(final Class<S> superclazz, final Class<C> clazz) {
    return new Predicate<S>() {
        public boolean apply(S input) {
            return Predicates.instanceOf(clazz).apply(input);
        }
    };
}

From source file:org.eclipse.sirius.tests.unit.contribution.Freezer.java

public static void thaw(EObject obj) {
    try {//from w w  w . j ava 2s.  c  om
        Adapter f = Iterables.find(obj.eAdapters(), Predicates.instanceOf(Freezer.class));
        obj.eAdapters().remove(f);
    } catch (NoSuchElementException nsee) {
        // Ignore.
    }
}

From source file:io.usethesource.criterion.FootprintUtils.java

public static String measureAndReport(final Object objectToMeasure, final String className, DataType dataType,
        Archetype archetype, boolean supportsStagedMutability, int size, int run,
        MemoryFootprintPreset preset) {//from   w ww. ja  va2  s  . c  o  m
    final Predicate<Object> predicate;

    switch (preset) {
    case DATA_STRUCTURE_OVERHEAD:
        predicate = Predicates.not(Predicates.instanceOf(JmhValue.class));
        break;
    case RETAINED_SIZE:
        predicate = Predicates.alwaysTrue();
        break;
    default:
        throw new IllegalStateException();
    }

    // System.out.println(GraphLayout.parseInstance(objectToMeasure).totalSize());

    long memoryInBytes = objectexplorer.MemoryMeasurer.measureBytes(objectToMeasure, predicate);

    Footprint memoryFootprint = objectexplorer.ObjectGraphMeasurer.measure(objectToMeasure, predicate);

    final String statString = String.format("%d\t %60s\t[%s]\t %s", memoryInBytes, className, dataType,
            memoryFootprint);
    System.out.println(statString);

    final String statFileString = String.format("%d,%d,%s,%s,%s,%b,%d,%d,%d", size, run, className, dataType,
            archetype, supportsStagedMutability, memoryInBytes, memoryFootprint.getObjects(),
            memoryFootprint.getReferences());

    return statFileString;
}

From source file:nl.cwi.swat.jmh_dscg_benchmarks.FootprintUtils.java

public static String measureAndReport(final Object objectToMeasure, final String className, DataType dataType,
        Archetype archetype, boolean supportsStagedMutability, int size, int run,
        MemoryFootprintPreset preset) {//from   w  w w  .  j  a v  a2  s .c  o m
    final Predicate<Object> predicate;

    switch (preset) {
    case DATA_STRUCTURE_OVERHEAD:
        predicate = Predicates.not(Predicates.instanceOf(org.eclipse.imp.pdb.facts.IValue.class));
        break;
    case RETAINED_SIZE:
        predicate = Predicates.alwaysTrue();
        break;
    default:
        throw new IllegalStateException();
    }

    // System.out.println(GraphLayout.parseInstance(objectToMeasure).totalSize());

    long memoryInBytes = objectexplorer.MemoryMeasurer.measureBytes(objectToMeasure, predicate);

    Footprint memoryFootprint = objectexplorer.ObjectGraphMeasurer.measure(objectToMeasure, predicate);

    final String statString = String.format("%d\t %60s\t[%s]\t %s", memoryInBytes, className, dataType,
            memoryFootprint);
    System.out.println(statString);

    final String statFileString = String.format("%d,%d,%s,%s,%s,%b,%d,%d,%d", size, run, className, dataType,
            archetype, supportsStagedMutability, memoryInBytes, memoryFootprint.getObjects(),
            memoryFootprint.getReferences());

    return statFileString;
}

From source file:org.eclipse.sirius.common.tools.api.resource.ResourceMigrationMarker.java

/**
 * Clear the {@link ResourceMigrationMarker} from the resource.
 * //from  w  ww  .  ja  va 2  s. c o m
 * @param res
 *            resource to clear.
 */
public static void clearMigrationMarker(Resource res) {
    Iterables.removeIf(res.eAdapters(), Predicates.instanceOf(ResourceMigrationMarker.class));
}

From source file:vazkii.botania.common.block.tile.mana.TileManaDetector.java

@Override
public void update() {
    boolean state = worldObj.getBlockState(getPos()).getValue(BotaniaStateProps.POWERED);
    boolean expectedState = worldObj.getEntitiesWithinAABB(Entity.class,
            new AxisAlignedBB(pos, pos.add(1, 1, 1)), Predicates.instanceOf(IManaBurst.class)).size() != 0;
    if (state != expectedState && !worldObj.isRemote)
        worldObj.setBlockState(getPos(),
                worldObj.getBlockState(getPos()).withProperty(BotaniaStateProps.POWERED, expectedState), 1 | 2);

    if (expectedState)
        for (int i = 0; i < 4; i++)
            Botania.proxy.sparkleFX(pos.getX() + Math.random(), pos.getY() + Math.random(),
                    pos.getZ() + Math.random(), 1F, 0.2F, 0.2F, 0.7F + 0.5F * (float) Math.random(), 5);
}