Set client in Sone builder.
[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 net.pterodactylus.sone.data.Client;
6 import net.pterodactylus.sone.database.SoneBuilder;
7 import net.pterodactylus.sone.freenet.wot.Identity;
8 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
9
10 /**
11  * Abstract {@link SoneBuilder} implementation.
12  *
13  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
14  */
15 public abstract class AbstractSoneBuilder implements SoneBuilder {
16
17         protected Identity identity;
18         protected boolean local;
19         protected long lastUpdated;
20         protected Client client;
21
22         @Override
23         public SoneBuilder from(Identity identity) {
24                 this.identity = identity;
25                 return this;
26         }
27
28         @Override
29         public SoneBuilder local() {
30                 this.local = true;
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         protected void validate() throws IllegalStateException {
47                 checkState(identity != null, "identity must not be null");
48                 checkState(!local || (identity instanceof OwnIdentity),
49                                 "can not create local Sone from remote identity");
50                 checkState(lastUpdated > 0, "last update time must be set");
51                 checkState(client != null, "client must not be null");
52         }
53
54 }