From: David ‘Bombe’ Roden Date: Sat, 12 Oct 2013 14:01:55 +0000 (+0200) Subject: Extract album baseclass that only stores the primitives. X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=ff08aebea0013faaa8fb26ecc5b494eb4d1cdffc Extract album baseclass that only stores the primitives. --- diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbum.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbum.java new file mode 100644 index 0000000..dce51ad --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractAlbum.java @@ -0,0 +1,159 @@ +/* + * Sone - AbstractAlbum.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data.impl; + +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.base.Preconditions.checkNotNull; + +import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; + +import com.google.common.base.Optional; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; + +/** + * Abstract {@link Album} implementation that contains only the attributes that + * are {@link String}s or primitives. + * + * @author David ‘Bombe’ Roden + */ +public abstract class AbstractAlbum implements Album { + + protected final String id; + protected String title; + protected String description; + protected String albumImage; + + protected AbstractAlbum(String id) { + this.id = checkNotNull(id, "id must not be null"); + } + + @Override + public String getId() { + return id; + } + + @Override + public boolean isEmpty() { + return getAlbums().isEmpty() && getImages().isEmpty(); + } + + @Override + public boolean isRoot() { + return getParent() == null; + } + + @Override + public String getTitle() { + return title; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public Modifier modify() throws IllegalStateException { + // TODO: reenable check for local Sones + return new Modifier() { + private Optional title = absent(); + private Optional description = absent(); + private Optional albumImage = absent(); + + @Override + public Modifier setTitle(String title) { + this.title = fromNullable(title); + return this; + } + + @Override + public Modifier setDescription(String description) { + this.description = fromNullable(description); + return this; + } + + @Override + public Modifier setAlbumImage(String imageId) { + this.albumImage = fromNullable(imageId); + return this; + } + + @Override + public Album update() throws IllegalStateException { + if (title.isPresent()) { + AbstractAlbum.this.title = title.get(); + } + if (description.isPresent()) { + AbstractAlbum.this.description = description.get(); + } + if (albumImage.isPresent()) { + AbstractAlbum.this.albumImage = albumImage.get(); + } + return AbstractAlbum.this; + } + }; + } + + @Override + public String getFingerprint() { + Hasher hash = Hashing.sha256().newHasher(); + hash.putString("Album("); + hash.putString("ID(").putString(id).putString(")"); + hash.putString("Title(").putString(title).putString(")"); + hash.putString("Description(").putString(description).putString(")"); + if (albumImage != null) { + hash.putString("AlbumImage(").putString(albumImage).putString(")"); + } + + /* add nested albums. */ + hash.putString("Albums("); + for (Album album : getAlbums()) { + hash.putString(album.getFingerprint()); + } + hash.putString(")"); + + /* add images. */ + hash.putString("Images("); + for (Image image : getImages()) { + if (image.isInserted()) { + hash.putString(image.getFingerprint()); + } + } + hash.putString(")"); + + hash.putString(")"); + return hash.hash().toString(); + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof DefaultAlbum)) { + return false; + } + DefaultAlbum album = (DefaultAlbum) object; + return id.equals(album.id); + } +} diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java index 7805bf4..6992a35 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -17,8 +17,6 @@ package net.pterodactylus.sone.data.impl; -import static com.google.common.base.Optional.absent; -import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; @@ -38,18 +36,13 @@ import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; -import com.google.common.hash.Hasher; -import com.google.common.hash.Hashing; /** - * Container for images that can also contain nested {@link Album}s. + * Dumb, store-everything-in-memory implementation of an {@link Album}. * * @author David ‘Bombe’ Roden */ -public class DefaultAlbum implements Album { - - /** The ID of this album. */ - private final String id; +public class DefaultAlbum extends AbstractAlbum { /** The Sone this album belongs to. */ private Sone sone; @@ -66,15 +59,6 @@ public class DefaultAlbum implements Album { /** The parent album. */ private Album parent; - /** The title of this album. */ - private String title; - - /** The description of this album. */ - private String description; - - /** The ID of the album picture. */ - private String albumImage; - /** Creates a new album with a random ID. */ public DefaultAlbum() { this(UUID.randomUUID().toString()); @@ -87,7 +71,7 @@ public class DefaultAlbum implements Album { * The ID of the album */ public DefaultAlbum(String id) { - this.id = checkNotNull(id, "id must not be null"); + super(id); } // @@ -95,11 +79,6 @@ public class DefaultAlbum implements Album { // @Override - public String getId() { - return id; - } - - @Override public Sone getSone() { return sone; } @@ -231,16 +210,6 @@ public class DefaultAlbum implements Album { } @Override - public boolean isEmpty() { - return albums.isEmpty() && images.isEmpty(); - } - - @Override - public boolean isRoot() { - return parent == null; - } - - @Override public Album getParent() { return parent; } @@ -258,16 +227,6 @@ public class DefaultAlbum implements Album { } @Override - public String getTitle() { - return title; - } - - @Override - public String getDescription() { - return description; - } - - @Override public ImageBuilder newImageBuilder() throws IllegalStateException { return new DefaultImageBuilder(sone, this) { @Override @@ -283,101 +242,4 @@ public class DefaultAlbum implements Album { }; } - @Override - public Modifier modify() throws IllegalStateException { - // TODO: reenable check for local Sones - return new Modifier() { - private Optional title = absent(); - - private Optional description = absent(); - - private Optional albumImage = absent(); - - @Override - public Modifier setTitle(String title) { - this.title = fromNullable(title); - return this; - } - - @Override - public Modifier setDescription(String description) { - this.description = fromNullable(description); - return this; - } - - @Override - public Modifier setAlbumImage(String imageId) { - this.albumImage = fromNullable(imageId); - return this; - } - - @Override - public Album update() throws IllegalStateException { - if (title.isPresent()) { - DefaultAlbum.this.title = title.get(); - } - if (description.isPresent()) { - DefaultAlbum.this.description = description.get(); - } - if (albumImage.isPresent()) { - DefaultAlbum.this.albumImage = albumImage.get(); - } - return DefaultAlbum.this; - } - }; - } - - // - // FINGERPRINTABLE METHODS - // - - @Override - public String getFingerprint() { - Hasher hash = Hashing.sha256().newHasher(); - hash.putString("Album("); - hash.putString("ID(").putString(id).putString(")"); - hash.putString("Title(").putString(title).putString(")"); - hash.putString("Description(").putString(description).putString(")"); - if (albumImage != null) { - hash.putString("AlbumImage(").putString(albumImage).putString(")"); - } - - /* add nested albums. */ - hash.putString("Albums("); - for (Album album : albums) { - hash.putString(album.getFingerprint()); - } - hash.putString(")"); - - /* add images. */ - hash.putString("Images("); - for (Image image : getImages()) { - if (image.isInserted()) { - hash.putString(image.getFingerprint()); - } - } - hash.putString(")"); - - hash.putString(")"); - return hash.hash().toString(); - } - - // - // OBJECT METHODS - // - - @Override - public int hashCode() { - return id.hashCode(); - } - - @Override - public boolean equals(Object object) { - if (!(object instanceof DefaultAlbum)) { - return false; - } - DefaultAlbum album = (DefaultAlbum) object; - return id.equals(album.id); - } - }