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();
}
}
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");
}
}
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;
}
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));
}
}
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");
}
}
Aggregations