1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
4 This file is part of GORITE.
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GORITE is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE. If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
20 package com.intendico.data;
22 import java.util.Observer;
23 import java.util.Vector;
26 * The And class implements a conjunction of multiple queries. It
27 * provides a left-to-right short-cut evaluation, which returns
28 * immediately if a conjunct fails.
30 public class And implements Query {
33 * Holds the conjuncts being combined.
35 public Query [] conjuncts;
38 * Keeps track of which query is current.
45 public And(Query/*...*/ [] q) throws Exception {
51 * The {@link Query#copy} method implemented by creating a new
52 * And object with copies of the conjuncts.
54 public Query copy(Vector/*<Ref>*/ newrefs) throws Exception {
55 Query [] q = new Query [ conjuncts.length ];
56 for ( int i = 0; i < q.length; i++ ) {
57 q[i] = conjuncts[i].copy( newrefs );
63 * The {@link Query#reset} method implemented by reverting to the
64 * first conjunct and resetting it.
70 if ( conjuncts != null && conjuncts.length > 0 )
71 conjuncts[ 0 ].reset();
75 * The {@link Query#next} method implemented by advancing all
76 * conjucts to the next match in a left-to-right fashion. Thus,
77 * the right-most conjunct is advanced to its next match. If the
78 * conjunct is exhausted, it gets reset, but after that the
79 * preceding conjunct is advanced to its next match. If that is
80 * exhausted, then that gets reset, and so on, up to advancing the
81 * first conjunct until it gets exhausted.
86 if ( conjuncts == null || conjuncts.length == 0 )
88 while ( current >= 0 ) {
89 if ( conjuncts[ current ].next() ) {
91 if ( current < conjuncts.length ) {
92 conjuncts[ current ].reset();
104 * The {@link Query#getRefs} method implemented by combining the
105 * Ref objects of all conjuncts.
107 public Vector/*<Ref>*/ getRefs(Vector/*<Ref>*/ v) {
108 if ( conjuncts != null ) {
109 for ( int i=0; i < conjuncts.length; i++ ) {
110 conjuncts[ i ].getRefs( v );
117 * The {@link Query#addObserver} method implemented by adding
118 * the observer to all the conjuncts.
120 public void addObserver(Observer x) {
121 if ( conjuncts != null )
122 for ( int i=0; i < conjuncts.length; i++ )
123 conjuncts[ i ].addObserver( x );
127 * The {@link Query#deleteObserver} method implemented by
128 * removing the observer from all the conjuncts.
130 public void deleteObserver(Observer x) {
131 if ( conjuncts != null )
132 for ( int i=0; i < conjuncts.length; i++ )
133 conjuncts[ i ].deleteObserver( x );
137 * The {@link Query#addable} method implemented by forwarding to all
140 public boolean addable()
142 for ( int i=0; i < conjuncts.length; i++ )
143 if ( ! conjuncts[ i ].addable() )
149 * The {@link Query#add} method implemented by forwarding to all
154 boolean added = false;
155 if ( conjuncts != null )
156 for ( int i=0; i < conjuncts.length; i++ )
157 added |= conjuncts[ i ].add();
162 * Implements {@link Query#removable} by finding a conjunct that
165 public boolean removable()
167 for ( int i=0; i < conjuncts.length; i++ )
168 if ( conjuncts[ i ].removable() )
174 * Implements {@link Query#remove} by forawrding to the first
175 * conjunct that is removable.
177 public boolean remove() {
178 for ( int i=0; i < conjuncts.length; i++ )
179 if ( conjuncts[ i ].removable() )
180 return conjuncts[ i ].remove();
185 * Returns a String representation of the And object.
187 public String toString() {
188 StringBuffer s = new StringBuffer();
190 if ( conjuncts != null )
191 for ( int i=0; i < conjuncts.length; i++ ) {
195 s.append( conjuncts[ i ].toString() );