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 Or class implements a disjunction of multiple queries.
28 public class Or implements Query {
31 * Holds the disjuncts being combined.
33 public Query [] disjuncts;
36 * Keeps track of which query is current.
43 public Or(Query/*...*/ [] q)
50 * The {@link Query#copy} method implemented by creating a new
51 * Or object with copies of the disjuncts.
53 public Query copy(Vector/*<Ref>*/ newrefs) throws Exception {
54 Query [] q = new Query [ disjuncts.length ];
55 for ( int i = 0; i < q.length; i++ ) {
56 q[i] = disjuncts[i].copy( newrefs );
62 * The {@link Query#reset} method implemented by reverting to the
63 * first disjunct and resetting it.
69 disjuncts[ 0 ].reset();
73 * The {@link Query#next} method implemented by considering all
74 * disjucts one by one to the next match in a left-to-right fashion.
79 while ( current < disjuncts.length ) {
80 if ( disjuncts[ current ].next() )
83 if ( current >= disjuncts.length )
85 disjuncts[ current ].reset();
91 * The {@link Query#getRefs} method implemented by combining the
92 * Ref objects of all disjuncts.
94 public Vector/*<Ref>*/ getRefs(Vector/*<Ref>*/ v) {
95 for ( int i=0; i < disjuncts.length; i++ )
96 disjuncts[ i ].getRefs( v );
101 * The {@link Query#addObserver} method implemented by adding
102 * the observer to all the disjuncts.
104 public void addObserver(Observer x) {
105 for ( int i=0; i < disjuncts.length; i++ )
106 disjuncts[ i ].addObserver( x );
110 * The {@link Query#deleteObserver} method implemented by
111 * removing the observer from all the disjuncts.
113 public void deleteObserver(Observer x) {
114 for ( int i=0; i < disjuncts.length; i++ )
115 disjuncts[ i ].deleteObserver( x );
119 * The {@link Query#addable} method implemented by forwarding to all
122 public boolean addable()
124 for ( int i=0; i < disjuncts.length; i++ )
125 if ( disjuncts[ i ].addable() )
131 * The {@link Query#add} method implemented by forwarding to first
136 for ( int i=0; i < disjuncts.length; i++ ) {
137 if ( disjuncts[ i ].addable() )
138 return disjuncts[ i ].add();
144 * Implements {@link Query#removable} by checking that all
145 * disjuncts are removable.
147 public boolean removable()
149 for ( int i=0; i < disjuncts.length; i++ )
150 if ( ! disjuncts[ i ].removable() )
156 * Implements {@link Query#remove} by forwarding to all disjuncts.
158 public boolean remove() {
159 boolean removed = false;
160 for ( int i=0; i < disjuncts.length; i++ )
161 if ( disjuncts[ i ].removable() )
162 removed |= disjuncts[ i ].remove();
167 * Returns a {@link String} representation of the Or object.
169 public String toString() {
170 StringBuffer s = new StringBuffer();
172 for ( int i=0; i < disjuncts.length; i++ ) {
176 s.append( disjuncts[ i ].toString() );