001: /*
002: * ColumnExpression.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.storage.filter;
013:
014: import java.util.Map;
015:
016: /**
017: * A class to define the filter criteria for a single column
018: *
019: * @author support@sql-workbench.net
020: */
021: public class ColumnExpression implements FilterExpression,
022: ExpressionValue {
023: private String columnName;
024: private Object filterValue;
025: private ColumnComparator comparator;
026: private boolean ignoreCase;
027:
028: /**
029: * Default constructor needed for XML serialisation.
030: */
031: public ColumnExpression() {
032: }
033:
034: /**
035: * Define the filter for a column
036: *
037: * @param column the column name
038: * @param comparator the comparator to be used to compare the reference value against the actual values
039: * @param filterValue the filter value to compare against the actual values
040: *
041: * @see #setFilterValue(Object)
042: * @see #setComparator(ColumnComparator)
043: */
044: public ColumnExpression(String column, ColumnComparator comparator,
045: Object filterValue) {
046: setComparator(comparator);
047: setFilterValue(filterValue);
048: setColumnName(column);
049: }
050:
051: /**
052: * Define a "generic" column filter
053: *
054: * @param comparator the comparator to be used to compare the reference value against the actual values
055: * @param filterValue the filter value to compare against the actual values
056: *
057: * @see #setFilterValue(Object)
058: * @see #setComparator(ColumnComparator)
059: */
060: public ColumnExpression(ColumnComparator comparator,
061: Object filterValue) {
062: setComparator(comparator);
063: setFilterValue(filterValue);
064: setColumnName("*");
065: }
066:
067: public Object getFilterValue() {
068: return filterValue;
069: }
070:
071: public void setFilterValue(Object value) {
072: this .filterValue = value;
073: }
074:
075: public ColumnComparator getComparator() {
076: return comparator;
077: }
078:
079: public void setComparator(ColumnComparator comp) {
080: this .comparator = comp;
081: }
082:
083: public boolean equals(Object other) {
084: try {
085: ColumnExpression def = (ColumnExpression) other;
086: if (this .filterValue == null || def.filterValue == null)
087: return false;
088: boolean result = this .filterValue.equals(def.filterValue);
089: if (result) {
090: result = this .comparator.equals(def.comparator);
091: }
092: return result;
093: } catch (Throwable th) {
094: return false;
095: }
096: }
097:
098: public boolean evaluate(Object value) {
099: if (value != null && !comparator.supportsType(value.getClass()))
100: return false;
101: return comparator.evaluate(filterValue, value, this .ignoreCase);
102: }
103:
104: public boolean evaluate(Map columnValues) {
105: Object value = columnValues.get(this .columnName);
106: return evaluate(value);
107: }
108:
109: public String getColumnName() {
110:
111: return this .columnName;
112: }
113:
114: public void setColumnName(String column) {
115: this .columnName = (column == null ? null : column.toLowerCase());
116: }
117:
118: public String toString() {
119: return "[" + columnName + "] " + this .comparator.getOperator()
120: + " " + comparator.getValueExpression(this .filterValue);
121: }
122:
123: public boolean isIgnoreCase() {
124: return ignoreCase;
125: }
126:
127: public void setIgnoreCase(boolean ignoreCase) {
128: this .ignoreCase = ignoreCase;
129: }
130:
131: public boolean isColumnSpecific() {
132: return true;
133: }
134: }
|