001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.util;
018:
019: /**
020: * @author sfowler
021: *
022: * Provides simple string comparison functionality.
023: *
024: */
025: public class SimpleExpression {
026: public static final String EQUAL = "EQUAL";
027: public static final String NOT_EQUAL = "NOT_EQUAL";
028: public static final String STARTS_WITH = "STARTS_WITH";
029: public static final String NOT_STARTS_WITH = "NOT_STARTS_WITH";
030: public static final String CONTAINS = "CONTAINS";
031: public static final String NOT_CONTAINS = "NOT_CONTAINS";
032: public static final String LESS_THAN = "LESS_THAN";
033: public static final String LESS_OR_EQUAL = "LESS_OR_EQUAL";
034: public static final String GREATER_THAN = "GREATER_THAN";
035: public static final String GREATER_OR_EQUAL = "GREAT";
036:
037: /**
038: * DOCUMENT ME!
039: *
040: * @param lhs_
041: * @param operator
042: * @param rhs_
043: *
044: * @return
045: */
046: public static boolean compare(String lhs_, String operator,
047: String rhs_) {
048: String lhs = (lhs_ == null) ? "" : lhs_;
049: String rhs = (rhs_ == null) ? "" : rhs_;
050:
051: if (operator.equals(SimpleExpression.STARTS_WITH)) {
052: if (lhs.startsWith(rhs)) {
053: return true;
054: } else {
055: return false;
056: }
057: } else if (operator.equals(SimpleExpression.NOT_STARTS_WITH)) {
058: if (lhs.startsWith(rhs)) {
059: return false;
060: } else {
061: return true;
062: }
063: } else if (operator.equals(SimpleExpression.EQUAL)) {
064: if (lhs.equals(rhs)) {
065: return true;
066: } else {
067: return false;
068: }
069: } else if (operator.equals(SimpleExpression.NOT_EQUAL)) {
070: if (lhs.equals(rhs)) {
071: return false;
072: } else {
073: return true;
074: }
075: } else if (operator.equals(SimpleExpression.CONTAINS)) {
076: if (lhs.indexOf(rhs) >= 0) {
077: return true;
078: } else {
079: return false;
080: }
081: } else if (operator.equals(SimpleExpression.NOT_CONTAINS)) {
082: if (lhs.indexOf(rhs) >= 0) {
083: return false;
084: } else {
085: return true;
086: }
087: } else if (operator.equals(SimpleExpression.LESS_THAN)) {
088: if (lhs.compareTo(rhs) < 0) {
089: return true;
090: } else {
091: return false;
092: }
093: } else if (operator.equals(SimpleExpression.LESS_OR_EQUAL)) {
094: if (lhs.compareTo(rhs) <= 0) {
095: return true;
096: } else {
097: return false;
098: }
099: } else if (operator.equals(SimpleExpression.GREATER_THAN)) {
100: if (lhs.compareTo(rhs) > 0) {
101: return true;
102: } else {
103: return false;
104: }
105: } else if (operator.equals(SimpleExpression.GREATER_OR_EQUAL)) {
106: if (lhs.compareTo(rhs) >= 0) {
107: return true;
108: } else {
109: return false;
110: }
111: }
112:
113: return false;
114: }
115: }
|