001: /*
002: $Header: /cvsroot/xorm/xorm/src/org/xorm/query/Operator.java,v 1.9 2003/04/26 23:58:40 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.query;
021:
022: import java.util.Collection;
023:
024: /**
025: * A typesafe enumeration with some helper functions.
026: */
027: public final class Operator {
028: private final int id;
029: private final String debug;
030: private final boolean isMethod;
031: private final int opposite;
032:
033: private Operator(int id, String debug, boolean isMethod,
034: int opposite) {
035: this .id = id;
036: this .debug = debug;
037: this .isMethod = isMethod;
038: this .opposite = opposite;
039: }
040:
041: public int getId() {
042: return id;
043: }
044:
045: public String toString() {
046: return debug;
047: }
048:
049: public boolean isMethod() {
050: return isMethod;
051: }
052:
053: public Operator getOpposite() {
054: if (opposite == -1)
055: return null;
056: return lookup(opposite);
057: }
058:
059: public boolean equals(Object o) {
060: if (this == o)
061: return true;
062: if (o == null)
063: return false;
064: if (!(o instanceof Operator))
065: return false;
066: return id == ((Operator) o).id;
067: }
068:
069: public static final Operator EQUAL = new Operator(0, "==", false, 1);
070: public static final Operator NOT_EQUAL = new Operator(1, "!=",
071: false, 0);
072: public static final Operator LT = new Operator(2, "<", false, 5);
073: public static final Operator GT = new Operator(3, ">", false, 4);
074: public static final Operator LTE = new Operator(4, "<=", false, 3);
075: public static final Operator GTE = new Operator(5, ">=", false, 2);
076: public static final Operator STARTS_WITH = new Operator(6,
077: "startsWith", true, -1);
078: public static final Operator ENDS_WITH = new Operator(7,
079: "endsWith", true, -1);
080: public static final Operator STR_CONTAINS = new Operator(8,
081: "strstr", true, -1);
082: public static final Operator CONTAINS = new Operator(9, "contains",
083: true, -1);
084: public static final Operator ANDC = new Operator(10, "&&", false,
085: -1);
086: public static final Operator ORC = new Operator(11, "||", false, -1);
087: public static final Operator ANDL = new Operator(12, "&", false, -1);
088: public static final Operator ORL = new Operator(13, "|", false, -1);
089: public static final Operator TIMES = new Operator(14, "*", false,
090: -1);
091: public static final Operator DIVIDE = new Operator(15, "/", false,
092: -1);
093: public static final Operator PLUS = new Operator(16, "+", false, -1);
094: public static final Operator MINUS = new Operator(17, "-", false,
095: -1);
096: public static final Operator XOR = new Operator(18, "^", false, -1);
097: public static final Operator MODULO = new Operator(19, "%", false,
098: -1);
099: public static final Operator IN = new Operator(20, "IN", false, -1);
100: public static final Operator INSTANCEOF = new Operator(21,
101: "instanceof", false, -1);
102:
103: private static final Operator[] all = new Operator[] { EQUAL,
104: NOT_EQUAL, LT, GT, LTE, GTE, STARTS_WITH, ENDS_WITH,
105: STR_CONTAINS, CONTAINS, ANDC, ORC, ANDL, ORL, TIMES,
106: DIVIDE, PLUS, MINUS, XOR, MODULO, IN, INSTANCEOF };
107:
108: public static Operator lookup(int id) {
109: if (id < 0 || id >= all.length)
110: return null;
111: return all[id];
112: }
113:
114: }
|