Search in sources :

Example 1 with Specification

use of org.qi4j.functional.Specification in project qi4j-sdk by Qi4j.

the class OperatorsTest method testOperators.

@Test
public void testOperators() throws UnitOfWorkCompletionException, ActivationException, AssemblyException {
    SingletonAssembler assembler = new SingletonAssembler() {

        @Override
        public void assemble(ModuleAssembly module) throws AssemblyException {
            new EntityTestAssembler().assemble(module);
            module.entities(TestEntity.class);
            module.values(TestValue.class);
            module.forMixin(TestEntity.class).declareDefaults().foo().set("Bar");
            module.forMixin(TestValue.class).declareDefaults().bar().set("Xyz");
        }
    };
    UnitOfWork uow = assembler.module().newUnitOfWork();
    try {
        EntityBuilder<TestEntity> entityBuilder = uow.newEntityBuilder(TestEntity.class, "123");
        entityBuilder.instance().value().set(assembler.module().newValue(TestValue.class));
        TestEntity testEntity = entityBuilder.newInstance();
        uow.complete();
        uow = assembler.module().newUnitOfWork();
        Iterable<TestEntity> entities = Iterables.iterable(testEntity = uow.get(testEntity));
        QueryBuilder<TestEntity> builder = assembler.module().newQueryBuilder(TestEntity.class);
        {
            Specification<Composite> where = QueryExpressions.eq(QueryExpressions.templateFor(TestEntity.class).foo(), "Bar");
            Assert.assertTrue(where.satisfiedBy(testEntity));
            System.out.println(where);
        }
        {
            Specification<Composite> where = QueryExpressions.eq(QueryExpressions.templateFor(TestEntity.class).value().get().bar(), "Xyz");
            Assert.assertTrue(where.satisfiedBy(testEntity));
            System.out.println(where);
            Assert.assertTrue(builder.where(where).newQuery(entities).find().equals(testEntity));
        }
    } finally {
        uow.discard();
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Specification(org.qi4j.functional.Specification) Test(org.junit.Test)

Example 2 with Specification

use of org.qi4j.functional.Specification in project qi4j-sdk by Qi4j.

the class RdfQueryParserImpl method processFilter.

private void processFilter(final Specification<Composite> expression, boolean allowInline, StringBuilder builder) {
    if (expression == null) {
        return;
    }
    if (expression instanceof AndSpecification) {
        final AndSpecification conjunction = (AndSpecification) expression;
        int start = builder.length();
        boolean first = true;
        for (Specification<Composite> operand : conjunction.operands()) {
            int size = builder.length();
            processFilter(operand, allowInline, builder);
            if (builder.length() > size) {
                if (first) {
                    first = false;
                } else {
                    builder.insert(size, " && ");
                }
            }
        }
        if (builder.length() > start) {
            builder.insert(start, '(');
            builder.append(')');
        }
    } else if (expression instanceof OrSpecification) {
        final OrSpecification disjunction = (OrSpecification) expression;
        int start = builder.length();
        boolean first = true;
        for (Specification<Composite> operand : disjunction.operands()) {
            int size = builder.length();
            processFilter(operand, false, builder);
            if (builder.length() > size) {
                if (first) {
                    first = false;
                } else {
                    builder.insert(size, "||");
                }
            }
        }
        if (builder.length() > start) {
            builder.insert(start, '(');
            builder.append(')');
        }
    } else if (expression instanceof NotSpecification) {
        builder.insert(0, "(!");
        processFilter(((NotSpecification) expression).operand(), false, builder);
        builder.append(")");
    } else if (expression instanceof ComparisonSpecification) {
        processComparisonPredicate(expression, allowInline, builder);
    } else if (expression instanceof ContainsAllSpecification) {
        processContainsAllPredicate((ContainsAllSpecification) expression, builder);
    } else if (expression instanceof ContainsSpecification<?>) {
        processContainsPredicate((ContainsSpecification<?>) expression, builder);
    } else if (expression instanceof MatchesSpecification) {
        processMatchesPredicate((MatchesSpecification) expression, builder);
    } else if (expression instanceof PropertyNotNullSpecification<?>) {
        processNotNullPredicate((PropertyNotNullSpecification) expression, builder);
    } else if (expression instanceof PropertyNullSpecification<?>) {
        processNullPredicate((PropertyNullSpecification) expression, builder);
    } else if (expression instanceof AssociationNotNullSpecification<?>) {
        processNotNullPredicate((AssociationNotNullSpecification) expression, builder);
    } else if (expression instanceof AssociationNullSpecification<?>) {
        processNullPredicate((AssociationNullSpecification) expression, builder);
    } else if (expression instanceof ManyAssociationContainsSpecification<?>) {
        processManyAssociationContainsPredicate((ManyAssociationContainsSpecification) expression, allowInline, builder);
    } else {
        throw new UnsupportedOperationException("Expression " + expression + " is not supported");
    }
}
Also used : OrSpecification(org.qi4j.api.query.grammar.OrSpecification) Composite(org.qi4j.api.composite.Composite) EntityComposite(org.qi4j.api.entity.EntityComposite) MatchesSpecification(org.qi4j.api.query.grammar.MatchesSpecification) ContainsSpecification(org.qi4j.api.query.grammar.ContainsSpecification) ContainsAllSpecification(org.qi4j.api.query.grammar.ContainsAllSpecification) ManyAssociationContainsSpecification(org.qi4j.api.query.grammar.ManyAssociationContainsSpecification) LeSpecification(org.qi4j.api.query.grammar.LeSpecification) PropertyNullSpecification(org.qi4j.api.query.grammar.PropertyNullSpecification) NotSpecification(org.qi4j.api.query.grammar.NotSpecification) PropertyNotNullSpecification(org.qi4j.api.query.grammar.PropertyNotNullSpecification) Specification(org.qi4j.functional.Specification) LtSpecification(org.qi4j.api.query.grammar.LtSpecification) GtSpecification(org.qi4j.api.query.grammar.GtSpecification) NeSpecification(org.qi4j.api.query.grammar.NeSpecification) EqSpecification(org.qi4j.api.query.grammar.EqSpecification) AssociationNotNullSpecification(org.qi4j.api.query.grammar.AssociationNotNullSpecification) ComparisonSpecification(org.qi4j.api.query.grammar.ComparisonSpecification) QuerySpecification(org.qi4j.api.query.grammar.QuerySpecification) OrSpecification(org.qi4j.api.query.grammar.OrSpecification) GeSpecification(org.qi4j.api.query.grammar.GeSpecification) AndSpecification(org.qi4j.api.query.grammar.AndSpecification) AssociationNullSpecification(org.qi4j.api.query.grammar.AssociationNullSpecification) ComparisonSpecification(org.qi4j.api.query.grammar.ComparisonSpecification) ContainsSpecification(org.qi4j.api.query.grammar.ContainsSpecification) ManyAssociationContainsSpecification(org.qi4j.api.query.grammar.ManyAssociationContainsSpecification) NotSpecification(org.qi4j.api.query.grammar.NotSpecification) AndSpecification(org.qi4j.api.query.grammar.AndSpecification) PropertyNullSpecification(org.qi4j.api.query.grammar.PropertyNullSpecification) MatchesSpecification(org.qi4j.api.query.grammar.MatchesSpecification) AssociationNotNullSpecification(org.qi4j.api.query.grammar.AssociationNotNullSpecification) ContainsAllSpecification(org.qi4j.api.query.grammar.ContainsAllSpecification) PropertyNotNullSpecification(org.qi4j.api.query.grammar.PropertyNotNullSpecification) ManyAssociationContainsSpecification(org.qi4j.api.query.grammar.ManyAssociationContainsSpecification) AssociationNullSpecification(org.qi4j.api.query.grammar.AssociationNullSpecification)

Example 3 with Specification

use of org.qi4j.functional.Specification in project qi4j-sdk by Qi4j.

the class ServiceSelectorImporter method importService.

@Override
public T importService(ImportedServiceDescriptor serviceDescriptor) throws ServiceImporterException {
    Specification<ServiceReference<?>> selector = serviceDescriptor.metaInfo(Specification.class);
    Class serviceType = Iterables.first(serviceDescriptor.types());
    Iterable<ServiceReference<T>> services = locator.findServices(serviceType);
    List<ServiceReference<T>> filteredServices = new ArrayList<ServiceReference<T>>();
    for (ServiceReference<T> service : services) {
        Specification selector1 = service.metaInfo(Specification.class);
        if (selector1 != null && selector1 == selector) {
            continue;
        }
        filteredServices.add(service);
    }
    T service = ServiceQualifier.firstService(selector, filteredServices);
    if (service == null) {
        throw new ServiceImporterException("Could not find any service to import that matches the given specification for " + serviceDescriptor.identity());
    }
    return service;
}
Also used : ArrayList(java.util.ArrayList) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) Specification(org.qi4j.functional.Specification) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 4 with Specification

use of org.qi4j.functional.Specification in project qi4j-sdk by Qi4j.

the class ClassScanner method findClasses.

/**
 * Get all classes from the same package of the given class, and recursively in all subpackages.
 * <p/>
 * This only works if the seed class is loaded from a file: URL. Jar files are possible as well. Abstract classes
 * are not included in the results. For further filtering use e.g. Iterables.filter.
 *
 * @param seedClass starting point for classpath scanning
 *
 * @return iterable of all concrete classes in the same package as the seedclass, and also all classes in subpackages.
 */
public static Iterable<Class<?>> findClasses(final Class<?> seedClass) {
    CodeSource codeSource = seedClass.getProtectionDomain().getCodeSource();
    if (codeSource == null) {
        return Iterables.empty();
    }
    URL location = codeSource.getLocation();
    if (!location.getProtocol().equals("file")) {
        throw new IllegalArgumentException("Can only enumerate classes from file system locations. URL is:" + location);
    }
    final File file;
    try {
        file = new File(location.toURI().getPath());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("The file location of codebase is invalid. Can not convert to URI. URL is:" + location);
    }
    if (file.getName().endsWith(".jar")) {
        try {
            final String packageName = seedClass.getPackage().getName().replace('.', '/');
            JarFile jarFile = new JarFile(file);
            Iterable<JarEntry> entries = Iterables.iterable(jarFile.entries());
            try {
                return Iterables.toList(filter(new ValidClass(), map(new Function<JarEntry, Class<?>>() {

                    @Override
                    public Class map(JarEntry jarEntry) {
                        String name = jarEntry.getName();
                        name = name.substring(0, name.length() - 6);
                        name = name.replace('/', '.');
                        try {
                            return seedClass.getClassLoader().loadClass(name);
                        } catch (ClassNotFoundException e) {
                            return null;
                        }
                    }
                }, filter(new Specification<JarEntry>() {

                    @Override
                    public boolean satisfiedBy(JarEntry jarEntry) {
                        return jarEntry.getName().startsWith(packageName) && jarEntry.getName().endsWith(".class");
                    }
                }, entries))));
            } finally {
                jarFile.close();
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Could not open jar file " + file, e);
        }
    } else {
        final File path = new File(file, seedClass.getPackage().getName().replace('.', File.separatorChar));
        Iterable<File> files = findFiles(path, new Specification<File>() {

            @Override
            public boolean satisfiedBy(File file) {
                return file.getName().endsWith(".class");
            }
        });
        return filter(new ValidClass(), map(new Function<File, Class<?>>() {

            @Override
            public Class<?> map(File f) {
                String fileName = f.getAbsolutePath().substring(file.toString().length() + 1);
                fileName = fileName.replace(File.separatorChar, '.').substring(0, fileName.length() - 6);
                try {
                    return seedClass.getClassLoader().loadClass(fileName);
                } catch (ClassNotFoundException e) {
                    return null;
                }
            }
        }, files));
    }
}
Also used : Specification(org.qi4j.functional.Specification) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CodeSource(java.security.CodeSource) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Function(org.qi4j.functional.Function) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 5 with Specification

use of org.qi4j.functional.Specification in project qi4j-sdk by Qi4j.

the class RdfQueryParserImpl2 method processFilter.

private void processFilter(final Specification<Composite> expression, boolean allowInline, StringBuilder builder) {
    if (expression == null) {
        return;
    }
    if (expression instanceof AndSpecification) {
        final AndSpecification conjunction = (AndSpecification) expression;
        int start = builder.length();
        boolean first = true;
        for (Specification<Composite> operand : conjunction.operands()) {
            int size = builder.length();
            processFilter(operand, allowInline, builder);
            if (builder.length() > size) {
                if (first) {
                    first = false;
                } else {
                    builder.insert(size, " && ");
                }
            }
        }
        if (builder.length() > start) {
            builder.insert(start, '(');
            builder.append(')');
        }
    } else if (expression instanceof OrSpecification) {
        final OrSpecification disjunction = (OrSpecification) expression;
        int start = builder.length();
        boolean first = true;
        for (Specification<Composite> operand : disjunction.operands()) {
            int size = builder.length();
            processFilter(operand, false, builder);
            if (builder.length() > size) {
                if (first) {
                    first = false;
                } else {
                    builder.insert(size, "||");
                }
            }
        }
        if (builder.length() > start) {
            builder.insert(start, '(');
            builder.append(')');
        }
    } else if (expression instanceof NotSpecification) {
        builder.insert(0, "(!");
        processFilter(((NotSpecification) expression).operand(), false, builder);
        builder.append(")");
    } else if (expression instanceof ComparisonSpecification) {
        processComparisonPredicate(expression, allowInline, builder);
    } else if (expression instanceof ContainsAllSpecification) {
        processContainsAllPredicate((ContainsAllSpecification) expression, builder);
    } else if (expression instanceof ContainsSpecification<?>) {
        processContainsPredicate((ContainsSpecification<?>) expression, builder);
    } else if (expression instanceof MatchesSpecification) {
        processMatchesPredicate((MatchesSpecification) expression, builder);
    } else if (expression instanceof PropertyNotNullSpecification<?>) {
        processNotNullPredicate((PropertyNotNullSpecification) expression, builder);
    } else if (expression instanceof PropertyNullSpecification<?>) {
        processNullPredicate((PropertyNullSpecification) expression, builder);
    } else if (expression instanceof AssociationNotNullSpecification<?>) {
        processNotNullPredicate((AssociationNotNullSpecification) expression, builder);
    } else if (expression instanceof AssociationNullSpecification<?>) {
        processNullPredicate((AssociationNullSpecification) expression, builder);
    } else if (expression instanceof ManyAssociationContainsSpecification<?>) {
        processManyAssociationContainsPredicate((ManyAssociationContainsSpecification) expression, allowInline, builder);
    } else {
        throw new UnsupportedOperationException("Expression " + expression + " is not supported");
    }
}
Also used : OrSpecification(org.qi4j.api.query.grammar.OrSpecification) Composite(org.qi4j.api.composite.Composite) EntityComposite(org.qi4j.api.entity.EntityComposite) MatchesSpecification(org.qi4j.api.query.grammar.MatchesSpecification) ContainsSpecification(org.qi4j.api.query.grammar.ContainsSpecification) ContainsAllSpecification(org.qi4j.api.query.grammar.ContainsAllSpecification) ManyAssociationContainsSpecification(org.qi4j.api.query.grammar.ManyAssociationContainsSpecification) LeSpecification(org.qi4j.api.query.grammar.LeSpecification) PropertyNullSpecification(org.qi4j.api.query.grammar.PropertyNullSpecification) NotSpecification(org.qi4j.api.query.grammar.NotSpecification) PropertyNotNullSpecification(org.qi4j.api.query.grammar.PropertyNotNullSpecification) Specification(org.qi4j.functional.Specification) LtSpecification(org.qi4j.api.query.grammar.LtSpecification) GtSpecification(org.qi4j.api.query.grammar.GtSpecification) NeSpecification(org.qi4j.api.query.grammar.NeSpecification) EqSpecification(org.qi4j.api.query.grammar.EqSpecification) AssociationNotNullSpecification(org.qi4j.api.query.grammar.AssociationNotNullSpecification) ComparisonSpecification(org.qi4j.api.query.grammar.ComparisonSpecification) QuerySpecification(org.qi4j.api.query.grammar.QuerySpecification) OrSpecification(org.qi4j.api.query.grammar.OrSpecification) GeSpecification(org.qi4j.api.query.grammar.GeSpecification) AndSpecification(org.qi4j.api.query.grammar.AndSpecification) AssociationNullSpecification(org.qi4j.api.query.grammar.AssociationNullSpecification) ComparisonSpecification(org.qi4j.api.query.grammar.ComparisonSpecification) ContainsSpecification(org.qi4j.api.query.grammar.ContainsSpecification) ManyAssociationContainsSpecification(org.qi4j.api.query.grammar.ManyAssociationContainsSpecification) NotSpecification(org.qi4j.api.query.grammar.NotSpecification) AndSpecification(org.qi4j.api.query.grammar.AndSpecification) PropertyNullSpecification(org.qi4j.api.query.grammar.PropertyNullSpecification) MatchesSpecification(org.qi4j.api.query.grammar.MatchesSpecification) AssociationNotNullSpecification(org.qi4j.api.query.grammar.AssociationNotNullSpecification) ContainsAllSpecification(org.qi4j.api.query.grammar.ContainsAllSpecification) PropertyNotNullSpecification(org.qi4j.api.query.grammar.PropertyNotNullSpecification) ManyAssociationContainsSpecification(org.qi4j.api.query.grammar.ManyAssociationContainsSpecification) AssociationNullSpecification(org.qi4j.api.query.grammar.AssociationNullSpecification)

Aggregations

Specification (org.qi4j.functional.Specification)5 Composite (org.qi4j.api.composite.Composite)2 EntityComposite (org.qi4j.api.entity.EntityComposite)2 AndSpecification (org.qi4j.api.query.grammar.AndSpecification)2 AssociationNotNullSpecification (org.qi4j.api.query.grammar.AssociationNotNullSpecification)2 AssociationNullSpecification (org.qi4j.api.query.grammar.AssociationNullSpecification)2 ComparisonSpecification (org.qi4j.api.query.grammar.ComparisonSpecification)2 ContainsAllSpecification (org.qi4j.api.query.grammar.ContainsAllSpecification)2 ContainsSpecification (org.qi4j.api.query.grammar.ContainsSpecification)2 EqSpecification (org.qi4j.api.query.grammar.EqSpecification)2 GeSpecification (org.qi4j.api.query.grammar.GeSpecification)2 GtSpecification (org.qi4j.api.query.grammar.GtSpecification)2 LeSpecification (org.qi4j.api.query.grammar.LeSpecification)2 LtSpecification (org.qi4j.api.query.grammar.LtSpecification)2 ManyAssociationContainsSpecification (org.qi4j.api.query.grammar.ManyAssociationContainsSpecification)2 MatchesSpecification (org.qi4j.api.query.grammar.MatchesSpecification)2 NeSpecification (org.qi4j.api.query.grammar.NeSpecification)2 NotSpecification (org.qi4j.api.query.grammar.NotSpecification)2 OrSpecification (org.qi4j.api.query.grammar.OrSpecification)2 PropertyNotNullSpecification (org.qi4j.api.query.grammar.PropertyNotNullSpecification)2