🔀 Merge branch “release-80”
[Sone.git] / src / main / java / net / pterodactylus / sone / data / TemporaryImage.java
1 /*
2  * Sone - TemporaryImage.java - Copyright © 2011–2019 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 public class TemporaryImage {
30
31         /** The ID of the temporary image. */
32         private final String id;
33
34         /** The MIME type of the image. */
35         private String mimeType;
36
37         /** The encoded image data. */
38         private byte[] imageData;
39
40         /**
41          * Creates a new temporary image with a random ID.
42          */
43         public TemporaryImage() {
44                 this(UUID.randomUUID().toString());
45         }
46
47         /**
48          * Creates a new temporary image.
49          *
50          * @param id
51          *            The ID of the temporary image
52          */
53         public TemporaryImage(String id) {
54                 this.id = id;
55         }
56
57         /**
58          * Returns the ID of the temporary image.
59          *
60          * @return The ID of the temporary image
61          */
62         public String getId() {
63                 return id;
64         }
65
66         /**
67          * Returns the MIME type of the image.
68          *
69          * @return The MIME type of the image
70          */
71         public String getMimeType() {
72                 return mimeType;
73         }
74
75         /**
76          * Sets the MIME type of the image. The MIME type can only be set once and
77          * it must not be {@code null}.
78          *
79          * @param mimeType
80          *            The MIME type of the image
81          * @return This temporary image
82          */
83         public TemporaryImage setMimeType(String mimeType) {
84                 checkNotNull(mimeType, "mimeType must not be null");
85                 checkState(this.mimeType == null, "mime type must not already be set");
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                 checkNotNull(imageData, "imageData must not be null");
109                 checkState(this.imageData == null, "image data must not already be set");
110                 this.imageData = imageData;
111                 return this;
112         }
113
114 }