Don’t store sone provider in abstract post builder.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryPost.java
1 /*
2  * Sone - PostImpl.java - Copyright © 2010–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.database.memory;
19
20 import java.util.UUID;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.Sone;
24
25 import com.google.common.base.Optional;
26
27 /**
28  * A post is a short message that a user writes in his Sone to let other users
29  * know what is going on.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 class MemoryPost implements Post {
34
35         /** The post database. */
36         private final MemoryDatabase memoryDatabase;
37
38         /** The GUID of the post. */
39         private final UUID id;
40
41         /** The ID of the owning Sone. */
42         private final String soneId;
43
44         /** The ID of the recipient Sone. */
45         private final String recipientId;
46
47         /** The time of the post (in milliseconds since Jan 1, 1970 UTC). */
48         private final long time;
49
50         /** The text of the post. */
51         private final String text;
52
53         /**
54          * Creates a new post.
55          *
56          * @param memoryDatabase
57          *              The database
58          * @param id
59          *              The ID of the post
60          * @param soneId
61          *              The ID of the Sone this post belongs to
62          * @param recipientId
63          *              The ID of the recipient of the post
64          * @param time
65          *              The time of the post (in milliseconds since Jan 1, 1970 UTC)
66          * @param text
67          *              The text of the post
68          */
69         public MemoryPost(MemoryDatabase memoryDatabase, String id, String soneId, String recipientId, long time, String text) {
70                 this.memoryDatabase = memoryDatabase;
71                 this.id = UUID.fromString(id);
72                 this.soneId = soneId;
73                 this.recipientId = recipientId;
74                 this.time = time;
75                 this.text = text;
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         /** {@inheritDoc} */
83         @Override
84         public String getId() {
85                 return id.toString();
86         }
87
88         /** {@inheritDoc} */
89         @Override
90         public Sone getSone() {
91                 return memoryDatabase.getSone(soneId).get();
92         }
93
94         /** {@inheritDocs} */
95         @Override
96         public Optional<String> getRecipientId() {
97                 return Optional.fromNullable(recipientId);
98         }
99
100         /** {@inheritDoc} */
101         @Override
102         public Optional<Sone> getRecipient() {
103                 return memoryDatabase.getSone(recipientId);
104         }
105
106         /** {@inheritDoc} */
107         @Override
108         public long getTime() {
109                 return time;
110         }
111
112         /** {@inheritDoc} */
113         @Override
114         public String getText() {
115                 return text;
116         }
117
118         /** {@inheritDoc} */
119         @Override
120         public boolean isKnown() {
121                 return memoryDatabase.isPostKnown(this);
122         }
123
124         /** {@inheritDoc} */
125         @Override
126         public MemoryPost setKnown(boolean known) {
127                 memoryDatabase.setPostKnown(this, known);
128                 return this;
129         }
130
131         //
132         // OBJECT METHODS
133         //
134
135         /** {@inheritDoc} */
136         @Override
137         public int hashCode() {
138                 return id.hashCode();
139         }
140
141         /** {@inheritDoc} */
142         @Override
143         public boolean equals(Object object) {
144                 if (!(object instanceof MemoryPost)) {
145                         return false;
146                 }
147                 MemoryPost post = (MemoryPost) object;
148                 return post.id.equals(id);
149         }
150
151         /** {@inheritDoc} */
152         @Override
153         public String toString() {
154                 return String.format("%s[id=%s,sone=%s,recipient=%s,time=%d,text=%s]", getClass().getName(), id, soneId, recipientId, time, text);
155         }
156
157 }