5c657ac4d85be58b2dab7862b8519cac71cb66c2
[Sone.git] / src / main / java / net / pterodactylus / sone / data / TemporaryImage.java
1 /*
2  * Sone - TemporaryImage.java - Copyright © 2011–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
18 package net.pterodactylus.sone.data;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22
23 import java.util.UUID;
24
25 /**
26  * A temporary image stores an uploaded image in memory until it has been
27  * inserted into Freenet and is subsequently loaded from there.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class TemporaryImage {
32
33         /** The ID of the temporary image. */
34         private final String id;
35
36         /** The MIME type of the image. */
37         private String mimeType;
38
39         /** The encoded image data. */
40         private byte[] imageData;
41
42         /**
43          * Creates a new temporary image with a random ID.
44          */
45         public TemporaryImage() {
46                 this(UUID.randomUUID().toString());
47         }
48
49         /**
50          * Creates a new temporary image.
51          *
52          * @param id
53          *            The ID of the temporary image
54          */
55         public TemporaryImage(String id) {
56                 this.id = id;
57         }
58
59         /**
60          * Returns the ID of the temporary image.
61          *
62          * @return The ID of the temporary image
63          */
64         public String getId() {
65                 return id;
66         }
67
68         /**
69          * Returns the MIME type of the image.
70          *
71          * @return The MIME type of the image
72          */
73         public String getMimeType() {
74                 return mimeType;
75         }
76
77         /**
78          * Sets the MIME type of the image. The MIME type can only be set once and
79          * it must not be {@code null}.
80          *
81          * @param mimeType
82          *            The MIME type of the image
83          * @return This temporary image
84          */
85         public TemporaryImage setMimeType(String mimeType) {
86                 checkNotNull(mimeType, "mimeType must not be null");
87                 checkState(this.mimeType == null, "mime type must not already be set");
88                 this.mimeType = mimeType;
89                 return this;
90         }
91
92         /**
93          * Returns the encoded image data.
94          *
95          * @return The encoded image data
96          */
97         public byte[] getImageData() {
98                 return imageData;
99         }
100
101         /**
102          * Sets the encoded image data. The encoded image data can only be set once
103          * and it must not be {@code null}.
104          *
105          * @param imageData
106          *            The encoded image data
107          * @return This temporary image
108          */
109         public TemporaryImage setImageData(byte[] imageData) {
110                 checkNotNull(imageData, "imageData must not be null");
111                 checkState(this.imageData == null, "image data must not already be set");
112                 this.imageData = imageData;
113                 return this;
114         }
115
116 }