View Javadoc

1   /*
2    * Copyright (c) 2007, Peter Mika All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.smusher.listener;
30  
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.HashSet;
34  import java.util.Set;
35  
36  
37  import org.openrdf.model.Resource;
38  
39  /** This listener collects all matches reported, 
40   * ignoring duplicates and symmetric matches.
41   * 
42   * @author Peter Mika (pmika@cs.vu.nl)
43   *
44   */
45  public class CollectorListener implements SmusherListener {
46  
47      public CollectorListener() {
48          super();
49          // TODO Auto-generated constructor stub
50      }
51  
52      protected Set _matches = new HashSet();
53      
54      public class Pair {
55          org.openrdf.model.Resource first, second;
56  
57          public Pair (Resource first, Resource second) {
58              this.first = first;
59              this.second = second;
60          }
61          
62          /**
63           * @return Returns the first.
64           */
65          public Resource getFirst() {
66              return first;
67          }
68  
69          /**
70           * @param first The first to set.
71           */
72          public void setFirst(Resource first) {
73              this.first = first;
74          }
75  
76          /**
77           * @return Returns the second.
78           */
79          public Resource getSecond() {
80              return second;
81          }
82  
83          /**
84           * @param second The second to set.
85           */
86          public void setSecond(Resource second) {
87              this.second = second;
88          }
89          
90          public int hashCode() {
91              return first.hashCode() ^ second.hashCode();
92          }
93          
94          public boolean equals(Object obj) {
95              if (obj instanceof Pair) {
96                  Pair other = (Pair) obj;
97                  return (this.first.equals(other.first) && 
98                          this.second.equals(other.second)) ||
99                         (this.first.equals(other.second) &&
100                         this.second.equals(other.first));
101             } else {
102                 return false;
103             }
104                 
105         }
106         
107         public String toString() {
108             return "[" + first.toString() + " " + second.toString() + "]";
109         }
110     }
111     
112   
113     public void start() {   
114         _matches = new HashSet();
115         
116     }
117     
118     public void matchFound(org.openrdf.model.Resource first, org.openrdf.model.Resource second) {     
119        
120         _matches.add(new Pair(first, second));
121         
122     }
123     
124     public void commit() {
125         //nothing to do
126     }
127      
128     public void finish() {
129         //we don't throw the matches away
130     }
131     
132     public Collection getUniqueMatches() {
133         return Collections.unmodifiableCollection(_matches);
134     }
135     
136   
137     
138 }