com.planetj.taste.impl.model
Class GenericDataModel

java.lang.Object
  extended by com.planetj.taste.impl.model.GenericDataModel
All Implemented Interfaces:
Refreshable, DataModel, java.io.Serializable

public final class GenericDataModel
extends java.lang.Object
implements DataModel, java.io.Serializable

A simple DataModel which uses a given List of Users as its data source. This implementation is mostly useful for small experiments and is not recommended for contexts where performance is important.

Author:
Sean Owen
See Also:
Serialized Form

Constructor Summary
GenericDataModel(DataModel dataModel)
          Creates a new GenericDataModel containing an immutable copy of the data from another given DataModel.
GenericDataModel(java.lang.Iterable<? extends User> users)
          Creates a new GenericDataModel from the given Users (and their preferences).
 
Method Summary
 Item getItem(java.lang.Object id)
          
 java.lang.Iterable<? extends Item> getItems()
          
 int getNumItems()
          
 int getNumUsers()
          
 java.lang.Iterable<? extends Preference> getPreferencesForItem(java.lang.Object itemID)
          
 Preference[] getPreferencesForItemAsArray(java.lang.Object itemID)
          
 User getUser(java.lang.Object id)
          
 java.lang.Iterable<? extends User> getUsers()
          
 void refresh()
          

Triggers "refresh" -- whatever that means -- of the implementation.

 void removePreference(java.lang.Object userID, java.lang.Object itemID)
          Removes a particular preference for a user.
 void setPreference(java.lang.Object userID, java.lang.Object itemID, double value)
          Sets a particular preference (item plus rating) for a user.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GenericDataModel

public GenericDataModel(java.lang.Iterable<? extends User> users)

Creates a new GenericDataModel from the given Users (and their preferences). This DataModel retains all this information in memory and is effectively immutable.

Parameters:
users - Users to include in this GenericDataModel

GenericDataModel

public GenericDataModel(DataModel dataModel)
                 throws TasteException

Creates a new GenericDataModel containing an immutable copy of the data from another given DataModel.

Parameters:
dataModel - DataModel to copy
Throws:
TasteException - if an error occurs while retrieving the other DataModel's users
Method Detail

getUsers

@NotNull
public java.lang.Iterable<? extends User> getUsers()

Specified by:
getUsers in interface DataModel
Returns:
a List of all Users in the model, ordered by User

getUser

@NotNull
public User getUser(java.lang.Object id)

Specified by:
getUser in interface DataModel
Parameters:
id - user ID
Returns:
User who has that ID
Throws:
java.util.NoSuchElementException - if there is no such User

getItems

@NotNull
public java.lang.Iterable<? extends Item> getItems()

Specified by:
getItems in interface DataModel
Returns:
a List of all Items in the model, order by Item

getItem

@NotNull
public Item getItem(java.lang.Object id)

Specified by:
getItem in interface DataModel
Parameters:
id - item ID
Returns:
Item that has that ID
Throws:
java.util.NoSuchElementException - if there is no such Item

getPreferencesForItem

@NotNull
public java.lang.Iterable<? extends Preference> getPreferencesForItem(java.lang.Object itemID)

Specified by:
getPreferencesForItem in interface DataModel
Parameters:
itemID - item ID
Returns:
all existing Preferences expressed for that item, ordered by User

getPreferencesForItemAsArray

@NotNull
public Preference[] getPreferencesForItemAsArray(java.lang.Object itemID)

Specified by:
getPreferencesForItemAsArray in interface DataModel
Parameters:
itemID - item ID
Returns:
all existing Preferences expressed for that item, ordered by User, as an array

getNumItems

public int getNumItems()

Specified by:
getNumItems in interface DataModel
Returns:
total number of Items known to the model. This is generally the union of all Items preferred by at least one User but could include more.

getNumUsers

public int getNumUsers()

Specified by:
getNumUsers in interface DataModel
Returns:
total number of Users known to the model.

setPreference

public void setPreference(java.lang.Object userID,
                          java.lang.Object itemID,
                          double value)
Description copied from interface: DataModel

Sets a particular preference (item plus rating) for a user.

Specified by:
setPreference in interface DataModel
Parameters:
userID - user to set preference for
itemID - item to set preference for
value - preference value
Throws:
java.lang.UnsupportedOperationException

removePreference

public void removePreference(java.lang.Object userID,
                             java.lang.Object itemID)
Description copied from interface: DataModel

Removes a particular preference for a user.

Specified by:
removePreference in interface DataModel
Parameters:
userID - user from which to remove preference
itemID - item to remove preference for
Throws:
java.lang.UnsupportedOperationException

refresh

public void refresh()

Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any Refreshable should always leave itself in a consistent, operational state, and that the refresh atomically updates internal state from old to new.

Specified by:
refresh in interface Refreshable

toString

@NotNull
public java.lang.String toString()
Overrides:
toString in class java.lang.Object