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