1 /*
2 * Copyright (c) 2009-2012 The 99 Software Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22 package org.nnsoft.sameas4j;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Set;
30
31 /**
32 * This class models an equivalence. An equivalence is a response given by
33 * <a href="http://sameas.org">sameas.org</a> to a given <code>URI</code> in
34 * terms of other <code>owl:sameAs</code> <code>URI</code>.
35 */
36 public final class Equivalence implements Iterable<URI> {
37
38 /**
39 * The equivalent URIs.
40 */
41 private final Set<URI> duplicates = new HashSet<URI>();
42
43 /**
44 * The requested URI.
45 */
46 private final URI uri;
47
48 /**
49 * Creates a new {@code Equivalence} instance by the requested URI.
50 *
51 * @param uri the requested URI.
52 */
53 protected Equivalence(URI uri) {
54 this.uri = uri;
55 }
56
57 /**
58 * Creates a new {@code Equivalence} instance by the requested URL.
59 *
60 * @param url the requested URL.
61 */
62 protected Equivalence(URL url) throws URISyntaxException {
63 this.uri = url.toURI();
64 }
65
66 /**
67 * Returns the requested URI.
68 *
69 * @return the requested URI.
70 */
71 public URI getUri() {
72 return this.uri;
73 }
74
75 /**
76 * Returns the number of equivalent URIs.
77 *
78 * @return the number of equivalent URIs.
79 */
80 public int getAmount() {
81 return this.duplicates.size();
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 public Iterator<URI> iterator() {
88 return this.duplicates.iterator();
89 }
90
91 /**
92 * Adds an equivalent URI.
93 *
94 * @param uri an equivalent URI.
95 */
96 protected void addDuplicate(URI uri) {
97 this.duplicates.add(uri);
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public String toString() {
105 return String.format("Equivalence { uri: %s, amount: %s, duplicates: %s }",
106 this.uri, this.getAmount(), this.duplicates);
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 @Override
113 public boolean equals(Object object) {
114 if (this == object) {
115 return true;
116 }
117 if (object == null || this.getClass() != object.getClass()) {
118 return false;
119 }
120 return this.uri.equals(((Equivalence) object).getUri()) ;
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public int hashCode() {
128 return this.uri.hashCode();
129 }
130
131 }