Use Sone builder to set the posts of a 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.database.SoneBuilder;
11 import net.pterodactylus.sone.freenet.wot.Identity;
12 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
13
14 /**
15  * Abstract {@link SoneBuilder} implementation.
16  *
17  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
18  */
19 public abstract class AbstractSoneBuilder implements SoneBuilder {
20
21         protected Identity identity;
22         protected boolean local;
23         protected long lastUpdated;
24         protected Client client;
25         protected final Collection<Post> posts = new HashSet<Post>();
26
27         @Override
28         public SoneBuilder from(Identity identity) {
29                 this.identity = identity;
30                 return this;
31         }
32
33         @Override
34         public SoneBuilder local() {
35                 this.local = true;
36                 return this;
37         }
38
39         @Override
40         public SoneBuilder lastUpdated(long lastUpdated) {
41                 this.lastUpdated = lastUpdated;
42                 return this;
43         }
44
45         @Override
46         public SoneBuilder using(Client client) {
47                 this.client = client;
48                 return this;
49         }
50
51         @Override
52         public SoneBuilder withPosts(Collection<Post> posts) {
53                 this.posts.clear();
54                 this.posts.addAll(posts);
55                 return this;
56         }
57
58         protected void validate() throws IllegalStateException {
59                 checkState(identity != null, "identity must not be null");
60                 checkState(!local || (identity instanceof OwnIdentity),
61                                 "can not create local Sone from remote identity");
62                 checkState(lastUpdated > 0, "last update time must be set");
63                 checkState(client != null, "client must not be null");
64         }
65
66 }