import java.util.Collection;
/**
- * TODO
+ * Data interface for artists.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public interface Artist extends Base {
+ /**
+ * Returns the name of this artist.
+ *
+ * @return The name of this artist
+ */
public String getName();
+ /**
+ * Sets the name of this artist.
+ *
+ * @param name
+ * The new name of this artist
+ * @return This artist
+ */
public Artist setName(String name);
+ /**
+ * Returns the groups this artist belongs to.
+ *
+ * @return The groups this artist belongs to
+ */
public Collection<Group> getGroups();
+ /**
+ * Sets the groups this artist belongs to.
+ *
+ * @param groups
+ * The groups this artist belongs to
+ * @return This artist
+ */
public Artist setGroups(Collection<Group> groups);
+ /**
+ * Returns the tracks created by this artist.
+ *
+ * @return The tracks created by this artist
+ */
public Collection<Track> getTracks();
+ /**
+ * Sets the tracks created by this artist.
+ *
+ * @param tracks
+ * The tracks created by this artist
+ * @return This artist
+ */
public Artist setTracks(Collection<Track> tracks);
}
package net.pterodactylus.demoscenemusic.data;
-
/**
- * TODO
+ * Base class for all data interfaces and implementations.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public interface Base {
+ /**
+ * Returns the ID of the data container.
+ *
+ * @return The ID of the data container
+ */
public String getId();
}
import net.pterodactylus.util.database.ValueFieldWhereClause;
/**
- * TODO
+ * Interface between the database and the application.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DataManager {
+ /** The artist object creator. */
@SuppressWarnings("synthetic-access")
private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
+
+ /** The group object creator. */
@SuppressWarnings("synthetic-access")
private final ObjectCreator<Group> groupCreator = new GroupCreator();
+
+ /** The track object creator. */
@SuppressWarnings("synthetic-access")
private final ObjectCreator<Track> trackCreator = new TrackCreator();
+
+ /** The style object creator. */
@SuppressWarnings("synthetic-access")
private final ObjectCreator<Style> styleCreator = new StyleCreator();
+ /** The database. */
private final Database database;
+ /**
+ * Creates a new data manager.
+ *
+ * @param database
+ * The database to operate on
+ */
public DataManager(Database database) {
this.database = database;
}
+ /**
+ * Returns all artists.
+ *
+ * @return All artists
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Collection<Artist> getAllArtists() throws DatabaseException {
Query query = new Query(Type.SELECT, "ARTISTS");
query.addField(new Field("ARTISTS.*"));
return database.getMultiple(query, artistCreator);
}
+ /**
+ * Returns the artist with the given ID.
+ *
+ * @param id
+ * The ID of the artist
+ * @return The artist with the given ID, or {@code null} if there is no
+ * artist with the given ID
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Artist getArtistById(String id) throws DatabaseException {
Query query = new Query(Type.SELECT, "ARTISTS");
query.addField(new Field("ARTISTS.*"));
return database.getSingle(query, artistCreator);
}
+ /**
+ * Returns all artists that belong to the group with the given ID.
+ *
+ * @param groupId
+ * The ID of the group
+ * @return All artists belonging to the given group
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
Query query = new Query(Type.SELECT, "ARTISTS");
query.addField(new Field("ARTISTS.*"));
return database.getMultiple(query, artistCreator);
}
+ /**
+ * Returns all artists involved in the track with the given ID.
+ *
+ * @param trackId
+ * The ID of the track
+ * @return All artists involved in the track, in preferred order
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
Query query = new Query(Type.SELECT, "ARTISTS");
query.addField(new Field("ARTISTS.*"));
return database.getMultiple(query, artistCreator);
}
+ /**
+ * Returns the track with the given ID.
+ *
+ * @param id
+ * The ID of the track
+ * @return The track with the given ID, or {@code null} if there is no such
+ * track
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Track getTrackById(String id) throws DatabaseException {
Query query = new Query(Type.SELECT, "TRACKS");
query.addField(new Field("TRACKS.*"));
return database.getSingle(query, trackCreator);
}
+ /**
+ * Returns all tracks by the artist with the given ID.
+ *
+ * @param artistId
+ * The ID of the artist
+ * @return All tracks by the given artist
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
Query query = new Query(Type.SELECT, "TRACKS");
query.addField(new Field("TRACKS.*"));
return database.getMultiple(query, trackCreator);
}
+ /**
+ * Returns all groups the artist with the given ID belongs to.
+ *
+ * @param artistId
+ * The ID of the artist
+ * @return All groups the artist belongs to
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
Query query = new Query(Type.SELECT, "GROUPS");
query.addField(new Field("GROUPS.*"));
return database.getMultiple(query, groupCreator);
}
+ /**
+ * Returns all styles for the track with the given ID.
+ *
+ * @param trackId
+ * The ID of the track
+ * @return All styles for the given track
+ * @throws DatabaseException
+ * if a database error occurs
+ */
public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
Query query = new Query(Type.SELECT, "STYLES");
query.addField(new Field("STYLES.*"));
return database.getMultiple(query, styleCreator);
}
+ /**
+ * {@link Artist} implementation that retrieves some attributes (such as
+ * {@link #getGroups()}, and {@link #getTracks()}) from the
+ * {@link DataManager} on demand.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class LazyArtist extends DefaultArtist {
+ /** Memoizer for the tracks by this artist. */
private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
public Void call() throws DatabaseException {
}
});
+ /** Memoizer for the groups of this artist. */
private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
});
+ /**
+ * Creates a new lazy artist.
+ *
+ * @param id
+ * The ID of the artist
+ */
public LazyArtist(String id) {
super(id);
}
+ //
+ // DEFAULTARTIST METHODS
+ //
+
/**
* {@inheritDoc}
*/
return super.getGroups();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Collection<Track> getTracks() {
tracksMemoizer.get();
}
+ /**
+ * {@link ObjectCreator} implementation that can create {@link Artist}
+ * objects. This specific class actually creates {@link LazyArtist}
+ * instances.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class ArtistCreator implements ObjectCreator<Artist> {
/**
}
+ /**
+ * {@link Group} implementation that retrieves some attributes (such as
+ * {@link #getArtists()}) from the {@link DataManager} on demand.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class LazyGroup extends DefaultGroup {
+ /** Memoizer for the artist. */
private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
});
+ /**
+ * Creates a new lazy group.
+ *
+ * @param id
+ * The ID of the group
+ */
public LazyGroup(String id) {
super(id);
}
+ //
+ // DEFAULTGROUP METHODS
+ //
+
/**
* {@inheritDoc}
*/
}
+ /**
+ * {@link ObjectCreator} implementation that can create {@link Group}
+ * objects. This specific implementation creates {@link LazyGroup}
+ * instances.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class GroupCreator implements ObjectCreator<Group> {
/**
}
+ /**
+ * {@link Track} implementation that retrieves some attributes (such as
+ * {@link #getArtists()}, and {@link #getStyles()}) from the
+ * {@link DataManager} on demand.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class LazyTrack extends DefaultTrack {
+ /** Memoizer for the artists. */
private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
});
+ /** Memoizer for the styles. */
private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
});
/**
+ * Creates a new track.
+ *
* @param id
+ * The ID of the track
*/
public LazyTrack(String id) {
super(id);
}
+ //
+ // DEFAULTTRACK METHODS
+ //
+
/**
* {@inheritDoc}
*/
}
+ /**
+ * {@link ObjectCreator} implementation that can create {@link Track}
+ * objects. This specific implementation creates {@link LazyTrack}
+ * instances.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class TrackCreator implements ObjectCreator<Track> {
/**
}
+ /**
+ * {@link ObjectCreator} implementation that can create {@link Style}
+ * objects.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
private class StyleCreator implements ObjectCreator<Style> {
/**
import java.util.Collection;
/**
- * TODO
+ * Default implementation for an artist data container.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultArtist extends DefaultBase implements Artist {
+ /**
+ * Creates a new artist data container.
+ *
+ * @param id
+ * The ID of the artist
+ */
public DefaultArtist(String id) {
super(id);
}
+ //
+ // ARTIST METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
@Override
public String getName() {
return getValue("name", String.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Artist setName(String name) {
getValue("name", String.class).set(name);
return this;
}
+ /**
+ * {@inheritDoc}
+ */
@Override
@SuppressWarnings({ "unchecked" })
public Collection<Track> getTracks() {
return getValue("tracks", Collection.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Artist setTracks(Collection<Track> tracks) {
getValue("tracks", Collection.class).set(tracks);
import java.util.Map.Entry;
/**
- * TODO
+ * Base implementation of a data container. It stores the ID of a container and
+ * can contain arbitrary attributes.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultBase implements Base {
+ /** The ID of the data container. */
private final String id;
+ /** The attributes of the data container. */
private final Map<String, Value<?>> attributes = new HashMap<String, Value<?>>();
+ /**
+ * Creates a new data container with the given ID.
+ *
+ * @param id
+ * The ID of the data container
+ */
protected DefaultBase(String id) {
this.id = id;
}
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
@Override
public String getId() {
return id;
}
+ /**
+ * Returns whether the data container contains an attribute with the given
+ * name.
+ *
+ * @param name
+ * The name of the attribute to check for
+ * @return {@code true} if this data container contains an attribute with
+ * the given name, {@code false} otherwise
+ */
protected boolean hasValue(String name) {
return attributes.containsKey(name);
}
+ /**
+ * Returns the value of the attribute with the given name. If no value for
+ * the given attribute exists, one will be created, stored, and returned.
+ *
+ * @param <T>
+ * The type of the value to return
+ * @param name
+ * The name of the attribute to get
+ * @param clazz
+ * The class of the value
+ * @return The value of the attribute
+ */
@SuppressWarnings({ "unchecked" })
protected <T> Value<T> getValue(String name, Class<T> clazz) {
if (!attributes.containsKey(name)) {
return (Value<T>) attributes.get(name);
}
+ /**
+ * Returns whether any of the attributes of this data container is dirty,
+ * i.e. it was modified after its initial inception.
+ *
+ * @return {@code true} if any attribute of this data container was modified
+ */
protected boolean isDirty() {
for (Value<?> value : attributes.values()) {
if (value.isDirty()) {
return stringBuilder.toString();
}
+ /**
+ * Container for an attribute value stored in a data container.
+ *
+ * @see DefaultBase#attributes
+ * @param <T>
+ * The type of the value
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
protected static class Value<T> {
+ /** The original value. */
private T original;
+ /** Whether the original value has been set. */
private boolean originalSet;
+ /** The current value. */
private T current;
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Returns the current value.
+ *
+ * @return The current value
+ */
public T get() {
return current;
}
+ /**
+ * Sets the current value. If no original value has yet been set, the
+ * given value is also stored as original value.
+ *
+ * @param value
+ * The value to set
+ * @return This value
+ */
public Value<T> set(T value) {
if (!originalSet) {
original = value;
return this;
}
+ /**
+ * Returns whether the value of this value does not equal its original
+ * value.
+ *
+ * @return {@code true} if the current value is not equal to the
+ * original value, {@code false} if it is equal to the original
+ * value
+ */
public boolean isDirty() {
return (original != null) ? !original.equals(current) : current != null;
}
+ /**
+ * Commits the current value, i.e. sets the original value to the
+ * current value.
+ *
+ * @return This value
+ */
public Value<T> commit() {
original = current;
return this;
import java.util.Collection;
/**
- * TODO
+ * Default implementation of a group data container.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultGroup extends DefaultBase implements Group {
+ /**
+ * Creates a new group data container.
+ *
+ * @param id
+ * The ID of the group
+ */
public DefaultGroup(String id) {
super(id);
}
+ //
+ // GROUP METHODS
+ //
+
/**
* {@inheritDoc}
*/
package net.pterodactylus.demoscenemusic.data;
/**
- * TODO
+ * Default implementation of a party data container.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultParty extends DefaultBase implements Party {
+ /**
+ * Creates a new party data container.
+ *
+ * @param id
+ * The ID of the party
+ */
public DefaultParty(String id) {
super(id);
}
+ //
+ // PARTY METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
@Override
public String getName() {
return getValue("name", String.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public DefaultParty setName(String name) {
getValue("name", String.class).set(name);
package net.pterodactylus.demoscenemusic.data;
/**
- * TODO
+ * Default implementation of a style data container.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultStyle extends DefaultBase implements Style {
+ /**
+ * Creates a new style data container.
+ *
+ * @param id
+ * The ID of the style
+ */
public DefaultStyle(String id) {
super(id);
}
+ //
+ // STYLE METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
@Override
public String getName() {
return getValue("name", String.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Style setName(String name) {
getValue("name", String.class).set(name);
import java.util.List;
/**
- * TODO
+ * Default implementation of a track data container.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultTrack extends DefaultBase implements Track {
+ /**
+ * Creates a new track data container.
+ *
+ * @param id
+ * The ID of the track
+ */
public DefaultTrack(String id) {
super(id);
}
+ //
+ // TRACK METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
@Override
public String getName() {
return getValue("name", String.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Track setName(String name) {
getValue("name", String.class).set(name);
return this;
}
+ /**
+ * {@inheritDoc}
+ */
@Override
@SuppressWarnings("unchecked")
public List<Artist> getArtists() {
return getValue("artists", List.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Track setArtists(List<Artist> artists) {
getValue("artists", List.class).set(artists);
return this;
}
+ /**
+ * {@inheritDoc}
+ */
@Override
@SuppressWarnings("unchecked")
public Collection<Style> getStyles() {
return getValue("styles", Collection.class).get();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Track setStyles(Collection<? extends Style> styles) {
getValue("styles", Collection.class).set(styles);
import java.util.Collection;
/**
- * TODO
+ * Data interface for groups.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public interface Group extends Base {
+ /**
+ * Returns the name of this group.
+ *
+ * @return The name of this group
+ */
public String getName();
+ /**
+ * Sets the name of this group.
+ *
+ * @param name
+ * The name of this group
+ * @return This group
+ */
public Group setName(String name);
+ /**
+ * Returns the URL of this group’s website.
+ *
+ * @return The URL of this group’s website
+ */
public String getUrl();
+ /**
+ * Sets the URL of this group’s website.
+ *
+ * @param url
+ * The URL of this group’s website
+ * @return This group
+ */
public Group setUrl(String url);
+ /**
+ * Returns all artists belonging to this group.
+ *
+ * @return All artists belonging to this group
+ */
public Collection<Artist> getArtists();
+ /**
+ * Sets all artists belonging to this group.
+ *
+ * @param artists
+ * All artists belonging to this group
+ * @return This group
+ */
public Group setArtists(Collection<Artist> artists);
}
package net.pterodactylus.demoscenemusic.data;
-
/**
- * TODO
+ * Data interface for parties.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public interface Party extends Base {
+ /**
+ * Returns the name of this party.
+ *
+ * @return The name of this party
+ */
public String getName();
+ /**
+ * Sets the name of this party.
+ *
+ * @param name
+ * The name of this party
+ * @return This party
+ */
public Party setName(String name);
}
package net.pterodactylus.demoscenemusic.data;
/**
- * TODO
+ * Data interface for styles.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public interface Style extends Base {
+ /**
+ * Returns the name of this style.
+ *
+ * @return The name of this style
+ */
public String getName();
+ /**
+ * Sets the name of this style.
+ *
+ * @param name
+ * The name of this style
+ * @return This style
+ */
public Style setName(String name);
}
import java.util.List;
/**
- * TODO
+ * Data interface for tracks.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public interface Track extends Base {
+ /**
+ * Returns the name of this track.
+ *
+ * @return The name of this track
+ */
public String getName();
+ /**
+ * Sets the name of this track.
+ *
+ * @param name
+ * The name of this track
+ * @return This track
+ */
public Track setName(String name);
+ /**
+ * Returns all artists involved in this track.
+ *
+ * @return All involved artists in preferred order
+ */
public List<Artist> getArtists();
+ /**
+ * Sets all artists involved in this track.
+ *
+ * @param artists
+ * All involved artists in preferred order
+ * @return This track
+ */
public Track setArtists(List<Artist> artists);
+ /**
+ * Returns all styles of this track.
+ *
+ * @return All styles of this track
+ */
public Collection<Style> getStyles();
+ /**
+ * Sets all styles of this track.
+ *
+ * @param styles
+ * All styles of this track
+ * @return This track
+ */
public Track setStyles(Collection<? extends Style> styles);
}