01: package it.unimi.dsi.mg4j.query.nodes;
02:
03: import java.util.Arrays;
04:
05: import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
06: import it.unimi.dsi.mg4j.index.MultiTermIndexIterator;
07: import it.unimi.dsi.mg4j.search.DocumentIterator;
08: import it.unimi.dsi.lang.MutableString;
09:
10: /*
11: * MG4J: Managing Gigabytes for Java
12: *
13: * Copyright (C) 2007 Sebastiano Vigna
14: *
15: * This library is free software; you can redistribute it and/or modify it
16: * under the terms of the GNU Lesser General Public License as published by the Free
17: * Software Foundation; either version 2.1 of the License, or (at your option)
18: * any later version.
19: *
20: * This library is distributed in the hope that it will be useful, but
21: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
23: * for more details.
24: *
25: * You should have received a copy of the GNU Lesser General Public License
26: * along with this program; if not, write to the Free Software
27: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28: *
29: */
30:
31: /** A node representing a virtual term obtained by merging the occurrences of the given terms.
32: *
33: * <p>This node is mainly useful when performing query expansion. The {@link QueryBuilderVisitor}
34: * used to generate {@linkplain DocumentIterator document iterators} can decide which
35: * policy to use for setting the frequency and the name of the virtual term.
36: *
37: * @author Sebastiano Vigna
38: * @see MultiTermIndexIterator
39: */
40:
41: public class MultiTerm extends Composite {
42:
43: /** Creates a new multi-term node.
44: *
45: * @param term a vector of nodes representing distinct terms.
46: * @throws IllegalArgumentException if some term appears twice in <code>term</code>.
47: */
48:
49: @SuppressWarnings("cast")
50: public MultiTerm(final Term... term) {
51: super ((Query[]) term);
52: final ObjectOpenHashSet<MutableString> s = new ObjectOpenHashSet<MutableString>(
53: term.length);
54: for (Term t : term)
55: s.add(new MutableString(t.term));
56: if (s.size() != term.length)
57: throw new IllegalArgumentException(
58: "Multiterm nodes require distinct terms");
59: }
60:
61: public String toString() {
62: return super .toString("MULTITERM(", ")", ", ");
63: }
64:
65: public <T> T accept(final QueryBuilderVisitor<T> visitor)
66: throws QueryBuilderVisitorException {
67: if (!visitor.visitPre(this ))
68: return null;
69: final T[] result = visitor.newArray(query.length);
70: for (int i = 0; i < query.length; i++)
71: if ((result[i] = query[i].accept(visitor)) == null)
72: return null;
73: return visitor.visitPost(this , result);
74: }
75:
76: public boolean equals(final Object o) {
77: if (!(o instanceof MultiTerm))
78: return false;
79: return Arrays.equals(query, ((MultiTerm) o).query);
80: }
81:
82: public int hashCode() {
83: return Arrays.hashCode(query) ^ getClass().hashCode();
84: }
85: }
|