1692beea4a31469a829ec407b556ed69f8afdce3
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AbstractImageBuilder.java
1 /*
2  * Sone - AbstractImageBuilder.java - Copyright © 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 package net.pterodactylus.sone.data.impl;
18
19 import static com.google.common.base.Preconditions.checkState;
20 import static java.util.UUID.randomUUID;
21
22 import net.pterodactylus.sone.data.Sone;
23 import net.pterodactylus.sone.database.ImageBuilder;
24
25 /**
26  * Abstract {@link ImageBuilder} implementation. It stores the state of the new
27  * album and performs validation, you only need to implement {@link #build()}.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public abstract class AbstractImageBuilder implements ImageBuilder {
32
33         /** Whether to create an album with a random ID. */
34         protected boolean randomId;
35
36         /** The ID of the album to create. */
37         protected String id;
38         protected Sone sone;
39         protected long creationTime;
40         protected boolean createdNow;
41         protected String key;
42         protected int width;
43         protected int height;
44
45         @Override
46         public ImageBuilder randomId() {
47                 randomId = true;
48                 return this;
49         }
50
51         @Override
52         public ImageBuilder withId(String id) {
53                 this.id = id;
54                 return this;
55         }
56
57         @Override
58         public ImageBuilder by(Sone sone) {
59                 this.sone = sone;
60                 return this;
61         }
62
63         @Override
64         public ImageBuilder created(long creationTime) {
65                 this.creationTime = creationTime;
66                 return this;
67         }
68
69         @Override
70         public ImageBuilder createdNow() {
71                 createdNow = true;
72                 return this;
73         }
74
75         @Override
76         public ImageBuilder at(String key) {
77                 this.key = key;
78                 return this;
79         }
80
81         @Override
82         public ImageBuilder sized(int width, int height) {
83                 this.width = width;
84                 this.height = height;
85                 return this;
86         }
87
88         //
89         // PROTECTED METHODS
90         //
91
92         protected String getId() {
93                 return randomId ? randomUUID().toString() : id;
94         }
95
96         protected long getCreationTime() {
97                 return createdNow ? System.currentTimeMillis() : creationTime;
98         }
99
100         /**
101          * Validates the state of this image builder.
102          *
103          * @throws IllegalStateException
104          *              if the state is not valid for building a new image
105          */
106         protected void validate() throws IllegalStateException {
107                 checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
108                 checkState(sone != null, "sone must not be null");
109                 checkState((createdNow && (creationTime == 0)) || (!createdNow && (creationTime > 0)), "exactly one of created now or creation time must be set");
110                 checkState((width > 0) && (height > 0), "width and height must be set");
111         }
112
113 }