Rework the text parser.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / FreenetLinkPart.java
1 /*
2  * Sone - FreenetLinkPart.java - Copyright © 2011–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.text;
19
20 import static com.google.common.base.Objects.equal;
21 import static java.lang.String.format;
22
23 import com.google.common.base.Objects;
24
25 /**
26  * {@link LinkPart} implementation that stores an additional attribute: if the
27  * link is an SSK or USK link and the post was created by an identity that owns
28  * the keyspace in question.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class FreenetLinkPart extends LinkPart {
33
34         /** Whether the link is trusted. */
35         private final boolean trusted;
36
37         /**
38          * Creates a new freenet link part.
39          *
40          * @param link
41          *            The link of the part
42          * @param text
43          *            The text of the part
44          * @param trusted
45          *            {@code true} if the link is trusted, {@code false} otherwise
46          */
47         public FreenetLinkPart(String link, String text, boolean trusted) {
48                 this(link, text, text, trusted);
49         }
50
51         /**
52          * Creates a new freenet link part.
53          *
54          * @param link
55          *            The link of the part
56          * @param text
57          *            The text of the part
58          * @param title
59          *            The title of the part
60          * @param trusted
61          *            {@code true} if the link is trusted, {@code false} otherwise
62          */
63         public FreenetLinkPart(String link, String text, String title, boolean trusted) {
64                 super(link, text, title);
65                 this.trusted = trusted;
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         /**
73          * Returns whether the link is trusted.
74          *
75          * @return {@code true} if the link is trusted, {@code false} otherwise
76          */
77         public boolean isTrusted() {
78                 return trusted;
79         }
80
81         @Override
82         public boolean isFreenetLink() {
83                 return true;
84         }
85
86         @Override
87         public int hashCode() {
88                 return (getLink().hashCode() << 16) ^ (getText().hashCode() << 8 ) ^ getTitle().hashCode() ^ (isTrusted() ? -1 : 0);
89         }
90
91         @Override
92         public boolean equals(Object object) {
93                 if (!(object instanceof FreenetLinkPart)) {
94                         return false;
95                 }
96                 FreenetLinkPart freenetLinkPart = (FreenetLinkPart) object;
97                 return equal(getLink(), freenetLinkPart.getLink()) && equal(getText(), freenetLinkPart.getText()) && equal(getTitle(), freenetLinkPart.getTitle()) && (isTrusted() == freenetLinkPart.isTrusted());
98         }
99
100         @Override
101         public String toString() {
102                 return format("FreenetLink(link=%s, text=%s, title=%s, trusted=%s)", getLink(), getText(), getTitle(), isTrusted());
103         }
104
105 }