1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.query.process;
25
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Set;
30 import org.modeshape.graph.Location;
31
32 /**
33 * A {@link ProcessingComponent} implementation that removes duplicates. The results from the delegate component do not need to be
34 * sorted; in fact, if the delegate component is a {@link SortValuesComponent}, then use {@link DistinctOfSortedComponent} instead of
35 * this class.
36 */
37 public class DistinctComponent extends DelegatingComponent {
38
39 public DistinctComponent( ProcessingComponent delegate ) {
40 super(delegate);
41 }
42
43 /**
44 * {@inheritDoc}
45 *
46 * @see org.modeshape.graph.query.process.ProcessingComponent#execute()
47 */
48 @Override
49 public List<Object[]> execute() {
50 List<Object[]> tuples = delegate().execute();
51 if (tuples.size() > 1) {
52 // Go through this list and remove any tuples that appear more than once ...
53 Iterator<Object[]> iter = tuples.iterator();
54
55 int locationCount = getColumns().getLocationCount();
56 int firstLocationIndex = getColumns().getColumnCount();
57 if (locationCount == 1) {
58 // We can determine duplicates faster/cheaper using a single Set<Location> ...
59 Set<Location> found = new HashSet<Location>();
60 while (iter.hasNext()) {
61 Location location = (Location)iter.next()[firstLocationIndex];
62 if (!found.add(location)) {
63 // Was already found, so remove this tuple from the results ...
64 iter.remove();
65 }
66 }
67 } else {
68 assert locationCount > 1;
69 // Duplicate tuples are removed using a Set<Location[]> ...
70 Set<Location[]> found = new HashSet<Location[]>();
71 while (iter.hasNext()) {
72 Object[] tuple = iter.next();
73 Location[] locations = new Location[locationCount];
74 System.arraycopy(tuple, firstLocationIndex, locations, 0, locationCount);
75 if (!found.add(locations)) {
76 // Was already found, so remove this tuple from the results ...
77 iter.remove();
78 }
79 }
80 }
81 }
82 return tuples;
83 }
84 }