Extract another super class that does not hold anything but primitives.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AbstractImage.java
1 /*
2  * Sone - AbstractImage.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
18 package net.pterodactylus.sone.data.impl;
19
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.base.Preconditions.checkState;
24
25 import net.pterodactylus.sone.data.Image;
26
27 import com.google.common.base.Optional;
28 import com.google.common.hash.Hasher;
29 import com.google.common.hash.Hashing;
30
31 /**
32  * Abstract {@link Image} implementation that contains only the attributes that
33  * are {@link String}s or primitives.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public abstract class AbstractImage implements Image {
38
39         protected final String id;
40         protected final long creationTime;
41         protected final int width;
42         protected final int height;
43         protected String key;
44         protected String title;
45         protected String description;
46
47         public AbstractImage(String id, String key, long creationTime, int width, int height) {
48                 this.id = checkNotNull(id, "id must not be null");
49                 this.key = key;
50                 this.creationTime = creationTime;
51                 this.width = width;
52                 this.height = height;
53         }
54
55         @Override
56         public String getId() {
57                 return id;
58         }
59
60         @Override
61         public String getKey() {
62                 return key;
63         }
64
65         @Override
66         public boolean isInserted() {
67                 return key != null;
68         }
69
70         @Override
71         public long getCreationTime() {
72                 return creationTime;
73         }
74
75         @Override
76         public int getWidth() {
77                 return width;
78         }
79
80         @Override
81         public int getHeight() {
82                 return height;
83         }
84
85         @Override
86         public String getTitle() {
87                 return title;
88         }
89
90         @Override
91         public String getDescription() {
92                 return description;
93         }
94
95         @Override
96         public Modifier modify() throws IllegalStateException {
97                 // TODO: reenable check for local images
98                 return new Modifier() {
99                         private Optional<String> key = absent();
100                         private Optional<String> title = absent();
101                         private Optional<String> description = absent();
102
103                         @Override
104                         public Modifier setKey(String key) {
105                                 this.key = fromNullable(key);
106                                 return this;
107                         }
108
109                         @Override
110                         public Modifier setTitle(String title) {
111                                 this.title = fromNullable(title);
112                                 return this;
113                         }
114
115                         @Override
116                         public Modifier setDescription(String description) {
117                                 this.description = fromNullable(description);
118                                 return this;
119                         }
120
121                         @Override
122                         public Image update() throws IllegalStateException {
123                                 checkState(!key.isPresent() || (AbstractImage.this.key == null), "key can not be changed");
124
125                                 if (key.isPresent()) {
126                                         AbstractImage.this.key = key.get();
127                                 }
128                                 if (title.isPresent()) {
129                                         AbstractImage.this.title = title.get();
130                                 }
131                                 if (description.isPresent()) {
132                                         AbstractImage.this.description = description.get();
133                                 }
134
135                                 return AbstractImage.this;
136                         }
137                 };
138         }
139
140         @Override
141         public String getFingerprint() {
142                 Hasher hash = Hashing.sha256().newHasher();
143                 hash.putString("Image(");
144                 hash.putString("ID(").putString(id).putString(")");
145                 hash.putString("Title(").putString(title).putString(")");
146                 hash.putString("Description(").putString(description).putString(")");
147                 hash.putString(")");
148                 return hash.hash().toString();
149         }
150
151         @Override
152         public int hashCode() {
153                 return id.hashCode();
154         }
155
156         @Override
157         public boolean equals(Object object) {
158                 if (!(object instanceof AbstractImage)) {
159                         return false;
160                 }
161                 return ((AbstractImage) object).id.equals(id);
162         }
163
164 }