@Override public boolean evaluate(ServiceDocument document, ServiceDocumentDescription description) { PropertyDescription pd = description.propertyDescriptions.get(this.propertyName); if (pd == null) { return false; } Object propValue = ReflectionUtils.getPropertyValue(pd, document); String matchAs = QuerySpecification.toMatchValue(propValue); if (matchAs == null) { return false; } Evaluator e = this.table.get(matchAs); if (e == null) { return false; } return e.evaluate(document, description); }
stringValue = QuerySpecification.toMatchValue((URI) v); luceneField = getAndSetStringField(fieldName, stringValue, fsv, isCollectionItem); } else if (pd.typeName.equals(TypeName.ENUM)) { stringValue = QuerySpecification.toMatchValue((Enum<?>) v); luceneField = getAndSetStringField(fieldName, stringValue, fsv, isCollectionItem); } else if (pd.typeName.equals(TypeName.LONG)) { stringValue = QuerySpecification.toMatchValue((boolean) v); luceneField = getAndSetStringField(fieldName, stringValue, fsv, isCollectionItem); } else if (pd.typeName.equals(TypeName.BYTES)) {
/** * Convert the given value to a normalized string representation that is used for * both generating indexed values and query criteria used to match against those * indexes. * * @return A string value that can be used to both index and query, or NULL if the given */ public static String toMatchValue(Object value) { if (value == null) { return null; } else if (value instanceof String) { return (String) value; } else if (value instanceof Boolean) { return toMatchValue((boolean) value); } else if (value instanceof URI) { return toMatchValue((URI) value); } else if (value instanceof Enum) { return toMatchValue((Enum<?>) value); } else { return value.toString(); } }
@Test public void toMatchValue() throws Throwable { final String str = "aaa"; final Enum<?> enumV = TaskStage.CANCELLED; final String uriStr = "http://about.drekware.com"; final URI uriV = URI.create(uriStr); // Object-based calls assertNull(QuerySpecification.toMatchValue((Object) null)); assertEquals(str, QuerySpecification.toMatchValue(str)); assertEquals("CANCELLED", QuerySpecification.toMatchValue((Object) enumV)); assertEquals(uriStr, QuerySpecification.toMatchValue((Object) uriV)); assertEquals("2345", QuerySpecification.toMatchValue(2345L)); assertEquals("true", QuerySpecification.toMatchValue((Object) true)); assertEquals("false", QuerySpecification.toMatchValue((Object) false)); // Boolean-specific calls assertEquals("true", QuerySpecification.toMatchValue(true)); assertEquals("false", QuerySpecification.toMatchValue(false)); // Enum-specific calls assertNull(QuerySpecification.toMatchValue((Enum<?>) null)); assertEquals("CANCELLED", QuerySpecification.toMatchValue(enumV)); // URI-specific calls assertNull(QuerySpecification.toMatchValue((URI) null)); assertEquals(uriStr, QuerySpecification.toMatchValue(uriV)); }
@Test public void evaluateWithEnum2() throws QueryFilterException { Query q = Query.Builder.create() .addFieldClause("e1", QueryTask.QuerySpecification.toMatchValue(Color.RED), MatchType.TERM, Occurance.SHOULD_OCCUR) .addFieldClause("e1", QueryTask.QuerySpecification.toMatchValue(Color.BLUE), MatchType.TERM, Occurance.SHOULD_OCCUR) .build(); q.setOccurance(Occurance.MUST_NOT_OCCUR); QueryFilter filter = QueryFilter.create(q); QueryFilterDocument document = new QueryFilterDocument(); document.e1 = Color.RED; assertFalse(filter.evaluate(document, this.description)); document.e1 = Color.BLUE; assertFalse(filter.evaluate(document, this.description)); document.e1 = Color.WHITE; assertTrue(filter.evaluate(document, this.description)); }
@Test public void toMatchValue() throws Throwable { final String str = "aaa"; final Enum<?> enumV = TaskStage.CANCELLED; final String uriStr = "http://about.drekware.com"; final URI uriV = URI.create(uriStr); // Object-based calls assertNull(QuerySpecification.toMatchValue((Object) null)); assertEquals(str, QuerySpecification.toMatchValue(str)); assertEquals("CANCELLED", QuerySpecification.toMatchValue((Object) enumV)); assertEquals(uriStr, QuerySpecification.toMatchValue((Object) uriV)); assertEquals("2345", QuerySpecification.toMatchValue(2345L)); assertEquals("true", QuerySpecification.toMatchValue((Object) true)); assertEquals("false", QuerySpecification.toMatchValue((Object) false)); // Boolean-specific calls assertEquals("true", QuerySpecification.toMatchValue(true)); assertEquals("false", QuerySpecification.toMatchValue(false)); // Enum-specific calls assertNull(QuerySpecification.toMatchValue((Enum<?>) null)); assertEquals("CANCELLED", QuerySpecification.toMatchValue(enumV)); // URI-specific calls assertNull(QuerySpecification.toMatchValue((URI) null)); assertEquals(uriStr, QuerySpecification.toMatchValue(uriV)); }
/** * Add a {@link Occurance#MUST_OCCUR} clause which matches a top level field name using * {@link MatchType#TERM}. * @param fieldName the top level field name. * @param fieldValue the field value to match. * @return a reference to this object. */ public Builder addFieldClause(String fieldName, Object fieldValue) { return addFieldClause(fieldName, QuerySpecification.toMatchValue(fieldValue), MatchType.TERM, Occurance.MUST_OCCUR); }