Normand Briere
2018-07-07 e416acb9b012b17d1efe49ad2199ea7132d874d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package timeflow.data.db;
 
import timeflow.data.db.filter.*;
import timeflow.data.time.*;
import timeflow.util.*;
 
import java.util.*;
 
public class DBUtils
{
 
        public static void dump(Act act)
        {
                List<Field> fields = act.getDB().getFields();
                for (Field f : fields)
                {
                        System.out.println(f.getName() + " = " + act.get(f));
                }
        }
 
        public static Object get(Act act, String field)
        {
                return act.get(act.getDB().getField(field));
        }
 
        public static List<String> getFieldAliases(ActDB db)
        {
                ArrayList<String> list = new ArrayList<String>();
                for (String s : db.getFieldKeys())
                {
                        if (!db.getField(s).getName().equals(s))
                        {
                                list.add(s);
                        }
                }
                return list;
        }
 
        public static Interval range(ActList a, Field[] fields)
        {
                if (fields == null || fields.length == 0)
                {
                        return new Interval(0, 0);
                }
                Interval t = null;
                for (Act act : a)
                {
                        for (Field f : fields)
                        {
                                RoughTime d = act.getTime(f);
                                if (d != null && d.isDefined())
                                {
                                        if (t == null)
                                        {
                                                t = new Interval(d.getTime(), d.getTime());
                                        } else
                                        {
                                                t.include(d.getTime());
                                        }
                                }
                        }
                }
                return t != null ? t : new Interval(RoughTime.UNKNOWN, RoughTime.UNKNOWN);
        }
 
        public static Interval range(ActList a, String fieldName)
        {
 
                Field field = a.getDB().getField(fieldName);
                if (field == null || a.size() == 0)
                {
                        return new Interval(0, 0);
                }
                Interval t = null;
                for (Act act : a)
                {
                        RoughTime d = act.getTime(field);
                        if (d != null && d.isDefined())
                        {
                                if (t == null)
                                {
                                        t = new Interval(d.getTime(), d.getTime());
                                } else
                                {
                                        t.include(d.getTime());
                                }
                        }
                }
                return t != null ? t : new Interval(RoughTime.UNKNOWN, RoughTime.UNKNOWN);
        }
 
        public static List<Field> categoryFields(ActDB db)
        {
                List<Field> list = new ArrayList<Field>();
                list.addAll(db.getFields());
//                list.addAll(db.getFields(String.class));
//                list.addAll(db.getFields(String[].class));
                return list;
        }
 
        public static int count(Iterable<Act> acts, Interval i, Field field)//String fieldName)
        {
                return count(acts, new TimeIntervalFilter(i, field));
        }
 
        public static int count(Iterable<Act> acts, ActFilter filter)
        {
                int num = 0;
                for (Act a : acts)
                {
                        if (filter.accept(a))
                        {
                                num++;
                        }
                }
                return num;
        }
 
        public static double[] minmax(Iterable<Act> acts, Field field)
        {
                double min = Double.NaN;
                double max = min;
                for (Act a : acts)
                {
                        double x = a.getValue(field);
                        if (Double.isNaN(min))
                        {
                                min = x;
                                max = x;
                        } else if (!Double.isNaN(x))
                        {
                                min = Math.min(x, min);
                                max = Math.max(x, max);
                        }
                }
                return new double[]
                        {
                                min, max
                        };
        }
 
        public static double[] getValues(Iterable<Act> acts, Field field)
        {
                ArrayList<Double> list = new ArrayList<Double>();
                if (field.getType() == Double.class)
                {
                        for (Act a : acts)
                        {
                                list.add(a.getValue(field));
                        }
                } else if (field.getType() == RoughTime.class)
                {
                        for (Act a : acts)
                        {
                                RoughTime r = a.getTime(field);
                                if (r != null)
                                {
                                        list.add(new Double(r.getTime()));
                                }
                        }
                }
                int n = list.size();
                double[] x = new double[n];
                for (int i = 0; i < n; i++)
                {
                        x[i] = list.get(i);
                }
                return x;
        }
 
        public static Bag<String> countValues(Iterable<Act> acts, Field field)
        {
                Bag<String> bag = new Bag<String>();
                if (field.getType() != String[].class)
                {
                        for (Act a : acts)
                        {
                                bag.add(a.getString(field));
                        }
                }
                else // if (field.getType() == String[].class)
                {
                        for (Act a : acts)
                        {
                                String[] s = a.getTextList(field);
                                if (s != null)
                                {
                                        for (int i = 0; i < s.length; i++)
                                        {
                                                bag.add(s[i]);
                                        }
                                }
                        }
//                } else
//                {
//                        throw new IllegalArgumentException("Asked to count values for non-text field: " + field);
                }
                return bag;
        }
 
        public static void setRecSizesFromCurrent(ActDB db)
        {
                // for String fields.
                for (Field f : db.getFields(String.class))
                {
                        int max = 0;
                        for (Act a : db)
                        {
                                String s = a.getString(f);
                                if (s != null)
                                {
                                        max = Math.max(s.length(), max);
                                }
                        }
                        f.setRecommendedSize(max);
                }
        }
 
        public static Field ensureField(ActDB db, String name, Class type)
        {
                Field f = db.getField(name);
                if (f == null)
                {
                        return db.addField(name, type);
                } else
                {
                        if (f.getType() != type)
                        {
                                throw new IllegalArgumentException("Mismatched types: got " + type + ", expected " + f.getType());
                        }
                }
                return f;
        }
}