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