a2f1333123ddcca6c743d3ac5b9d3aaf54c5a95f
[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 boolean local;
24         protected long lastUpdated;
25         protected Client client;
26         protected final Collection<Post> posts = new HashSet<Post>();
27         protected final Collection<PostReply> postReplies = new HashSet<PostReply>();
28
29         @Override
30         public SoneBuilder from(Identity identity) {
31                 this.identity = identity;
32                 return this;
33         }
34
35         @Override
36         public SoneBuilder local() {
37                 this.local = true;
38                 return this;
39         }
40
41         @Override
42         public SoneBuilder lastUpdated(long lastUpdated) {
43                 this.lastUpdated = lastUpdated;
44                 return this;
45         }
46
47         @Override
48         public SoneBuilder using(Client client) {
49                 this.client = client;
50                 return this;
51         }
52
53         @Override
54         public SoneBuilder withPosts(Collection<Post> posts) {
55                 this.posts.clear();
56                 this.posts.addAll(posts);
57                 return this;
58         }
59
60         @Override
61         public SoneBuilder withPostReplies(Collection<PostReply> postReplies) {
62                 this.postReplies.clear();
63                 this.postReplies.addAll(postReplies);
64                 return this;
65         }
66
67         protected void validate() throws IllegalStateException {
68                 checkState(identity != null, "identity must not be null");
69                 checkState(!local || (identity instanceof OwnIdentity),
70                                 "can not create local Sone from remote identity");
71                 checkState(lastUpdated > 0, "last update time must be set");
72                 checkState(client != null, "client must not be null");
73         }
74
75 }