Rework the text parser.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / LinkPart.java
1 /*
2  * Sone - LinkPart.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
22 /**
23  * {@link Part} implementation that can hold a link. A link contains of three
24  * attributes: the link itself, the text that is shown instead of the link, and
25  * an explanatory text that can be displayed e.g. as a tooltip.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class LinkPart implements Part {
30
31         /** The link of this part. */
32         private final String link;
33
34         /** The text of this part. */
35         private final String text;
36
37         /** The title of this part. */
38         private final String title;
39
40         /**
41          * Creates a new link part.
42          *
43          * @param link
44          *            The link of the link part
45          * @param text
46          *            The text of the link part
47          */
48         public LinkPart(String link, String text) {
49                 this(link, text, text);
50         }
51
52         /**
53          * Creates a new link part.
54          *
55          * @param link
56          *            The link of the link part
57          * @param text
58          *            The text of the link part
59          * @param title
60          *            The title of the link part
61          */
62         public LinkPart(String link, String text, String title) {
63                 this.link = link;
64                 this.text = text;
65                 this.title = title;
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         /**
73          * Returns the link of this part.
74          *
75          * @return The link of this part
76          */
77         public String getLink() {
78                 return link;
79         }
80
81         /**
82          * Returns the title of this part.
83          *
84          * @return The title of this part
85          */
86         public String getTitle() {
87                 return title;
88         }
89
90         @Override
91         public boolean isPlainText() {
92                 return false;
93         }
94
95         @Override
96         public boolean isFreenetLink() {
97                 return false;
98         }
99
100         //
101         // PART METHODS
102         //
103
104         @Override
105         public String getText() {
106                 return text;
107         }
108
109         @Override
110         public int hashCode() {
111                 return getLink().hashCode() ^ (getTitle().hashCode() << 16) ^ getText().hashCode();
112         }
113
114         @Override
115         public boolean equals(Object object) {
116                 if (!(object instanceof LinkPart)) {
117                         return false;
118                 }
119                 LinkPart linkPart = (LinkPart) object;
120                 return equal(getLink(), linkPart.getLink()) && equal(getText(), linkPart.getText()) && equal(getTitle(), linkPart.getTitle());
121         }
122
123 }