001: /*
002: * $Id: EntityOperator.java,v 1.3 2004/01/14 00:08:11 ajzeneski Exp $
003: *
004: * Copyright (c) 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024:
025: package org.ofbiz.entity.condition;
026:
027: import java.util.HashMap;
028:
029: /**
030: * Encapsulates operations between entities and entity fields. This is a immutable class.
031: *
032: *@author <a href='mailto:chris_maurer@altavista.com'>Chris Maurer</a>
033: *@author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
034: *@author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
035: *@version $Revision: 1.3 $
036: *@since 2.0
037: */
038: public class EntityOperator implements java.io.Serializable {
039:
040: public static final int ID_EQUALS = 1;
041: public static final int ID_NOT_EQUAL = 2;
042: public static final int ID_LESS_THAN = 3;
043: public static final int ID_GREATER_THAN = 4;
044: public static final int ID_LESS_THAN_EQUAL_TO = 5;
045: public static final int ID_GREATER_THAN_EQUAL_TO = 6;
046: public static final int ID_IN = 7;
047: public static final int ID_BETWEEN = 8;
048: public static final int ID_NOT = 9;
049: public static final int ID_AND = 10;
050: public static final int ID_OR = 11;
051: public static final int ID_LIKE = 12;
052: public static final int ID_NOT_IN = 13;
053:
054: private static HashMap registry = new HashMap();
055:
056: private static void register(String name, EntityOperator operator) {
057: registry.put(name, operator);
058: }
059:
060: public static EntityOperator lookup(String name) {
061: return (EntityOperator) registry.get(name);
062: }
063:
064: public static EntityComparisonOperator lookupComparison(String name) {
065: EntityOperator operator = lookup(name);
066: if (!(operator instanceof EntityComparisonOperator))
067: throw new IllegalArgumentException(name
068: + " is not a comparison operator");
069: return (EntityComparisonOperator) operator;
070: }
071:
072: public static EntityJoinOperator lookupJoin(String name) {
073: EntityOperator operator = lookup(name);
074: if (!(operator instanceof EntityJoinOperator))
075: throw new IllegalArgumentException(name
076: + " is not a join operator");
077: return (EntityJoinOperator) operator;
078: }
079:
080: public static final EntityComparisonOperator EQUALS = new EntityComparisonOperator(
081: ID_EQUALS, "=") {
082: public boolean compare(Object lhs, Object rhs) {
083: return EntityComparisonOperator.compareEqual(lhs, rhs);
084: }
085: };
086: static {
087: register("equals", EQUALS);
088: }
089: public static final EntityComparisonOperator NOT_EQUAL = new EntityComparisonOperator(
090: ID_NOT_EQUAL, "<>") {
091: public boolean compare(Object lhs, Object rhs) {
092: return EntityComparisonOperator.compareNotEqual(lhs, rhs);
093: }
094: };
095: static {
096: register("notEqual", NOT_EQUAL);
097: }
098: public static final EntityComparisonOperator LESS_THAN = new EntityComparisonOperator(
099: ID_LESS_THAN, "<") {
100: public boolean compare(Object lhs, Object rhs) {
101: return EntityComparisonOperator.compareLessThan(lhs, rhs);
102: }
103: };
104: static {
105: register("lessThan", LESS_THAN);
106: }
107: public static final EntityComparisonOperator GREATER_THAN = new EntityComparisonOperator(
108: ID_GREATER_THAN, ">") {
109: public boolean compare(Object lhs, Object rhs) {
110: return EntityComparisonOperator
111: .compareGreaterThan(lhs, rhs);
112: }
113: };
114: static {
115: register("greaterThan", GREATER_THAN);
116: }
117: public static final EntityComparisonOperator LESS_THAN_EQUAL_TO = new EntityComparisonOperator(
118: ID_LESS_THAN_EQUAL_TO, "<=") {
119: public boolean compare(Object lhs, Object rhs) {
120: return EntityComparisonOperator.compareLessThanEqualTo(lhs,
121: rhs);
122: }
123: };
124: static {
125: register("lessThanEqualTo", LESS_THAN_EQUAL_TO);
126: }
127: public static final EntityComparisonOperator GREATER_THAN_EQUAL_TO = new EntityComparisonOperator(
128: ID_GREATER_THAN_EQUAL_TO, ">=") {
129: public boolean compare(Object lhs, Object rhs) {
130: return EntityComparisonOperator.compareGreaterThanEqualTo(
131: lhs, rhs);
132: }
133: };
134: static {
135: register("greaterThanEqualTo", GREATER_THAN_EQUAL_TO);
136: }
137: public static final EntityComparisonOperator IN = new EntityComparisonOperator(
138: ID_IN, "IN") {
139: public boolean compare(Object lhs, Object rhs) {
140: return EntityComparisonOperator.compareIn(lhs, rhs);
141: }
142: };
143: static {
144: register("in", IN);
145: }
146: public static final EntityComparisonOperator BETWEEN = new EntityComparisonOperator(
147: ID_BETWEEN, "BETWEEN");
148: static {
149: register("between", BETWEEN);
150: }
151: public static final EntityComparisonOperator NOT = new EntityComparisonOperator(
152: ID_NOT, "NOT");
153: static {
154: register("not", NOT);
155: }
156: public static final EntityJoinOperator AND = new EntityJoinOperator(
157: ID_AND, "AND", false);
158: static {
159: register("and", AND);
160: }
161: public static final EntityJoinOperator OR = new EntityJoinOperator(
162: ID_OR, "OR", true);
163: static {
164: register("or", OR);
165: }
166: public static final EntityComparisonOperator LIKE = new EntityComparisonOperator(
167: ID_LIKE, "LIKE") {
168: public boolean compare(Object lhs, Object rhs) {
169: return EntityComparisonOperator.compareLike(lhs, rhs);
170: }
171: };
172: static {
173: register("like", LIKE);
174: }
175: public static final EntityComparisonOperator NOT_IN = new EntityComparisonOperator(
176: ID_NOT_IN, "NOT IN");
177: static {
178: register("not-in", NOT_IN);
179: }
180:
181: protected int idInt;
182: protected String codeString;
183:
184: public EntityOperator(int id, String code) {
185: idInt = id;
186: codeString = code;
187: }
188:
189: public String getCode() {
190: if (codeString == null)
191: return "null";
192: else
193: return codeString;
194: }
195:
196: public int getId() {
197: return idInt;
198: }
199:
200: public String toString() {
201: return codeString;
202: }
203:
204: public int hashCode() {
205: return this .codeString.hashCode();
206: }
207:
208: public boolean equals(Object obj) {
209: EntityOperator otherOper = (EntityOperator) obj;
210: return this .idInt == otherOper.idInt;
211: }
212:
213: public class MatchResult {
214: public boolean shortCircuit = false;
215: public boolean matches = false;
216: }
217: }
|