001:/*
002:This source file is part of Smyle, a database library.
003:For up-to-date information, see http://www.drjava.de/smyle
004:Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006:This library is free software; you can redistribute it and/or
007:modify it under the terms of the GNU Lesser General Public
008:License as published by the Free Software Foundation; either
009:version 2.1 of the License, or (at your option) any later version.
010:
011:This library 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 GNU
014:Lesser General Public License for more details.
015:
016:You should have received a copy of the GNU Lesser General Public
017:License along with this library; if not, write to the Free Software
018:Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020:For full license text, see doc/license/lgpl.txt in this distribution
021:*/
022:
023:package drjava.smyle.meta;
024:
025:import java.util.*;
026:import org.artsProject.mcop.*;
027:import drjava.smyle.*;
028:
029:/** an object that holds conditions for filtering records; similar to
030: "where" and "order by" clauses in SQL queries.<p>
031: Subclasses of Filter are generated by the Smyle IDL compiler. */
032:public class Filter<T extends Struct<T>> {
033: protected ArrayList<Filter<T>.Clause> clauses = new ArrayList<Filter<T>.Clause>();
034: protected ArrayList<Function/*<T,Comparable>*/> order = new ArrayList<Function/*<T,Comparable>*/>();
035: protected boolean reverse = false;
036:
037: public boolean matches(T t) {
038: for (int i = 0; i < clauses.size(); i++)
039: if (!clauses.get(i).matches(t))
040: return false;
041: return true;
042: }
043:
044: public interface Clause {
045: public boolean matches(T t);
046:
047: // for automagic indexing - return function that could
048: // be indexed in order to speed up this clause
049: public Function getFunction();
050: public Object getValue();
051: }
052:
053: protected class FunctionEquals<A> implements Clause {
054: private final Function<T,A> f;
055: private final A value;
056:
057: public FunctionEquals(Function<T,A> f, A value) {
058: this .f = f;
059: this .value = value;
060: }
061:
062: public boolean matches(T t) {
063: return MCOP.equals(value, f.of(t));
064: }
065:
066: public Function getFunction() { return f; }
067: public Object getValue() { return value; }
068: }
069:
070: /*protected class FunctionComparator<A implements Comparable> implements Comparator<T> {
071: private final Function<T,A> f;
072:
073: public FunctionComparator(Function<T,A> f) {
074: this.f = f;
075: }
076:
077: public int compare(T a, T b) {
078: return f.of(a).compareTo(f.of(b));
079: }
080: }
081:
082: protected class BooleanFunctionComparator implements Comparator<T> {
083: private final Function<T,Boolean> f;
084:
085: public BooleanFunctionComparator(Function<T,Boolean> f) {
086: this.f = f;
087: }
088:
089: public int compare(T a, T b) {
090: return (f.of(a).booleanValue() ? 1 : 0) - (f.of(b).booleanValue() ? 1 : 0);
091: }
092: }*/
093:
094: public ArrayList<Function<T,Comparable>> _getOrder() {
095: return (ArrayList) order;
096: }
097:
098: public int _numClauses() {
099: return clauses.size();
100: }
101:
102: public Clause _getClause(int index) {
103: return clauses.get(index);
104: }
105:
106: /*public void _addClause(Filter<T>.Clause c) {
107: clauses.add(c);
108: }*/
109:
110: public boolean isReversed() {
111: return reverse;
112: }
113:
114: public <A implements Comparable> void _orderBy(Field<T,A> field) {
115: order.add(field);
116: }
117:
118: public void _setReverse(boolean b) {
119: reverse = b;
120: }
121:
122: public T _createMatchingElement() {
123: T t = createElement();
124: for (int i = 0; i < clauses.size(); i++) {
125: Filter<T>.Clause c = clauses.get(i);
126: Function f = c.getFunction();
127: if (f instanceof Cascade)
128: f = ((Cascade) f).f();
129: if (f instanceof Field)
130: t.setField(((Field) f).nr(), c.getValue());
131: }
132: return t;
133: }
134:
135: protected T createElement() {
136: throw new InternalSmyleError("not available in generic filters");
137: }
138:}
|