From: David ‘Bombe’ Roden Date: Mon, 15 Jul 2013 05:12:45 +0000 (+0200) Subject: Add and implement Identified interface. X-Git-Tag: 0.8.7^2~31 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f855be86d4d02a29c4603522c793a3b0eecc961c;p=Sone.git Add and implement Identified interface. --- diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index c47d83f..0c8780c 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -44,7 +44,7 @@ import com.google.common.hash.Hashing; * * @author David ‘Bombe’ Roden */ -public class Album implements Fingerprintable { +public class Album implements Identified, Fingerprintable { /** Compares two {@link Album}s by {@link #getTitle()}. */ public static final Comparator TITLE_COMPARATOR = new Comparator() { diff --git a/src/main/java/net/pterodactylus/sone/data/Identified.java b/src/main/java/net/pterodactylus/sone/data/Identified.java new file mode 100644 index 0000000..f12f842 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/Identified.java @@ -0,0 +1,46 @@ +/* + * Sone - Identified.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; + +import com.google.common.base.Function; +import com.google.common.base.Optional; + +/** + * Interface for all objects that expose an ID. + * + * @author David ‘Bombe’ Roden + */ +public interface Identified { + + /** Function to extract the ID from an optional. */ + public static final Function, Optional> GET_ID = new Function, Optional>() { + + @Override + public Optional apply(Optional identified) { + return identified.isPresent() ? Optional.of(identified.get().getId()) : Optional.absent(); + } + }; + + /** + * Returns the ID of this element. + * + * @return The ID of this element + */ + public String getId(); + +} diff --git a/src/main/java/net/pterodactylus/sone/data/Image.java b/src/main/java/net/pterodactylus/sone/data/Image.java index 952d540..d8a8ab1 100644 --- a/src/main/java/net/pterodactylus/sone/data/Image.java +++ b/src/main/java/net/pterodactylus/sone/data/Image.java @@ -31,7 +31,7 @@ import com.google.common.hash.Hashing; * * @author David ‘Bombe’ Roden */ -public class Image implements Fingerprintable { +public class Image implements Identified, Fingerprintable { /** The ID of the image. */ private final String id; diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 6523e9b..4860a01 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -28,7 +28,7 @@ import com.google.common.base.Predicate; * * @author David ‘Bombe’ Roden */ -public interface Post { +public interface Post extends Identified { /** Comparator for posts, sorts descending by time. */ public static final Comparator TIME_COMPARATOR = new Comparator() { diff --git a/src/main/java/net/pterodactylus/sone/data/Reply.java b/src/main/java/net/pterodactylus/sone/data/Reply.java index a686023..60e5eeb 100644 --- a/src/main/java/net/pterodactylus/sone/data/Reply.java +++ b/src/main/java/net/pterodactylus/sone/data/Reply.java @@ -28,7 +28,7 @@ import com.google.common.base.Predicate; * The type of the reply * @author David ‘Bombe’ Roden */ -public interface Reply> { +public interface Reply> extends Identified { /** Comparator that sorts replies ascending by time. */ public static final Comparator> TIME_COMPARATOR = new Comparator>() { diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 6bee1c1..8b15720 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -54,7 +54,7 @@ import com.google.common.primitives.Ints; * * @author David ‘Bombe’ Roden */ -public class Sone implements Fingerprintable, Comparable { +public class Sone implements Identified, Fingerprintable, Comparable { /** * Enumeration for the possible states of a {@link Sone}.