Don’t hand in null to function not allowing it
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryPost.java
1 /*
2  * Sone - MemoryPost.java - Copyright © 2010–2016 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.database.memory;
19
20 import static com.google.common.base.Optional.fromNullable;
21
22 import java.util.UUID;
23
24 import javax.annotation.Nullable;
25
26 import net.pterodactylus.sone.data.Post;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.database.SoneProvider;
29
30 import com.google.common.base.Function;
31 import com.google.common.base.Optional;
32
33 /**
34  * A post is a short message that a user writes in his Sone to let other users
35  * know what is going on.
36  */
37 class MemoryPost implements Post {
38
39         /** The post database. */
40         private final MemoryDatabase postDatabase;
41
42         /** The Sone provider. */
43         private final SoneProvider soneProvider;
44
45         /** The GUID of the post. */
46         private final UUID id;
47
48         /** The ID of the owning Sone. */
49         private final String soneId;
50
51         /** The ID of the recipient Sone. */
52         private final String recipientId;
53
54         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
55         private final long time;
56
57         /** The text of the post. */
58         private final String text;
59
60         /**
61          * Creates a new post.
62          *
63          * @param postDatabase
64          *            The post database
65          * @param soneProvider
66          *            The Sone provider
67          * @param id
68          *            The ID of the post
69          * @param soneId
70          *            The ID of the Sone this post belongs to
71          * @param recipientId
72          *            The ID of the recipient of the post
73          * @param time
74          *            The time of the post (in milliseconds since Jan 1, 1970 UTC)
75          * @param text
76          *            The text of the post
77          */
78         public MemoryPost(MemoryDatabase postDatabase, SoneProvider soneProvider, String id, String soneId, String recipientId, long time, String text) {
79                 this.postDatabase = postDatabase;
80                 this.soneProvider = soneProvider;
81                 this.id = UUID.fromString(id);
82                 this.soneId = soneId;
83                 this.recipientId = recipientId;
84                 this.time = time;
85                 this.text = text;
86         }
87
88         //
89         // ACCESSORS
90         //
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         public String getId() {
97                 return id.toString();
98         }
99
100         @Override
101         public boolean isLoaded() {
102                 return true;
103         }
104
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public Sone getSone() {
110                 return soneProvider.getSone(soneId);
111         }
112
113         /**
114          * {@inheritDocs}
115          */
116         @Override
117         public Optional<String> getRecipientId() {
118                 return fromNullable(recipientId);
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public Optional<Sone> getRecipient() {
126                 return Optional.fromNullable(recipientId).transform(new Function<String, Sone>() {
127                         @Nullable
128                         @Override
129                         public Sone apply(String input) {
130                                 return soneProvider.getSone(input);
131                         }
132                 });
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         @Override
139         public long getTime() {
140                 return time;
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public String getText() {
148                 return text;
149         }
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         public boolean isKnown() {
156                 return postDatabase.isPostKnown(this);
157         }
158
159         /**
160          * {@inheritDoc}
161          */
162         @Override
163         public MemoryPost setKnown(boolean known) {
164                 postDatabase.setPostKnown(this, known);
165                 return this;
166         }
167
168         //
169         // OBJECT METHODS
170         //
171
172         /**
173          * {@inheritDoc}
174          */
175         @Override
176         public int hashCode() {
177                 return id.hashCode();
178         }
179
180         /**
181          * {@inheritDoc}
182          */
183         @Override
184         public boolean equals(Object object) {
185                 if (!(object instanceof MemoryPost)) {
186                         return false;
187                 }
188                 MemoryPost post = (MemoryPost) object;
189                 return post.id.equals(id);
190         }
191
192         /**
193          * {@inheritDoc}
194          */
195         @Override
196         public String toString() {
197                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
198         }
199
200 }