com.googlecode.cqengine.attribute.Attribute Java Examples

The following examples show how to use com.googlecode.cqengine.attribute.Attribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testInOne() {
    // Create an indexed collection (note: could alternatively use CQEngine.copyFrom() existing collection)...
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();

    Attribute<Car, String> NAME = new SimpleNullableAttribute<Car, String>("name") {
        public String getValue(Car car, QueryOptions queryOptions) {
            return car.name;
        }
    };
    cars.addIndex(NavigableIndex.onAttribute(NAME));

    // Add some objects to the collection...
    cars.add(new Car(1, "ford", null, null));
    cars.add(new Car(2, "honda", null, null));
    cars.add(new Car(3, "toyota", null, null));

    Assert.assertEquals(cars.retrieve(in(NAME, "ford")).size(), 1);
    Assert.assertEquals(cars.retrieve(in(NAME, Collections.singletonList("ford"))).size(), 1);
}
 
Example #2
Source File: LongestPrefixTest.java    From cqengine with Apache License 2.0 6 votes vote down vote up
@Test
public void testLongestPrefix() {
    Attribute<String, String> stringIdentity = new SelfAttribute<String>(String.class, "identity");
    assertTrue(LongestPrefix.countPrefixChars( "35387123456", "35387") > 0);
    assertEquals(5, LongestPrefix.countPrefixChars( "35387123456", "35387"));

    assertTrue(LongestPrefix.countPrefixChars("35387", "35387") > 0);
    assertEquals(5, LongestPrefix.countPrefixChars("35387", "35387"));

    assertFalse(LongestPrefix.countPrefixChars("35386123456", "35387") > 0);
    assertEquals(0, LongestPrefix.countPrefixChars("35386123456", "35387"));

    assertFalse(LongestPrefix.countPrefixChars("35386123456", "35387") > 0);
    assertEquals(0, LongestPrefix.countPrefixChars("35386123456", "35387"));

    assertFalse(LongestPrefix.countPrefixChars("3538", "35387") > 0);
    assertEquals(0, LongestPrefix.countPrefixChars("3538", "35387"));
}
 
Example #3
Source File: Min.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<O> getMatchesForNonSimpleAttribute(Attribute<O, A> attribute, ObjectSet<O> objectsInCollection, QueryOptions queryOptions) {
    A minimumValue = null;
    Set<O> results = new HashSet<>();
    for (O object : objectsInCollection) {
        Iterable<A> attributeValues = attribute.getValues(object, queryOptions);
        for (A attributeValue : attributeValues) {
            minimumValue = evaluate(object, attributeValue, minimumValue, results);
        }
    }
    return results;
}
 
Example #4
Source File: Max.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<O> getMatchesForNonSimpleAttribute(Attribute<O, A> attribute, ObjectSet<O> objectsInCollection, QueryOptions queryOptions) {
    A maximumValue = null;
    Set<O> results = new HashSet<>();
    for (O object : objectsInCollection) {
        Iterable<A> attributeValues = attribute.getValues(object, queryOptions);
        for (A attributeValue : attributeValues) {
            maximumValue = evaluate(object, attributeValue, maximumValue, results);
        }
    }
    return results;
}
 
Example #5
Source File: RadixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected RadixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(In.class);
        add(StringStartsWith.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentRadixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #6
Source File: SQLAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitGreaterThanOrEqualToQuery(SQLGrammarParser.GreaterThanOrEqualToQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    Comparable value = queryParser.parseValue(attribute, ctx.queryParameter());
    addParsedQuery(ctx, QueryFactory.greaterThanOrEqualTo(attribute, value));
}
 
Example #7
Source File: SQLAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitGreaterThanQuery(SQLGrammarParser.GreaterThanQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    Comparable value = queryParser.parseValue(attribute, ctx.queryParameter());
    addParsedQuery(ctx, QueryFactory.greaterThan(attribute, value));
}
 
Example #8
Source File: SQLAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitNotBetweenQuery(SQLGrammarParser.NotBetweenQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    List<? extends ParseTree> queryParameters = ctx.queryParameter();
    Comparable lowerValue = queryParser.parseValue(attribute, queryParameters.get(0));
    Comparable upperValue = queryParser.parseValue(attribute, queryParameters.get(1));
    addParsedQuery(ctx, QueryFactory.not(QueryFactory.between(attribute, lowerValue, upperValue)));
}
 
Example #9
Source File: SQLAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public void exitStartsWithQuery(SQLGrammarParser.StartsWithQueryContext ctx) {
    Attribute<O, String> attribute = queryParser.getAttribute(ctx.attributeName(), String.class);
    String value = queryParser.parseValue(attribute, ctx.queryParameterTrailingPercent());
    value = value.substring(0, value.length() - 1);
    addParsedQuery(ctx, QueryFactory.startsWith(attribute, value));
}
 
Example #10
Source File: StringIsPrefixOf.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesNonSimpleAttribute(Attribute<O, A> attribute, O object, QueryOptions queryOptions) {
    for (A attributeValue : attribute.getValues(object, queryOptions)) {
        if (StringStartsWith.matchesValue(value, attributeValue, queryOptions)) {
            return true;
        }
    }
    return false;
}
 
Example #11
Source File: SQLAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
public void exitEndsWithQuery(SQLGrammarParser.EndsWithQueryContext ctx) {
    Attribute<O, String> attribute = queryParser.getAttribute(ctx.attributeName(), String.class);
    String value = queryParser.parseValue(attribute, ctx.queryParameterLeadingPercent());
    value = value.substring(1, value.length());
    addParsedQuery(ctx, QueryFactory.endsWith(attribute, value));
}
 
Example #12
Source File: ReversedRadixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected ReversedRadixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(In.class);
        add(StringEndsWith.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentReversedRadixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #13
Source File: TestUtil.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the values of the given attribute in objects returned by the given result set.
 * <p/>
 * The set returned preserves the order of objects returned by the result set, but eliminates duplicates.
 */
@SuppressWarnings({"JavaDoc"})
public static <O, A> Set<A> valuesOf(Attribute<O, A> attribute, ResultSet<O> resultSet) {
    Set<A> attributeValues = new LinkedHashSet<A>();
    for (O object : resultSet) {
        for (A value : attribute.getValues(object, noQueryOptions())) {
            attributeValues.add(value);
        }
    }
    return attributeValues;
}
 
Example #14
Source File: In.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesNonSimpleAttribute(Attribute<O, A> attribute, O object, QueryOptions queryOptions) {
    for (A attributeValue : attribute.getValues(object, queryOptions)) {
        if (values.contains(attributeValue)) {
            return true;
        }
    }
    return false;
}
 
Example #15
Source File: DynamicExample.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    // Generate attributes dynamically for fields in the given POJO...
    Map<String, Attribute<Car, Comparable>> attributes = DynamicIndexer.generateAttributesForPojo(Car.class);
    // Build indexes on the dynamically generated attributes...
    IndexedCollection<Car> cars = DynamicIndexer.newAutoIndexedCollection(attributes.values());

    // Add some objects to the collection...
    cars.add(new Car(1, "ford", "focus", 4, 9000));
    cars.add(new Car(2, "ford", "mondeo", 5, 10000));
    cars.add(new Car(2, "ford", "fiesta", 3, 2000));
    cars.add(new Car(3, "honda", "civic", 5, 11000));

    Query<Car> query = and(
            equal(attributes.get("manufacturer"), "ford"),
            lessThan(attributes.get("doors"), value(5)),
            greaterThan(attributes.get("horsepower"), value(3000))
    );
    ResultSet<Car> results = cars.retrieve(query);

    System.out.println("Ford cars with less than 5 doors and horsepower greater than 3000:- ");
    System.out.println("Using NavigableIndex: " + (results.getRetrievalCost() == 40));
    for (Car car : results) {
        System.out.println(car);
    }

    // Prints:
    //    Ford cars with less than 5 doors and horsepower greater than 3000:-
    //    Using NavigableIndex: true
    //    Car{carId=1, manufacturer='ford', model='focus', doors=4, horsepower=9000}
}
 
Example #16
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitLessThanOrEqualToQuery(CQNGrammarParser.LessThanOrEqualToQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    Comparable value = queryParser.parseValue(attribute, ctx.queryParameter());
    addParsedQuery(ctx, QueryFactory.lessThanOrEqualTo(attribute, value));
}
 
Example #17
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitLessThanQuery(CQNGrammarParser.LessThanQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    Comparable value = queryParser.parseValue(attribute, ctx.queryParameter());
    addParsedQuery(ctx, QueryFactory.lessThan(attribute, value));
}
 
Example #18
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitGreaterThanOrEqualToQuery(CQNGrammarParser.GreaterThanOrEqualToQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    Comparable value = queryParser.parseValue(attribute, ctx.queryParameter());
    addParsedQuery(ctx, QueryFactory.greaterThanOrEqualTo(attribute, value));
}
 
Example #19
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitGreaterThanQuery(CQNGrammarParser.GreaterThanQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    Comparable value = queryParser.parseValue(attribute, ctx.queryParameter());
    addParsedQuery(ctx, QueryFactory.greaterThan(attribute, value));
}
 
Example #20
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitVerboseBetweenQuery(CQNGrammarParser.VerboseBetweenQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    List<? extends ParseTree> queryParameters = ctx.queryParameter(), booleanParameters = ctx.BooleanLiteral();
    Comparable lowerValue = queryParser.parseValue(attribute, queryParameters.get(0));
    boolean lowerInclusive = queryParser.parseValue(Boolean.class, booleanParameters.get(0));
    Comparable upperValue = queryParser.parseValue(attribute, queryParameters.get(1));
    boolean upperInclusive = queryParser.parseValue(Boolean.class, booleanParameters.get(1));
    addParsedQuery(ctx, QueryFactory.between(attribute, lowerValue, lowerInclusive, upperValue, upperInclusive));
}
 
Example #21
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected SuffixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(StringEndsWith.class);
        add(StringContains.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentSuffixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #22
Source File: StringMatchesRegex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesNonSimpleAttribute(Attribute<O, A> attribute, O object, QueryOptions queryOptions) {
    for (A attributeValue : attribute.getValues(object, queryOptions)) {
        if (matchesValue(attributeValue, queryOptions)) {
            return true;
        }
    }
    return false;
}
 
Example #23
Source File: QueryParser.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public <A> Attribute<O, A> getAttribute(ParseTree attributeNameContext, Class<A> expectedSuperType) {
    String attributeName = parseValue(String.class, attributeNameContext.getText());
    Attribute<O, ?> attribute = attributes.get(attributeName);
    if (attribute == null) {
        throw new IllegalStateException("No such attribute has been registered with the parser: " + attributeName);
    }
    if (!expectedSuperType.isAssignableFrom(attribute.getAttributeType())) {
        throw new IllegalStateException("Non-" + expectedSuperType.getSimpleName() + " attribute used in a query which requires a " + expectedSuperType.getSimpleName() + " attribute: " + attribute.getAttributeName());
    }
    @SuppressWarnings("unchecked")
    Attribute<O, A> result = (Attribute<O, A>) attribute;
    return result;
}
 
Example #24
Source File: NavigableIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods. Creates a new NavigableIndex initialized to index
 * the supplied attribute.
 *
 * @param indexMapFactory A factory used to create the main map-based data structure used by the index
 * @param valueSetFactory A factory used to create sets to store values in the index
 * @param attribute The attribute on which the index will be built
 */
protected NavigableIndex(Factory<ConcurrentNavigableMap<A, StoredResultSet<O>>> indexMapFactory, Factory<StoredResultSet<O>> valueSetFactory, Attribute<O, A> attribute) {
    super(indexMapFactory, valueSetFactory,  attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(In.class);
        add(LessThan.class);
        add(GreaterThan.class);
        add(Between.class);
        add(Has.class);
    }});
}
 
Example #25
Source File: ExistsIn.java    From cqengine with Apache License 2.0 5 votes vote down vote up
public ExistsIn(IndexedCollection<F> foreignCollection, Attribute<O, A> localKeyAttribute, Attribute<F, A> foreignKeyAttribute, Query<F> foreignRestrictions) {
    super(requireNonNull(localKeyAttribute, "The localKeyAttribute cannot be null"));
    this.foreignCollection = requireNonNull(foreignCollection, "The foreignCollection cannot be null");
    this.localKeyAttribute = requireNonNull(localKeyAttribute, "The localKeyAttribute cannot be null");
    this.foreignKeyAttribute = requireNonNull(foreignKeyAttribute, "The foreignKeyAttribute cannot be null");
    this.foreignRestrictions = foreignRestrictions; // ..this may be null
}
 
Example #26
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected SuffixTreeIndex(Attribute<O, A> attribute) {
    this(attribute, new DefaultCharArrayNodeFactory());
}
 
Example #27
Source File: DeduplicatingIterator.java    From cqengine with Apache License 2.0 4 votes vote down vote up
public DeduplicatingIterator(Attribute<O, A> uniqueAttribute, QueryOptions queryOptions, Iterator<O> wrappedIterator) {
    super(wrappedIterator, queryOptions);
    this.uniqueAttribute = uniqueAttribute;
}
 
Example #28
Source File: RadixTreeIndex.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected RadixTreeIndex(Attribute<O, A> attribute) {
    this(attribute, new DefaultCharArrayNodeFactory());
}
 
Example #29
Source File: GreaterThan.java    From cqengine with Apache License 2.0 4 votes vote down vote up
public GreaterThan(Attribute<O, A> attribute, A value, boolean valueInclusive) {
    super(attribute);
    this.value = checkQueryValueNotNull(value);
    this.valueInclusive = valueInclusive;
}
 
Example #30
Source File: QueryParser.java    From cqengine with Apache License 2.0 4 votes vote down vote up
public <A> A parseValue(Attribute<O, A> attribute, ParseTree parameterContext) {
    return parseValue(attribute.getAttributeType(), parameterContext.getText());
}