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