]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/csv/ExportInterpolation.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / csv / ExportInterpolation.java
1 package org.simantics.history.csv;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 \r
6 /**\r
7  * Specifies the interpolation method to use when producing exported samples in\r
8  * {@link CSVFormatter}.\r
9  * \r
10  * @author Tuukka Lehtonen\r
11  * @see CSVFormatter\r
12  */\r
13 public enum ExportInterpolation {\r
14 \r
15         /**\r
16          * Directly take the last sample whose time stamp is less than or equal to\r
17          * the sampling time.\r
18          */\r
19         PREVIOUS_SAMPLE("Previous sample", "previous"),\r
20 \r
21         /**\r
22          * <pre>\r
23          * v<sub>1</sub>               v<sub>s</sub>                   v<sub>2</sub>\r
24          * |----------------|--------------------|\r
25          * t<sub>1</sub>               t<sub>s</sub>                   t<sub>2</sub>\r
26          * </pre>\r
27          * \r
28          * Here t<sub>1</sub> and t<sub>2</sub> are time stamps of those two\r
29          * successive samples for which t<sub>1</sub> &le; t<sub>s</sub> &le;\r
30          * t<sub>2</sub>.\r
31          * \r
32          * <p>\r
33          * The sampling result is then:\r
34          * <code>v<sub>s</sub> = v<sub>1</sub>+(v<sub>2</sub>-v<sub>1</sub>)*(t<sub>s</sub>-t<sub>1</sub>)/(t<sub>2</sub>-t<sub>1</sub>).\r
35          */\r
36         LINEAR_INTERPOLATION("Linear interpolation", "lerp");\r
37 \r
38         public String label;\r
39         public String preference;\r
40         ExportInterpolation(String label, String preference) {\r
41                 this.label = label;\r
42                 this.preference = preference;\r
43         }\r
44         public static List<ExportInterpolation> list() {\r
45                 return toList(LINEAR_INTERPOLATION, PREVIOUS_SAMPLE);\r
46         }\r
47         public static ExportInterpolation fromIndex(int index) {\r
48                 return list().get(index);\r
49         }\r
50         public static ExportInterpolation fromPreference(String preference) {\r
51                 for(ExportInterpolation s : list()) {\r
52                         if(s.preference.equals(preference)) return s;\r
53                 }\r
54                 return list().get(0);\r
55         }\r
56         public int index() {\r
57                 return list().indexOf(this);\r
58         }\r
59         public static ExportInterpolation getDefault() {\r
60                 return LINEAR_INTERPOLATION;\r
61         }\r
62         public String toPreference() {\r
63                 return preference;\r
64         }\r
65         private static <T> List<T> toList(T ... ts) {\r
66                 ArrayList<T> result = new ArrayList<T>();\r
67                 for(T t : ts) result.add(t);\r
68                 return result;\r
69         }\r
70 }\r