001     
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006  Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    /*****************************************************************************
021    
022    /*
023     * Converter.java
024     *
025     * Created on July 20, 2006, 7:59 AM
026     */
027    
028    package org.jbudget.io;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    import org.jbudget.Core.Account;
033    import org.jbudget.Core.Budget;
034    import org.jbudget.util.Pair;
035    
036    /**
037     *  Converts objects (such as accounts and budgets) and list of objects to 
038     *  Pairs<Integer, String> and lists of pairs. Also perfoms the reverse
039     *  operation using the DataManager.
040     * @author petrov
041     */
042    public class Converter
043    {
044      /** Creates a new instance of Converter */
045      private Converter()
046      {
047      }
048      
049      /** Returns a Pair<Integer,String> representing a given account */
050      public static Pair<Integer,String> getInfo(Account account){
051        return new Pair<Integer,String>(
052            new Integer(account.getID()), account.getName());
053      }
054      
055      /** Returns the account object represented by a given Pair */
056      public static Account getAccount(Pair<Integer,String>accountInfo){
057        Account account = 
058            DataManager.getInstance().getAccount(accountInfo.first().intValue());
059        assert account == null ||account.getName().equals(accountInfo.second());
060        return account;
061      }
062      
063      /** Returns a Pair<Integer,String> representing a given budget */
064      public static Pair<Integer,String> getInfo(Budget budget){
065        return new Pair<Integer,String>(
066            new Integer(budget.getID()), budget.getName());
067      }
068        
069      /** Returns the budget object represented by a given Pair */
070      public static Budget getBudget(Pair<Integer,String>budgetInfo){
071        Budget budget = 
072            DataManager.getInstance().getBudget(budgetInfo.first().intValue());
073        assert budget == null ||budget.getName().equals(budgetInfo.second());
074        return budget;
075      }
076      
077      /** Converts a list of objects into a list of pairs */
078      public static List<Pair<Integer,String>> getInfo(List<Account> accounts)
079      {
080        List<Pair<Integer,String>> pairs = 
081            new ArrayList<Pair<Integer,String>>(accounts.size());
082        for (Account account : accounts)
083          pairs.add(getInfo(account));
084        return pairs;
085      }
086      
087      /** Converts a list of pairs into a list of accounts */
088      public static List<Account> getAccounts(List<Pair<Integer,String>> pairs)
089      {
090        List<Account> accounts = new ArrayList<Account>(pairs.size());
091        for (Pair<Integer,String> pair : pairs)
092          accounts.add(getAccount(pair));
093        return accounts;
094      }
095      
096      /** Converts a list of objects into a list of pairs */
097      public static List<Pair<Integer,String>> getPairsBudgets(List<Budget> budgets)
098      {
099        List<Pair<Integer,String>> pairs = 
100            new ArrayList<Pair<Integer,String>>(budgets.size());
101        for (Budget budget : budgets)
102          pairs.add(getInfo(budget));
103        return pairs;
104      }
105      
106      /** Converts a list of pairs into a list of budgets */
107      public static List<Budget> getBudgets(List<Pair<Integer,String>> pairs)
108      {
109        List<Budget> budgets = new ArrayList<Budget>(pairs.size());
110        for (Pair<Integer,String> pair : pairs)
111          budgets.add(getBudget(pair));
112        return budgets;
113      }
114      
115    }