org.jbudget.io
Class DataIndex

java.lang.Object
  extended by org.jbudget.io.DataIndex

public class DataIndex
extends java.lang.Object

An object that stores an index of all available Month objects.

Author:
petrov

Field Summary
private  int accountCounter
          A counter that is used for assigning IDs to Accounts
private  java.util.Map<java.lang.Integer,org.jbudget.Core.Account> accounts
          A map of numberic account ID's and Accounts.
private  int budgetCounter
          A counter that is used for assinging IDs to Budgets
private  java.util.Map<java.lang.Integer,org.jbudget.Core.Budget> budgets
          A map of numeric budget ID's and budgets.
private  int categoryCounter
          A counter that is used for assigning IDs to ExpenseCategories and income sources.
private  boolean haveData
          If this flag is true the object does not store any Month instances.
private  java.util.Map<java.lang.Integer,org.jbudget.Core.Month> months
          A map of year*100+month to the Month objects
private  long transactionCounter
          A counter that is used for assigning IDs to Transactions
private  int userCounter
          A user counter
private  java.util.Map<java.lang.Integer,org.jbudget.Core.User> users
          A map of uids and User objects
private  java.util.List<Pair<java.lang.Integer,java.lang.String>> validAccounts
          A list of valid accounts.
private  java.util.List<Pair<java.lang.Integer,java.lang.String>> validBudgets
          A list of Pairs of budgetIDs and names of the budgets that are active.
This has different meaning for the index of data in the memory and on the disk or on a server.
private  java.util.Map<java.lang.Integer,java.util.List<java.lang.Integer>> validMonths
          A map that connects a year to a list of valid months for that year
private  java.util.List<Pair<java.lang.Integer,java.lang.String>> validUsers
          A list of uids/names of users
private  java.util.List<java.lang.Integer> validYears
          A list of valid years
 
Constructor Summary
DataIndex(boolean storeData)
          Creates a new instance of DataIndex
 
Method Summary
 void addAccount(org.jbudget.Core.Account account)
          Stores an account in this index
 void addBudget(org.jbudget.Core.Budget budget)
          Stores the budget.
 void addMonth(org.jbudget.Core.Month monthObject)
          Adds or replaces a Month object that is associated with the given year/month combination
private static
<T extends java.lang.Comparable,E>
void
addToList(java.util.List<Pair<T,E>> list, T index, E data, java.lang.String name)
          Used to add a pair Integer, String and such to sorted lists and maintain sorting.
 void addUser(org.jbudget.Core.User user)
          Stores an user in this index
 int generateAccountID()
          Creates a new numeric account id and increases the counter.
 int generateBudgetID()
          Creates a new numeric budget id and increases the counter.
 int generateExpenseCategoryID()
          Creates a new numeric id for an expense category and increases the counter.
 int generateIncomeSourceID()
          Creates a new numeric id for an income source and increases the counter.
 long generateTransactionID()
          Creates a new numeric transaction id for and increases the counter.
 int generateUID()
          Creates a new uid for a new user and increases the counter.
 org.jbudget.Core.Account getAccount(int id)
          Returns an account with a given ID.
 java.util.List<Pair<java.lang.Integer,java.lang.String>> getAccountInfo()
          Returns an unmodifiable list that contains numerical IDs and names of all valid accounts
 java.lang.String getAccountName(int id)
          Returns the name of the account that has a given ID
 org.jbudget.Core.Budget getBudget(int id)
          Returns a Budget object with the given numeric ID.
private static
<T extends java.lang.Comparable,E>
E
getFromList(java.util.List<Pair<T,E>> list, T index)
          Searches the list of pairs index/data for a pair with a give index and returns the data
static DataIndex getInstance(Element element)
          Parses JDOM Element into a DataIndex object.
 org.jbudget.Core.Month getMonth(int month, int year)
          Returns the Month object that represents the given year/month combination.
 org.jbudget.Core.User getUser(int uid)
          Returns an user with a given UID.
 java.util.List<Pair<java.lang.Integer,java.lang.String>> getUserInfo()
          Returns an unmodifiable list that contains UIDs and names of all valid users
 java.lang.String getUserName(int uid)
          Returns the name of the user that has a given UID
 java.util.List<Pair<java.lang.Integer,java.lang.String>> getValidBudgets()
          Returns a list containing Pairs of ids and names of all valid Budgets
 java.util.List<java.lang.Integer> getValidMonths(int year)
          Returns a list of valid months for a given year
 java.util.List<java.lang.Integer> getValidYears()
          Returns a list of years that have at least one valid month (a month for which we have a Month object)
 Element getXmlElement()
          Returns a JDOM XML Element representation of the current budget
 boolean indexingMode()
          Returns true if the index is used in indexing only mode.
 boolean isValidAccount(int id)
          Returns true if there is an account with a given ID
 boolean isValidBudget(int id)
          Returns true if there is a Budget with such id
 boolean isValidMonth(int month, int year)
          Returns true if we have a Month object for the given year/month combination
 boolean isValidUser(int uid)
          Returns true if there is a user with a given ID
 void registerAccount(int accountID, java.lang.String accountName)
          Records information about account.
private  void registerAccountPrivate(int accountID, java.lang.String accountName)
          Actually does the work of registering the account
 void registerBudget(int id, java.lang.String name)
          adds information about a Budget instance to the list of valid budgets.
private  void registerBudgetPrivate(int id, java.lang.String name)
          adds information about a Budget instance to the list of valid budgets.
 void registerMonth(int month, int year)
          Registers a month object by marking that there is a month object that corresponds to the given year/month combination.
private  void registerMonthPrivate(int month, int year)
          Actually does the work necessary to make a record that a month is valid.
 void registerUser(int uid, java.lang.String userName)
          Records information about a user.
private  void registerUserPrivate(int uid, java.lang.String userName)
          Actually does the work of registering the user
 void removeAccount(int accountID)
          Removes all information about an account with a given id.
 void removeBudget(int id)
          Removes a budget with a given numeric ID.
private static
<T extends java.lang.Comparable,E>
void
removeFromList(java.util.List<Pair<T,E>> list, T index, java.lang.String name)
          Removes a generic pair index/data (i.e.
 void removeMonth(org.jbudget.Core.Month monthObject)
          Removes a Month object from this data index.
 void removeUser(int uid)
          Removes all information about an user with a given uid.
 java.lang.String toString()
           
 void unregisterAccount(int accountID)
          Removes a record of information about account.Should only be used when the DataIndex object is in indexing only mode (haveData is false).
private  void unregisterAccountPrivate(int accountID)
          Acutally does the work on unregestering the account
 void unregisterBudget(int id)
          removes information about a Budget instance from the list of valid budgets.
private  void unregisterBudgetPrivate(int id)
          removes information about a Budget instance from the list of valid budgets.
 void unregisterMonth(int month, int year)
          Unregisteres a month object by removing all information related to it from this data index.
private  void unregisterMonthPrivate(int month, int year)
          Actually does the work to remove information about a month object.
 void unregisterUser(int uid)
          Removes a record of information about a user.
private  void unregisterUserPrivate(int uid)
          Acutally does the work on unregestering the user
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

validYears

private java.util.List<java.lang.Integer> validYears
A list of valid years


validMonths

private java.util.Map<java.lang.Integer,java.util.List<java.lang.Integer>> validMonths
A map that connects a year to a list of valid months for that year


months

private java.util.Map<java.lang.Integer,org.jbudget.Core.Month> months
A map of year*100+month to the Month objects


validBudgets

private java.util.List<Pair<java.lang.Integer,java.lang.String>> validBudgets
A list of Pairs of budgetIDs and names of the budgets that are active.
This has different meaning for the index of data in the memory and on the disk or on a server. In the case when this object is used as an index of data in the memory (current work set) the map describes the budget objects that have been loaded from the permanent storage and currently reside in memory. Any budget from this list can be accessed by calling getBudget(budgetID). In the case when this object is used to index the data available in permanent storage, the map just contains all numeric ids and names of all budgets available in storage.


budgets

private java.util.Map<java.lang.Integer,org.jbudget.Core.Budget> budgets
A map of numeric budget ID's and budgets.


validAccounts

private java.util.List<Pair<java.lang.Integer,java.lang.String>> validAccounts
A list of valid accounts. Works pretty much the same way as the list of valid budgets


accounts

private java.util.Map<java.lang.Integer,org.jbudget.Core.Account> accounts
A map of numberic account ID's and Accounts.


validUsers

private java.util.List<Pair<java.lang.Integer,java.lang.String>> validUsers
A list of uids/names of users


users

private java.util.Map<java.lang.Integer,org.jbudget.Core.User> users
A map of uids and User objects


haveData

private final boolean haveData
If this flag is true the object does not store any Month instances. An attempt to add a Month or to get a month will cause an exception.


budgetCounter

private int budgetCounter
A counter that is used for assinging IDs to Budgets


accountCounter

private int accountCounter
A counter that is used for assigning IDs to Accounts


categoryCounter

private int categoryCounter
A counter that is used for assigning IDs to ExpenseCategories and income sources.


transactionCounter

private long transactionCounter
A counter that is used for assigning IDs to Transactions


userCounter

private int userCounter
A user counter

Constructor Detail

DataIndex

public DataIndex(boolean storeData)
Creates a new instance of DataIndex

Method Detail

getValidYears

public java.util.List<java.lang.Integer> getValidYears()
Returns a list of years that have at least one valid month (a month for which we have a Month object)


getValidMonths

public java.util.List<java.lang.Integer> getValidMonths(int year)
Returns a list of valid months for a given year


isValidMonth

public boolean isValidMonth(int month,
                            int year)
Returns true if we have a Month object for the given year/month combination


getMonth

public org.jbudget.Core.Month getMonth(int month,
                                       int year)
Returns the Month object that represents the given year/month combination.

Throws:
java.lang.UnsupportedOperationException - if this object is used in indexing only mode (a 'false' is passed to the constructor).

addMonth

public void addMonth(org.jbudget.Core.Month monthObject)
Adds or replaces a Month object that is associated with the given year/month combination

Throws:
java.lang.UnsupportedOperationException - if this object is used in indexing only mode ('false' is passed to the constructor).

removeMonth

public void removeMonth(org.jbudget.Core.Month monthObject)
Removes a Month object from this data index.

Throws:
java.lang.UnsupportedOperationException - if this object is used in indexing only mode ('false' is passed to the constructor).
java.lang.IllegalArgumentException - if the Month is not present in the data index.

registerMonth

public void registerMonth(int month,
                          int year)
Registers a month object by marking that there is a month object that corresponds to the given year/month combination. This function sould be used only when the DataIndex is used in indexing only mode and does not store any month objects. In the opposite case use addMonth.

Throws:
java.lang.UnsupportedOperationException - if this object is not used in indexing only mode ('true' is passed to the constructor).

unregisterMonth

public void unregisterMonth(int month,
                            int year)
Unregisteres a month object by removing all information related to it from this data index. This function sould be used only when the DataIndex is used in indexing only mode. In the opposite case use removeMonth.

Throws:
java.lang.UnsupportedOperationException - if this object is not used in indexing only mode ('true' is passed to the constructor).
java.lang.IllegalArgumentException - if the month that is bein passed to this function is not present in this index.

registerMonthPrivate

private void registerMonthPrivate(int month,
                                  int year)
Actually does the work necessary to make a record that a month is valid.

See Also:
registerMonth(int, int), addMonth(Month)

unregisterMonthPrivate

private void unregisterMonthPrivate(int month,
                                    int year)
Actually does the work to remove information about a month object.

Throws:
java.lang.IllegalArgumentException - if no record for the current month exists.
See Also:
unregisterMonth(int, int), removeMonth(Month)

getValidBudgets

public java.util.List<Pair<java.lang.Integer,java.lang.String>> getValidBudgets()
Returns a list containing Pairs of ids and names of all valid Budgets


registerBudget

public void registerBudget(int id,
                           java.lang.String name)
adds information about a Budget instance to the list of valid budgets.

Throws:
java.lang.IllegalStateException - if a budget with given id is already present.
java.lang.UnsupportedOperationException - if this object is not used in indexing only mode ('true' is passed to the constructor).

registerBudgetPrivate

private void registerBudgetPrivate(int id,
                                   java.lang.String name)
adds information about a Budget instance to the list of valid budgets.

Throws:
java.lang.IllegalStateException - if a budget with given id is already present.

unregisterBudget

public void unregisterBudget(int id)
removes information about a Budget instance from the list of valid budgets.

Throws:
java.lang.IllegalArgumentException - if no budget with the given id exists.
java.lang.UnsupportedOperationException - if this object is not used in indexing only mode ('true' is passed to the constructor).

unregisterBudgetPrivate

private void unregisterBudgetPrivate(int id)
removes information about a Budget instance from the list of valid budgets.

Throws:
java.lang.IllegalArgumentException - if no budget with the given id exists.

getBudget

public org.jbudget.Core.Budget getBudget(int id)
Returns a Budget object with the given numeric ID.

Throws:
java.lang.UnsupportedOperationException - if this object is used in indexing only mode ('false' is passed to the constructor).
IllegalArguementException - if there is no budget with such id.

addBudget

public void addBudget(org.jbudget.Core.Budget budget)
Stores the budget.

Throws:
java.lang.UnsupportedOperationException - if this object is used in indexing only mode ('false' is passed to the constructor).

removeBudget

public void removeBudget(int id)
Removes a budget with a given numeric ID.

Throws:
java.lang.UnsupportedOperationException - if this object is used in indexing only mode ('false' is passed to the constructor).
java.lang.IllegalArgumentException - if no budget with the given id exists.

isValidBudget

public boolean isValidBudget(int id)
Returns true if there is a Budget with such id


isValidAccount

public boolean isValidAccount(int id)
Returns true if there is an account with a given ID


getAccountInfo

public java.util.List<Pair<java.lang.Integer,java.lang.String>> getAccountInfo()
Returns an unmodifiable list that contains numerical IDs and names of all valid accounts


getAccountName

public java.lang.String getAccountName(int id)
Returns the name of the account that has a given ID


getAccount

public org.jbudget.Core.Account getAccount(int id)
Returns an account with a given ID.

Throws:
an - UnsupportedOperationException if haveData is false.

addAccount

public void addAccount(org.jbudget.Core.Account account)
Stores an account in this index

Throws:
an - UnsupportedOperationException if haveData is false.

removeAccount

public void removeAccount(int accountID)
Removes all information about an account with a given id.

Throws:
an - UnsupportedOperationException if haveData is false.

registerAccount

public void registerAccount(int accountID,
                            java.lang.String accountName)
Records information about account. Should only be used when the DataIndex object is in indexing only mode (haveData is false).

Throws:
an - UnsupportedOperationException if haveData is true.

registerAccountPrivate

private void registerAccountPrivate(int accountID,
                                    java.lang.String accountName)
Actually does the work of registering the account


unregisterAccount

public void unregisterAccount(int accountID)
Removes a record of information about account.Should only be used when the DataIndex object is in indexing only mode (haveData is false).

Throws:
an - UnsupportedOperationException if haveData is true.
na - IllegalArgumentException if there is no account with such ID.

unregisterAccountPrivate

private void unregisterAccountPrivate(int accountID)
Acutally does the work on unregestering the account


isValidUser

public boolean isValidUser(int uid)
Returns true if there is a user with a given ID


getUserInfo

public java.util.List<Pair<java.lang.Integer,java.lang.String>> getUserInfo()
Returns an unmodifiable list that contains UIDs and names of all valid users


getUserName

public java.lang.String getUserName(int uid)
Returns the name of the user that has a given UID


getUser

public org.jbudget.Core.User getUser(int uid)
Returns an user with a given UID.

Throws:
an - UnsupportedOperationException if haveData is false.

addUser

public void addUser(org.jbudget.Core.User user)
Stores an user in this index

Throws:
an - UnsupportedOperationException if haveData is false.

removeUser

public void removeUser(int uid)
Removes all information about an user with a given uid.

Throws:
an - UnsupportedOperationException if haveData is false.

registerUser

public void registerUser(int uid,
                         java.lang.String userName)
Records information about a user. Should only be used when the DataIndex object is in indexing only mode (haveData is false).

Throws:
an - UnsupportedOperationException if haveData is true.

registerUserPrivate

private void registerUserPrivate(int uid,
                                 java.lang.String userName)
Actually does the work of registering the user


unregisterUser

public void unregisterUser(int uid)
Removes a record of information about a user. Should only be used when the DataIndex object is in indexing only mode (haveData is false).

Throws:
an - UnsupportedOperationException if haveData is true.
na - IllegalArgumentException if there is no user with such ID.

unregisterUserPrivate

private void unregisterUserPrivate(int uid)
Acutally does the work on unregestering the user


generateBudgetID

public int generateBudgetID()
Creates a new numeric budget id and increases the counter.


generateAccountID

public int generateAccountID()
Creates a new numeric account id and increases the counter.


generateExpenseCategoryID

public int generateExpenseCategoryID()
Creates a new numeric id for an expense category and increases the counter.


generateIncomeSourceID

public int generateIncomeSourceID()
Creates a new numeric id for an income source and increases the counter.


generateUID

public int generateUID()
Creates a new uid for a new user and increases the counter.


generateTransactionID

public long generateTransactionID()
Creates a new numeric transaction id for and increases the counter.


addToList

private static <T extends java.lang.Comparable,E> void addToList(java.util.List<Pair<T,E>> list,
                                                                 T index,
                                                                 E data,
                                                                 java.lang.String name)
Used to add a pair Integer, String and such to sorted lists and maintain sorting. Used in various registerXXXXPrivate methods.


removeFromList

private static <T extends java.lang.Comparable,E> void removeFromList(java.util.List<Pair<T,E>> list,
                                                                      T index,
                                                                      java.lang.String name)
Removes a generic pair index/data (i.e. Integer, String) from a sorted list and maintans sorting. Used in most unregisterXXXXPrivate methods.


getFromList

private static <T extends java.lang.Comparable,E> E getFromList(java.util.List<Pair<T,E>> list,
                                                                T index)
Searches the list of pairs index/data for a pair with a give index and returns the data


indexingMode

public boolean indexingMode()
Returns true if the index is used in indexing only mode. That is, if the DataIndex object does not store any Month objects


toString

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

getXmlElement

public Element getXmlElement()
Returns a JDOM XML Element representation of the current budget


getInstance

public static DataIndex getInstance(Element element)
                             throws JDOMException
Parses JDOM Element into a DataIndex object. The DataIndex object created by this function works in indexing only mode.

Throws:
JDOMException - in case of parsing errors.