Use different method to create a local Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AbstractSoneBuilder.java
1 package net.pterodactylus.sone.data.impl;
2
3 import static com.google.common.base.Preconditions.checkState;
4
5 import java.util.Collection;
6 import java.util.HashSet;
7
8 import net.pterodactylus.sone.data.Client;
9 import net.pterodactylus.sone.data.Post;
10 import net.pterodactylus.sone.data.PostReply;
11 import net.pterodactylus.sone.database.SoneBuilder;
12 import net.pterodactylus.sone.freenet.wot.Identity;
13 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
14
15 /**
16  * Abstract {@link SoneBuilder} implementation.
17  *
18  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
19  */
20 public abstract class AbstractSoneBuilder implements SoneBuilder {
21
22         protected Identity identity;
23         protected long lastUpdated;
24         protected Client client;
25         protected final Collection<Post> posts = new HashSet<Post>();
26         protected final Collection<PostReply> postReplies = new HashSet<PostReply>();
27
28         @Override
29         public SoneBuilder from(Identity identity) {
30                 this.identity = identity;
31                 return this;
32         }
33
34         @Override
35         public SoneBuilder lastUpdated(long lastUpdated) {
36                 this.lastUpdated = lastUpdated;
37                 return this;
38         }
39
40         @Override
41         public SoneBuilder using(Client client) {
42                 this.client = client;
43                 return this;
44         }
45
46         @Override
47         public SoneBuilder withPosts(Collection<Post> posts) {
48                 this.posts.clear();
49                 this.posts.addAll(posts);
50                 return this;
51         }
52
53         @Override
54         public SoneBuilder withPostReplies(Collection<PostReply> postReplies) {
55                 this.postReplies.clear();
56                 this.postReplies.addAll(postReplies);
57                 return this;
58         }
59
60         protected void validate() throws IllegalStateException {
61                 checkState(identity != null, "identity must not be null");
62                 checkState(lastUpdated > 0, "last update time must be set");
63                 checkState(client != null, "client must not be null");
64         }
65
66         protected void validateLocal() throws IllegalStateException {
67                 validate();
68                 checkState(identity instanceof OwnIdentity,
69                                 "identity must be an own identity for a local Sone");
70         }
71
72 }