ca.spaz.cron.datasource
Class AbstractMutableFoodDatasource

java.lang.Object
  extended by ca.spaz.cron.datasource.AbstractFoodDataSource
      extended by ca.spaz.cron.datasource.AbstractMutableFoodDatasource
All Implemented Interfaces:
IFoodDatasource, ILocalFoodDatasource
Direct Known Subclasses:
SQLDatasource

public abstract class AbstractMutableFoodDatasource
extends AbstractFoodDataSource
implements ILocalFoodDatasource

A partial implementation of a mutable food source. Provides a certain degree of input validation and notification support.

Author:
Chris Rose

Constructor Summary
protected AbstractMutableFoodDatasource(java.lang.String name)
           
 
Method Summary
 Food addFood(Food food)
          Add a Food to this datasource.
 void addFoodDatasourceListener(IFoodDatasourceListener listener)
          Add a new IFoodDatasourceListener to this implementation.
 void addFoodGroup(FoodGroup foodGroup)
          Add a new FoodGroup to the data source.
 boolean addMeasure(Food food, Measure measure)
          Each food in the Datasource has available a list of Measures that describe portions or servings.
 Food addServing(Serving serving)
          This method consumes the food in a serving.
 boolean changeMeasure(Food food, java.util.List measures)
           
 boolean changeServingAmount(Serving serving)
          Changes the amount of a food consumed to the amount in the serving provided.
 Food createNewFood()
          Create a new Food object associated with this datasource.
protected abstract  Food doAddFood(Food food)
          Add a Food to this datasource.
protected abstract  void doAddFoodGroup(FoodGroup foodGroup)
          Add a new FoodGroup to the data source.
protected abstract  boolean doAddMeasure(Food food, Measure measure)
          Each food in the Datasource has available a list of Measures that describe portions or servings.
protected abstract  boolean doChangeMeasure(Food food, java.util.List measures)
           
protected abstract  boolean doChangeServingAmount(Serving serving)
          Changes the amount of a food consumed to the amount in the serving provided.
protected abstract  Food doConsumeFood(Serving serving)
          This method consumes the food in a serving.
protected abstract  Food doCreateNewFood()
          Create a new Food object associated with this datasource.
protected abstract  int doGetTimesConsumed(Food food)
          Retrieve the total number of times that this food has been consumed.
protected abstract  int doGetTimesConsumed(Food food, java.util.Date startDate, java.util.Date endDate)
          Retrieve the number of times the specified Food was consumed between the provided dates.
protected abstract  boolean doRemoveFood(Food food)
          Remove a Food from the datasource.
protected abstract  boolean doRemoveMeasure(Food food, Measure measure)
          Remove a particular form of Measure from the list of those available for a particular Food.
protected abstract  boolean doSaveFood(Food food)
          Alter the information of some Food object in the backing representation of the datasource.
protected abstract  boolean doUnConsumeFood(Serving serving)
          Remove the Food in a particular serving from the user's consumed list.
protected abstract  Food findFoodBySourceUID(java.lang.String sourceUID)
          Find a local food by its sourceUID field value.
protected  boolean getNotify()
           
 int getTimesConsumed(Food food)
          Retrieve the total number of times that this food has been consumed.
 int getTimesConsumed(Food food, java.util.Date startDate, java.util.Date endDate)
          Retrieve the number of times the specified Food was consumed between the provided dates.
protected  void notifyObservers(FoodDataEvent type)
           
protected  void notifyObservers(FoodDataEvent type, java.lang.Object message)
           
 boolean removeFood(Food food)
          Remove a Food from the datasource.
 void removeFoodDatasourceListener(IFoodDatasourceListener listener)
          Remove an IFoodDatasourceListener from the list of observers of this class.
 boolean removeMeasure(Food food, Measure measure)
          Remove a particular form of Measure from the list of those available for a particular Food.
 boolean removeServing(Serving serving)
          Remove the Food in a particular serving from the user's consumed list.
 boolean saveFood(Food food)
          Alter the information of some Food object in the backing representation of the datasource.
protected  void setNotify(boolean notify)
           
 
Methods inherited from class ca.spaz.cron.datasource.AbstractFoodDataSource
diffSource, doFindAllFoods, doFindFoods, doFindFoods, doGetMeasuresFor, doGetNutrientsFor, findAllFoods, findFoods, findFoods, getLastError, getMeasuresFor, getName, getNutrientsFor, notNull, registerError, sameSource, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ca.spaz.cron.datasource.ILocalFoodDatasource
getConsumedOn
 
Methods inherited from interface ca.spaz.cron.datasource.IFoodDatasource
close, containsFood, findAllFoods, findFoods, findFoods, getFoodGroups, getLastError, getMeasuresFor, getName, getNutrientsFor, getSources, initialize, isAvailable, isListable, isSearchable
 

Constructor Detail

AbstractMutableFoodDatasource

protected AbstractMutableFoodDatasource(java.lang.String name)
Method Detail

setNotify

protected final void setNotify(boolean notify)

getNotify

protected final boolean getNotify()

addFood

public final Food addFood(Food food)
Description copied from interface: ILocalFoodDatasource
Add a Food to this datasource. This method is an exception to the local datasource rule -- the Food provided to this method must not be attached to this datasource.

Specified by:
addFood in interface ILocalFoodDatasource
Parameters:
food - A Food object that comes from another datasource.
Returns:
a Food object whose information is the same as the parameter, but that exists only in this datasource, with all nutrient information still represtented. null will be returned if some error occurred.

findFoodBySourceUID

protected abstract Food findFoodBySourceUID(java.lang.String sourceUID)
Find a local food by its sourceUID field value.

Parameters:
sourceUID - The key to search on
Returns:
The Food found, or null if no food matched.

createNewFood

public final Food createNewFood()
Description copied from interface: ILocalFoodDatasource
Create a new Food object associated with this datasource.

Specified by:
createNewFood in interface ILocalFoodDatasource
Returns:
a new Food instance.

doCreateNewFood

protected abstract Food doCreateNewFood()
Create a new Food object associated with this datasource.

Returns:
a new Food instance.

addFoodDatasourceListener

public final void addFoodDatasourceListener(IFoodDatasourceListener listener)
Description copied from interface: ILocalFoodDatasource
Add a new IFoodDatasourceListener to this implementation. The listener will be notified on any successful change to the underlying datasource.

Specified by:
addFoodDatasourceListener in interface ILocalFoodDatasource
Parameters:
listener - the new IFoodDatasourceListener.

addFoodGroup

public final void addFoodGroup(FoodGroup foodGroup)
Description copied from interface: ILocalFoodDatasource
Add a new FoodGroup to the data source. The food group may already exist, in which case an implementing method should silently succeed.

Specified by:
addFoodGroup in interface ILocalFoodDatasource
Parameters:
foodGroup - the new food group to add to the data source.

doAddFoodGroup

protected abstract void doAddFoodGroup(FoodGroup foodGroup)
Add a new FoodGroup to the data source. The food group may already exist, in which case an implementing method should silently succeed.

Parameters:
foodGroup - the new food group to add to the data source.

addMeasure

public final boolean addMeasure(Food food,
                                Measure measure)
Description copied from interface: ILocalFoodDatasource
Each food in the Datasource has available a list of Measures that describe portions or servings. This method will add a valid Measure to a Food object stored in this datasource.

Specified by:
addMeasure in interface ILocalFoodDatasource
Parameters:
food - The Food object to which the new measure applies.
measure - The new Measure.
Returns:
true if successful, false if some error occurred.

changeServingAmount

public final boolean changeServingAmount(Serving serving)
Description copied from interface: ILocalFoodDatasource
Changes the amount of a food consumed to the amount in the serving provided. This will alter the amount of the Food retrieved by calling getFood() on newServing. If the food is not already in the database as being consumed, it will be added. The food must already be in the user datasource, however.

Specified by:
changeServingAmount in interface ILocalFoodDatasource
Parameters:
serving - a Serving object whose Food's serving quantity on the proper date will be altered.
Returns:
true if the serving was altered, false if some error occurred.

saveFood

public final boolean saveFood(Food food)
Description copied from interface: ILocalFoodDatasource
Alter the information of some Food object in the backing representation of the datasource. This method has potential complications. If the Food implementation specific to the datasource has a concept of unique identifiers, a true alteration will occur. However, if the Food object does not have this feature, for example in a flat-file database or something like it, the implementor should check for name similarity, and if that does not match, simply add the new food to the database.

Specified by:
saveFood in interface ILocalFoodDatasource
Parameters:
food - a Food object to be changed.
Returns:
true if successful, false if some error occurred.

changeMeasure

public final boolean changeMeasure(Food food,
                                   java.util.List measures)
Specified by:
changeMeasure in interface ILocalFoodDatasource
Returns:
true if successful, false if some error occurred.

addServing

public final Food addServing(Serving serving)
Description copied from interface: ILocalFoodDatasource
This method consumes the food in a serving. This an exception to the requirement that a Food object have this as its datasource, since in this case if the Food object has a different datasource, it will either A) be added to this one, and then consumed, or B) if it is already present in this datasource it will simply be added from this datasource.

Specified by:
addServing in interface ILocalFoodDatasource
Parameters:
serving - a Serving to add to the user's consumed list.
Returns:
A new Food object that represents the Food that is associated with this datasource. If the food was already associated with this datasource, the same object will be returned. null will be returned if some error occurred.

doAddFood

protected abstract Food doAddFood(Food food)
Add a Food to this datasource. This method is an exception to the local datasource rule -- the Food provided to this method must not be attached to this datasource.

Parameters:
food - A Food object that comes from another datasource.
Returns:
a Food object whose information is the same as the parameter, but that exists only in this datasource, with all nutrient information still represtented. null will be returned if some error occurred.

doAddMeasure

protected abstract boolean doAddMeasure(Food food,
                                        Measure measure)
Each food in the Datasource has available a list of Measures that describe portions or servings. This method will add a valid Measure to a Food object stored in this datasource.

Parameters:
food - The Food object to which the new measure applies.
measure - The new Measure.
Returns:
true if successful, false if some error occurred.

doChangeServingAmount

protected abstract boolean doChangeServingAmount(Serving serving)
Changes the amount of a food consumed to the amount in the serving provided. This will alter the amount of the Food retrieved by calling getFood() on newServing. If the food is not already in the database as being consumed, it will be added. The food must already be in the user datasource, however.

Parameters:
serving - a Serving object whose Food's serving quantity on the proper date will be altered.
Returns:
true if the serving was altered, false if some error occurred.

doSaveFood

protected abstract boolean doSaveFood(Food food)
Alter the information of some Food object in the backing representation of the datasource. This method has potential complications. If the Food implementation specific to the datasource has a concept of unique identifiers, a true alteration will occur. However, if the Food object does not have this feature, for example in a flat-file database or something like it, the implementor should check for name similarity, and if that does not match, simply add the new food to the database.

Parameters:
food - a Food object to be changed.
Returns:
true if successful, false if some error occurred.

doChangeMeasure

protected abstract boolean doChangeMeasure(Food food,
                                           java.util.List measures)
Parameters:
food -
measures -
Returns:
true if successful, false if some error occurred.

doConsumeFood

protected abstract Food doConsumeFood(Serving serving)
This method consumes the food in a serving. This an exception to the requirement that a Food object have this as its datasource, since in this case if the Food object has a different datasource, it will either A) be added to this one, and then consumed, or B) if it is already present in this datasource it will simply be added from this datasource.

Parameters:
serving - a Serving to add to the user's consumed list.
Returns:
A new Food object that represents the Food that is associated with this datasource. If the food was already associated with this datasource, the same object will be returned. null will be returned if some error occurred.

doGetTimesConsumed

protected abstract int doGetTimesConsumed(Food food)
Retrieve the total number of times that this food has been consumed.

Parameters:
food - the Food to check.
Returns:
the number of times consumed, or -1 on error.

doGetTimesConsumed

protected abstract int doGetTimesConsumed(Food food,
                                          java.util.Date startDate,
                                          java.util.Date endDate)
Retrieve the number of times the specified Food was consumed between the provided dates.

Parameters:
food - the Food to check.
startDate - the starting date.
endDate - the ending date.
Returns:
the number of times consumed between the two dates, or -1 on error.

doRemoveFood

protected abstract boolean doRemoveFood(Food food)
Remove a Food from the datasource.

Parameters:
food - the Food to remove.
Returns:
true if successful, false if some error occurred.

doRemoveMeasure

protected abstract boolean doRemoveMeasure(Food food,
                                           Measure measure)
Remove a particular form of Measure from the list of those available for a particular Food.

Parameters:
food -
measure -
Returns:
true if successful, false if some error occurred.

doUnConsumeFood

protected abstract boolean doUnConsumeFood(Serving serving)
Remove the Food in a particular serving from the user's consumed list. Operates regardless of quantity.

Parameters:
serving - the serving to un-consume.
Returns:
true if the action succeeded, false if there was some error.

getTimesConsumed

public final int getTimesConsumed(Food food)
Description copied from interface: ILocalFoodDatasource
Retrieve the total number of times that this food has been consumed.

Specified by:
getTimesConsumed in interface ILocalFoodDatasource
Parameters:
food - the Food to check.
Returns:
the number of times consumed, or -1 on error.

getTimesConsumed

public final int getTimesConsumed(Food food,
                                  java.util.Date startDate,
                                  java.util.Date endDate)
Description copied from interface: ILocalFoodDatasource
Retrieve the number of times the specified Food was consumed between the provided dates.

Specified by:
getTimesConsumed in interface ILocalFoodDatasource
Parameters:
food - the Food to check.
startDate - the starting date.
endDate - the ending date.
Returns:
the number of times consumed between the two dates, or -1 on error.

notifyObservers

protected final void notifyObservers(FoodDataEvent type)

notifyObservers

protected final void notifyObservers(FoodDataEvent type,
                                     java.lang.Object message)

removeFood

public final boolean removeFood(Food food)
Description copied from interface: ILocalFoodDatasource
Remove a Food from the datasource.

Specified by:
removeFood in interface ILocalFoodDatasource
Parameters:
food - the Food to remove.
Returns:
true if successful, false if some error occurred.

removeFoodDatasourceListener

public final void removeFoodDatasourceListener(IFoodDatasourceListener listener)
Description copied from interface: ILocalFoodDatasource
Remove an IFoodDatasourceListener from the list of observers of this class.

Specified by:
removeFoodDatasourceListener in interface ILocalFoodDatasource
Parameters:
listener - the IFoodDatasourceListener.

removeMeasure

public final boolean removeMeasure(Food food,
                                   Measure measure)
Description copied from interface: ILocalFoodDatasource
Remove a particular form of Measure from the list of those available for a particular Food.

Specified by:
removeMeasure in interface ILocalFoodDatasource
Returns:
true if successful, false if some error occurred.

removeServing

public final boolean removeServing(Serving serving)
Description copied from interface: ILocalFoodDatasource
Remove the Food in a particular serving from the user's consumed list. Operates regardless of quantity.

Specified by:
removeServing in interface ILocalFoodDatasource
Parameters:
serving - the serving to un-consume.
Returns:
true if the action succeeded, false if there was some error.


Copyright © 2005 Spaz. All Rights Reserved.