%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
ca.spaz.cron.database.Serving |
|
|
1 | /* |
|
2 | * Created on Apr 2, 2005 by davidson |
|
3 | */ |
|
4 | package ca.spaz.cron.database; |
|
5 | ||
6 | import java.util.*; |
|
7 | ||
8 | import org.apache.log4j.Logger; |
|
9 | ||
10 | import ca.spaz.cron.datasource.ILocalFoodDatasource; |
|
11 | ||
12 | /** |
|
13 | * Stores an amount and time of a food serving |
|
14 | * |
|
15 | * @author davidson |
|
16 | */ |
|
17 | 10 | public class Serving { |
18 | /** |
|
19 | * Logger for this class |
|
20 | */ |
|
21 | 13 | private static final Logger logger = Logger.getLogger(Serving.class); |
22 | ||
23 | 40 | private int ID = -1; |
24 | ||
25 | private Food food; |
|
26 | ||
27 | private double grams; |
|
28 | 40 | private Measure measure = Measure.GRAM; |
29 | ||
30 | private Date date; |
|
31 | ||
32 | private int meal; |
|
33 | ||
34 | 80 | public Serving() {} |
35 | ||
36 | public Serving(Food f) { |
|
37 | 0 | this(f, 1.0); |
38 | 0 | } |
39 | ||
40 | 0 | public Serving(Food f, double grams) { |
41 | 0 | this.food = f; |
42 | 0 | this.grams = grams; |
43 | 0 | this.date = new Date(System.currentTimeMillis()); |
44 | 0 | this.meal = -1; |
45 | 0 | } |
46 | ||
47 | ||
48 | public double getGrams() { |
|
49 | 0 | return grams; |
50 | } |
|
51 | ||
52 | ||
53 | public double getAmount() { |
|
54 | 0 | assert(getMeasure() != null); |
55 | 0 | return getGrams()/getMeasure().getGrams(); |
56 | } |
|
57 | ||
58 | ||
59 | public void setAmount(double val) { |
|
60 | 0 | assert(getMeasure() != null); |
61 | // TODO: verify this is correct with measures that have Amount != 1 |
|
62 | 0 | setGrams(getMeasure().getGrams() * val); |
63 | 0 | } |
64 | ||
65 | public Food getFood() { |
|
66 | 60 | return food; |
67 | } |
|
68 | ||
69 | public Date getDate() { |
|
70 | 0 | return date; |
71 | } |
|
72 | ||
73 | public void setDate(Date d) { |
|
74 | 0 | this.date = d; |
75 | 0 | } |
76 | ||
77 | /** |
|
78 | * @return true if the object is already in the database |
|
79 | */ |
|
80 | public boolean exists() { |
|
81 | 0 | return ID != -1; |
82 | } |
|
83 | ||
84 | /** |
|
85 | * Add the current food to the current day |
|
86 | */ |
|
87 | /* public void addToDatabase() { |
|
88 | ILocalFoodDatasource lds = (ILocalFoodDatasource) getFood().getDataSource(); |
|
89 | lds.consumeFood(this); |
|
90 | } |
|
91 | */ |
|
92 | /** |
|
93 | * Update the existing food information |
|
94 | */ |
|
95 | public void updateDatabase() { |
|
96 | 0 | ILocalFoodDatasource lds = (ILocalFoodDatasource) getFood().getDataSource(); |
97 | 0 | lds.changeServingAmount(this); |
98 | 0 | } |
99 | ||
100 | public void deleteFromDatabase() { |
|
101 | 0 | ILocalFoodDatasource lds = (ILocalFoodDatasource) getFood().getDataSource(); |
102 | 0 | lds.removeServing(this); |
103 | 0 | } |
104 | ||
105 | public void setGrams(double amount) { |
|
106 | 0 | this.grams = amount; |
107 | 0 | } |
108 | ||
109 | /** |
|
110 | * @return the ID of the meal this serving was eaten at. |
|
111 | */ |
|
112 | public int getMeal() { |
|
113 | 0 | return meal; |
114 | } |
|
115 | ||
116 | /** |
|
117 | * @return Returns the iD. |
|
118 | */ |
|
119 | public int getID() { |
|
120 | 0 | return ID; |
121 | } |
|
122 | /** |
|
123 | * @param id The ID to set. |
|
124 | */ |
|
125 | public void setID(int id) { |
|
126 | 0 | ID = id; |
127 | 0 | } |
128 | /** |
|
129 | * @return Returns the measure. |
|
130 | */ |
|
131 | public Measure getMeasure() { |
|
132 | 0 | return measure; |
133 | } |
|
134 | /** |
|
135 | * @param measure The measure to set. |
|
136 | */ |
|
137 | public void setMeasure(Measure measure) { |
|
138 | 0 | this.measure = measure; |
139 | 0 | } |
140 | /** |
|
141 | * @param meal The meal to set. |
|
142 | */ |
|
143 | public void setMeal(int meal) { |
|
144 | 0 | this.meal = meal; |
145 | 0 | } |
146 | ||
147 | /** |
|
148 | * @param food |
|
149 | */ |
|
150 | public void setFood(Food food) { |
|
151 | 0 | this.food = food; |
152 | // map existing measure to a matching measure in this food |
|
153 | // this is a hack to work around converting from a food |
|
154 | // in one datasource to another... |
|
155 | 0 | if (food == null) return; |
156 | 0 | List measures = getFood().getMeasures(); |
157 | 0 | if (measures == null) return; |
158 | 0 | for (int i=0; i<measures.size(); i++) { |
159 | 0 | Measure m = (Measure)measures.get(i); |
160 | 0 | if (m == measure) return; |
161 | 0 | if (m.getGrams() == measure.getGrams()) { |
162 | 0 | if (m.getDescription().equals(measure.getDescription())) { |
163 | 0 | setMeasure(m); |
164 | } |
|
165 | } |
|
166 | } |
|
167 | 0 | } |
168 | ||
169 | public void setMeasure(int MID) { |
|
170 | 0 | List measures = getFood().getMeasures(); |
171 | 0 | for (int i=0; i<measures.size(); i++) { |
172 | 0 | Measure m = (Measure)measures.get(i); |
173 | 0 | if (m.getID() == MID) setMeasure(m); |
174 | } |
|
175 | 0 | } |
176 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |