From 28b2e2b0d2ff2320f5b6a5ac36a858a604c7c7ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 13 Oct 2013 02:42:53 +0200 Subject: [PATCH] Return an Optional for the album image. --- src/main/java/net/pterodactylus/sone/core/Core.java | 3 ++- src/main/java/net/pterodactylus/sone/data/Album.java | 9 ++------- src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java | 8 +++++--- src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java | 2 ++ 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index e61a58f..5dc733b 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -19,6 +19,7 @@ package net.pterodactylus.sone.core; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static net.pterodactylus.sone.data.Identified.GET_ID; import java.net.MalformedURLException; import java.util.Collection; @@ -1724,7 +1725,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle()); configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription()); configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId()); - configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId()); + configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage().transform(GET_ID).orNull()); } configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null); diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index e8efb59..a173e19 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -30,6 +30,7 @@ import net.pterodactylus.sone.database.AlbumBuilder; import net.pterodactylus.sone.database.ImageBuilder; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; @@ -127,13 +128,7 @@ public interface Album extends Identified, Fingerprintable { */ List getImages(); - /** - * Returns the album image of this album, or {@code null} if no album image has - * been set. - * - * @return The image to show when this album is listed - */ - Image getAlbumImage(); + Optional getAlbumImage(); /** * Returns whether this album contains any other albums or images. 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 32052d5..7102bfe 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -17,6 +17,8 @@ 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.checkState; import java.util.ArrayList; @@ -102,11 +104,11 @@ public class DefaultAlbum extends AbstractAlbum { } @Override - public Image getAlbumImage() { + public Optional getAlbumImage() { if (albumImage == null) { - return null; + return absent(); } - return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next()); + return fromNullable(fromNullable(images.get(albumImage)).or(images.values().iterator().next())); } @Override diff --git a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java index 9ef168a..51cd974 100644 --- a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java @@ -49,6 +49,8 @@ public class AlbumAccessor extends ReflectionAccessor { } backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone()))); return backlinks; + } else if ("albumImage".equals(member)) { + return album.getAlbumImage().orNull(); } return super.get(templateContext, object, member); } -- 2.7.4