logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
return;
}
- Image image = getImage(imageId).modify().setSone(sone).setCreationTime(creationTime).setKey(key).setTitle(title).setDescription(description).setWidth(width).setHeight(height).update();
- album.addImage(image);
+ album.newImageBuilder().withId(imageId).by(sone).created(creationTime).at(key).sized(width, height).build().modify().setTitle(title).setDescription(description).update();
}
/* load avatar. */
checkNotNull(temporaryImage, "temporaryImage must not be null");
checkArgument(sone.isLocal(), "sone must be a local Sone");
checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
- Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
- album.addImage(image);
+ Image image = album.newImageBuilder().withId(temporaryImage.getId()).by(sone).createdNow().sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build();
database.storeImage(image);
imageInserter.insertImage(temporaryImage, image);
return image;
logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
return null;
}
- Image image = core.getImage(imageId).modify().setSone(sone).setKey(imageKey).setCreationTime(creationTime).update();
+ Image image = album.newImageBuilder().withId(imageId).by(sone).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build();
image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
- image = image.modify().setWidth(imageWidth).setHeight(imageHeight).update();
- album.addImage(image);
}
}
album.modify().setAlbumImage(albumImageId).update();
import java.util.List;
import javax.annotation.Nonnull;
+import net.pterodactylus.sone.database.ImageBuilder;
+
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
List<Image> getImages();
/**
- * Adds the given image to this album.
- *
- * @param image
- * The image to add
- */
- void addImage(Image image);
-
- /**
* Removes the given image from this album.
*
* @param image
*/
String getDescription();
+ ImageBuilder newImageBuilder() throws IllegalStateException;
+
/**
* Returns a modifier for this album.
*
import java.util.Map;
import java.util.UUID;
+import net.pterodactylus.sone.data.impl.ImageBuilderImpl;
+import net.pterodactylus.sone.database.ImageBuilder;
+
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
}
@Override
- public void addImage(Image image) {
- checkNotNull(image, "image must not be null");
- checkNotNull(image.getSone(), "image must have an owner");
- checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
- if (image.getAlbum() != null) {
- image.getAlbum().removeImage(image);
- }
- image.setAlbum(this);
- if (imageIds.isEmpty() && (albumImage == null)) {
- albumImage = image.getId();
- }
- if (!imageIds.contains(image.getId())) {
- imageIds.add(image.getId());
- images.put(image.getId(), image);
- }
- }
-
- @Override
public void removeImage(Image image) {
checkNotNull(image, "image must not be null");
checkNotNull(image.getSone(), "image must have an owner");
}
@Override
+ public ImageBuilder newImageBuilder() throws IllegalStateException {
+ return new ImageBuilderImpl() {
+ @Override
+ public Image build() throws IllegalStateException {
+ Image image = super.build();
+ if (images.isEmpty() && (albumImage == null)) {
+ albumImage = image.getId();
+ }
+ images.put(image.getId(), image);
+ imageIds.add(image.getId());
+ return image;
+ }
+ };
+ }
+
+ @Override
public Modifier modify() throws IllegalStateException {
// TODO: reenable check for local Sones
return new Modifier() {
Album getAlbum();
/**
- * Sets the album this image belongs to. The album of an image can only be
- * set once, and it is usually called by {@link Album#addImage(Image)}.
- *
- * @param album
- * The album this image belongs to
- * @return This image
- */
- Image setAlbum(Album album);
-
- /**
* Returns the request key of this image.
*
* @return The request key of this image
interface Modifier {
- Modifier setSone(Sone sone);
-
- Modifier setCreationTime(long creationTime);
-
Modifier setKey(String key);
Modifier setTitle(String title);
Modifier setDescription(String description);
- Modifier setWidth(int width);
-
- Modifier setHeight(int height);
-
Image update() throws IllegalStateException;
}
import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
-import static com.google.common.base.Optional.of;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
private final String id;
/** The Sone the image belongs to. */
- private Sone sone;
+ private final Sone sone;
/** The album this image belongs to. */
private Album album;
private String key;
/** The creation time of the image. */
- private long creationTime;
+ private final long creationTime;
/** The width of the image. */
- private int width;
+ private final int width;
/** The height of the image. */
- private int height;
+ private final int height;
/** The title of the image. */
private String title;
private String description;
/** Creates a new image with a random ID. */
- public ImageImpl() {
- this(UUID.randomUUID().toString());
- this.creationTime = System.currentTimeMillis();
+ public ImageImpl(Sone sone, long creationTime, String key, int width, int height) {
+ this(UUID.randomUUID().toString(), sone, creationTime, key, width, height);
}
/**
*
* @param id
* The ID of the image
+ * @param creationTime
*/
- public ImageImpl(String id) {
+ public ImageImpl(String id, Sone sone, long creationTime, String key, int width, int height) {
this.id = checkNotNull(id, "id must not be null");
+ this.sone = sone;
+ this.creationTime = creationTime;
+ this.key = key;
+ this.width = width;
+ this.height = height;
}
//
}
@Override
- public Image setAlbum(Album album) {
- checkNotNull(album, "album must not be null");
- checkNotNull(album.getSone().equals(getSone()), "album must belong to the same Sone as this image");
- this.album = album;
- return this;
- }
-
- @Override
public String getKey() {
return key;
}
public Modifier modify() throws IllegalStateException {
// TODO: reenable check for local images
return new Modifier() {
- private Optional<Sone> sone = absent();
-
- private Optional<Long> creationTime = absent();
-
private Optional<String> key = absent();
-
private Optional<String> title = absent();
-
private Optional<String> description = absent();
- private Optional<Integer> width = absent();
-
- private Optional<Integer> height = absent();
-
- @Override
- public Modifier setSone(Sone sone) {
- this.sone = fromNullable(sone);
- return this;
- }
-
- @Override
- public Modifier setCreationTime(long creationTime) {
- this.creationTime = of(creationTime);
- return this;
- }
-
@Override
public Modifier setKey(String key) {
this.key = fromNullable(key);
}
@Override
- public Modifier setWidth(int width) {
- this.width = of(width);
- return this;
- }
-
- @Override
- public Modifier setHeight(int height) {
- this.height = of(height);
- return this;
- }
-
- @Override
public Image update() throws IllegalStateException {
- checkState(!sone.isPresent() || (ImageImpl.this.sone == null) || sone.get().equals(ImageImpl.this.sone), "can not change Sone once set");
- checkState(!creationTime.isPresent() || ((ImageImpl.this.creationTime == 0) || (ImageImpl.this.creationTime == creationTime.get())), "can not change creation time once set");
- checkState(!key.isPresent() || (ImageImpl.this.key == null) || key.get().equals(ImageImpl.this.key), "can not change key once set");
- checkState(!width.isPresent() || (ImageImpl.this.width == 0) || width.get().equals(ImageImpl.this.width), "can not change width once set");
- checkState(!height.isPresent() || (ImageImpl.this.height == 0) || height.get().equals(ImageImpl.this.height), "can not change height once set");
-
- if (sone.isPresent()) {
- ImageImpl.this.sone = sone.get();
- }
- if (creationTime.isPresent()) {
- ImageImpl.this.creationTime = creationTime.get();
- }
+ checkState(!key.isPresent() || (ImageImpl.this.key == null), "key can not be changed");
+
if (key.isPresent()) {
ImageImpl.this.key = key.get();
}
if (description.isPresent()) {
ImageImpl.this.description = description.get();
}
- if (width.isPresent()) {
- ImageImpl.this.width = width.get();
- }
- if (height.isPresent()) {
- ImageImpl.this.height = height.get();
- }
return ImageImpl.this;
}
import static com.google.common.base.Preconditions.checkState;
+import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.database.ImageBuilder;
/**
/** The ID of the album to create. */
protected String id;
+ protected Sone sone;
+ protected long creationTime;
+ protected boolean createdNow;
+ protected String key;
+ protected int width;
+ protected int height;
@Override
public ImageBuilder randomId() {
return this;
}
+ @Override
+ public ImageBuilder by(Sone sone) {
+ this.sone = sone;
+ return this;
+ }
+
+ @Override
+ public ImageBuilder created(long creationTime) {
+ this.creationTime = creationTime;
+ return this;
+ }
+
+ @Override
+ public ImageBuilder createdNow() {
+ createdNow = true;
+ return this;
+ }
+
+ @Override
+ public ImageBuilder at(String key) {
+ this.key = key;
+ return this;
+ }
+
+ @Override
+ public ImageBuilder sized(int width, int height) {
+ this.width = width;
+ this.height = height;
+ return this;
+ }
+
//
// PROTECTED METHODS
//
*/
protected void validate() throws IllegalStateException {
checkState((randomId && (id == null)) || (!randomId && (id != null)), "exactly one of random ID or custom ID must be set");
+ checkState(sone != null, "sone must not be null");
+ checkState((createdNow && (creationTime == 0)) || (!createdNow && (creationTime > 0)), "exactly one of created now or creation time must be set");
+ checkState((width > 0) && (height > 0), "width and height must be set");
}
}
package net.pterodactylus.sone.data.impl;
+import static java.util.UUID.randomUUID;
+
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.ImageImpl;
import net.pterodactylus.sone.database.ImageBuilder;
@Override
public Image build() throws IllegalStateException {
validate();
- return randomId ? new ImageImpl() : new ImageImpl(id);
+ String id = randomId ? randomUUID().toString() : this.id;
+ long creationTime = createdNow ? System.currentTimeMillis() : this.creationTime;
+ return new ImageImpl(id, sone, creationTime, key, width, height);
}
}
package net.pterodactylus.sone.database;
import net.pterodactylus.sone.data.Image;
+import net.pterodactylus.sone.data.Sone;
/**
* Builder for {@link Image} objects.
public interface ImageBuilder {
ImageBuilder randomId();
-
ImageBuilder withId(String id);
+ ImageBuilder by(Sone sone);
+ ImageBuilder created(long creationTime);
+ ImageBuilder createdNow();
+ ImageBuilder at(String key);
+ ImageBuilder sized(int width, int height);
Image build() throws IllegalStateException;