1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.search.expression;
18
19 import java.util.Map;
20
21 import net.sf.ehcache.Element;
22 import net.sf.ehcache.search.SearchException;
23 import net.sf.ehcache.search.attribute.AttributeExtractor;
24 import net.sf.ehcache.search.attribute.AttributeType;
25
26 /***
27 * A comparison operator meaning Java "equals to" condition
28 *
29 * @author teck
30 */
31 public class EqualTo extends BaseCriteria {
32
33 private final Object value;
34 private final String attributeName;
35 private final AttributeType type;
36
37 /***
38 * Constructor
39 *
40 * @param attributeName attribute name
41 * @param value
42 */
43 public EqualTo(String attributeName, Object value) {
44 if (value == null || attributeName == null) {
45 throw new NullPointerException();
46 }
47
48 this.attributeName = attributeName;
49 this.value = value;
50
51 this.type = AttributeType.typeFor(attributeName, value);
52 }
53
54 /***
55 * Get attribute value.
56 *
57 * @return attribute value
58 */
59 public Object getValue() {
60 return value;
61 }
62
63 /***
64 * Get attribute name.
65 *
66 * @return attribute name.
67 */
68 public String getAttributeName() {
69 return attributeName;
70 }
71
72 /***
73 * Get attribute type.
74 *
75 * @return attribute type.
76 */
77 public AttributeType getType() {
78 return type;
79 }
80
81 /***
82 * {@inheritDoc}
83 */
84 public boolean execute(Element e, Map<String, AttributeExtractor> attributeExtractors) {
85 Object attributeValue = attributeExtractors.get(attributeName).attributeFor(e, getAttributeName());
86 if (attributeValue == null) {
87 return false;
88 } else {
89 AttributeType attrType = AttributeType.typeFor(getAttributeName(), attributeValue);
90 if (!getType().equals(attrType)) {
91 throw new SearchException("Expecting attribute of type " + getType().name() + " but was " + attrType.name());
92 }
93
94 if (getType().equals(AttributeType.STRING)) {
95 return ((String) this.value).equalsIgnoreCase((String) attributeValue);
96 } else {
97 return this.value.equals(attributeValue);
98 }
99 }
100 }
101 }