Browse code

initial import

devnewton authored on 27/06/2014 at 13:01:19
Showing 10 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
+    <modelVersion>4.0.0</modelVersion>
3
+
4
+    <groupId>im.bci</groupId>
5
+    <artifactId>preumsor</artifactId>
6
+    <version>1.0-SNAPSHOT</version>
7
+    <packaging>jar</packaging>
8
+
9
+    <name>preums</name>
10
+    <url>http://maven.apache.org</url>
11
+
12
+    <properties>
13
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14
+    </properties>
15
+
16
+    <dependencies>
17
+        <dependency>
18
+            <groupId>net.sf.supercsv</groupId>
19
+            <artifactId>super-csv-dozer</artifactId>
20
+            <version>2.2.0</version>
21
+        </dependency>
22
+        <dependency>
23
+            <groupId>joda-time</groupId>
24
+            <artifactId>joda-time</artifactId>
25
+            <version>2.3</version>
26
+        </dependency>
27
+        <dependency>
28
+            <groupId>org.apache.commons</groupId>
29
+            <artifactId>commons-math3</artifactId>
30
+            <version>3.3</version>
31
+        </dependency>
32
+    </dependencies>
33
+</project>
0 34
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+package im.bci.preumsor;
1
+
2
+import org.joda.time.DateTime;
3
+
4
+/**
5
+ *
6
+ * @author devnewton
7
+ */
8
+public class DateUtils {
9
+
10
+    public static boolean isSameDay(DateTime a, DateTime b) {
11
+        if(null == a && b == null) {
12
+            return true;
13
+        }
14
+        if(null != a && null != b) {
15
+            return a.getYear() == b.getYear() && a.getMonthOfYear() == b.getMonthOfYear() && a.getDayOfMonth() == b.getDayOfMonth();
16
+        }
17
+        return false;
18
+    }
19
+    
20
+}
0 21
new file mode 100644
... ...
@@ -0,0 +1,51 @@
0
+package im.bci.preumsor;
1
+
2
+import im.bci.preumsor.handlers.MoulingDayPrinter;
3
+import im.bci.preumsor.handlers.MoulingDayStatsPrinter;
4
+import java.io.File;
5
+import java.io.FileNotFoundException;
6
+import java.io.FileReader;
7
+import java.io.IOException;
8
+import org.supercsv.cellprocessor.ParseDate;
9
+import org.supercsv.cellprocessor.ParseEnum;
10
+import org.supercsv.cellprocessor.ift.CellProcessor;
11
+import org.supercsv.io.dozer.CsvDozerBeanReader;
12
+import org.supercsv.io.dozer.ICsvDozerBeanReader;
13
+import org.supercsv.prefs.CsvPreference;
14
+
15
+public class Main {
16
+
17
+    public static void main(String[] args) throws FileNotFoundException, IOException {
18
+        final CellProcessor[] processors = new CellProcessor[]{
19
+            new ParseDate("yyyyMMddHHmmss"),
20
+            new ParseEnum(PostType.class)
21
+        };
22
+
23
+        ICsvDozerBeanReader beanReader = null;
24
+        try {
25
+            beanReader = new CsvDozerBeanReader(new FileReader(new File(System.getProperty("user.home"),"tmp/preums.csv")), CsvPreference.STANDARD_PREFERENCE);
26
+
27
+            beanReader.getHeader(true); // ignore the header
28
+            String[] FIELD_MAPPING = new String[] {
29
+                "timestamp",
30
+                "type"
31
+            };
32
+            beanReader.configureBeanMapping(Post.class, FIELD_MAPPING);
33
+
34
+            Post post;
35
+            MoulingDayGenerator wd = new MoulingDayGenerator();
36
+            wd.addWorkingDayHandller(new MoulingDayPrinter());
37
+            wd.addWorkingDayHandller(new MoulingDayStatsPrinter());
38
+            while ((post = beanReader.read(Post.class, processors)) != null) {
39
+                //System.out.println(String.format("lineNo=%s, rowNo=%s, post=%s", beanReader.getLineNumber(),beanReader.getRowNumber(), post));
40
+                wd.handlePost(post);
41
+            }
42
+            wd.finish();
43
+
44
+        } finally {
45
+            if (beanReader != null) {
46
+                beanReader.close();
47
+            }
48
+        }
49
+    }
50
+}
0 51
new file mode 100644
... ...
@@ -0,0 +1,48 @@
0
+package im.bci.preumsor;
1
+
2
+import org.joda.time.DateTime;
3
+
4
+/**
5
+ *
6
+ * @author devnewton
7
+ */
8
+public class MoulingDay {
9
+
10
+    private DateTime preums;
11
+    private DateTime deurns;
12
+
13
+    public DateTime getPreums() {
14
+        return preums;
15
+    }
16
+
17
+    public void setPreums(DateTime preums) {
18
+        this.preums = preums;
19
+    }
20
+
21
+    public DateTime getDeurns() {
22
+        return deurns;
23
+    }
24
+
25
+    public void setDeurns(DateTime deurns) {
26
+        this.deurns = deurns;
27
+    }
28
+
29
+    boolean isValid() {
30
+        if (null == preums) {
31
+            return false;
32
+        }
33
+        if (null == deurns) {
34
+            return false;
35
+        }
36
+        if (preums.isAfter(deurns)) {
37
+            return false;
38
+        }
39
+        return DateUtils.isSameDay(preums, deurns);
40
+    }
41
+
42
+    @Override
43
+    public String toString() {
44
+        return "WorkingDay{" + "preums=" + preums + ", deurns=" + deurns + '}';
45
+    }
46
+
47
+}
0 48
new file mode 100644
... ...
@@ -0,0 +1,70 @@
0
+package im.bci.preumsor;
1
+
2
+import java.util.ArrayList;
3
+import java.util.List;
4
+import org.joda.time.DateTime;
5
+
6
+/**
7
+ *
8
+ * @author devnewton
9
+ */
10
+public class MoulingDayGenerator {
11
+
12
+    private MoulingDay currentDay;
13
+    private List<MoulingDayHandler> workingDayHandlers = new ArrayList<MoulingDayHandler>();
14
+
15
+    public void addWorkingDayHandller(MoulingDayHandler d) {
16
+        workingDayHandlers.add(d);
17
+    }
18
+
19
+    public void handlePost(Post post) {
20
+        switch (post.getType()) {
21
+            case preums:
22
+                handlePreums(post);
23
+                break;
24
+            case deurns:
25
+                handleDeurns(post);
26
+        }
27
+    }
28
+
29
+    public void finish() {
30
+        handleCurrentWorkingDay();
31
+        for (MoulingDayHandler h : workingDayHandlers) {
32
+            h.finish();
33
+        }
34
+    }
35
+
36
+    private void handlePreums(Post post) {
37
+        final DateTime preums = new DateTime(post.getTimestamp());
38
+        if (null == currentDay) {
39
+            currentDay = new MoulingDay();
40
+            currentDay.setPreums(preums);
41
+        } else {
42
+            if (!DateUtils.isSameDay(preums, currentDay.getPreums())) {
43
+                handleCurrentWorkingDay();
44
+            }
45
+        }
46
+    }
47
+
48
+    private void handleDeurns(Post post) {
49
+        if (null != currentDay) {
50
+            currentDay.setDeurns(new DateTime(post.getTimestamp()));
51
+        }
52
+    }
53
+
54
+    private void handleCurrentWorkingDay() {
55
+        if (null != currentDay) {
56
+            if (currentDay.isValid()) {
57
+                handleWorkingDay(currentDay);
58
+            }
59
+            currentDay = null;
60
+        }
61
+    }
62
+
63
+    private void handleWorkingDay(MoulingDay d) {
64
+        for (MoulingDayHandler h : workingDayHandlers) {
65
+            h.handleWorkingDay(d);
66
+        }
67
+    }
68
+
69
+}
0 70
new file mode 100644
... ...
@@ -0,0 +1,10 @@
0
+package im.bci.preumsor;
1
+
2
+/**
3
+ *
4
+ * @author devnewton
5
+ */
6
+public interface MoulingDayHandler {
7
+    void handleWorkingDay(MoulingDay d);
8
+    void finish();
9
+}
0 10
new file mode 100644
... ...
@@ -0,0 +1,35 @@
0
+package im.bci.preumsor;
1
+
2
+import java.util.Date;
3
+
4
+/**
5
+ *
6
+ * @author devnewton
7
+ */
8
+public class Post {
9
+
10
+    private Date timestamp;
11
+    private PostType type;
12
+
13
+    public Date getTimestamp() {
14
+        return timestamp;
15
+    }
16
+
17
+    @Override
18
+    public String toString() {
19
+        return "Post{" + "timestamp=" + timestamp + ", type=" + type + '}';
20
+    }
21
+
22
+    public void setTimestamp(Date timestamp) {
23
+        this.timestamp = timestamp;
24
+    }
25
+
26
+    public PostType getType() {
27
+        return type;
28
+    }
29
+
30
+    public void setType(PostType type) {
31
+        this.type = type;
32
+    }
33
+
34
+}
0 35
new file mode 100644
... ...
@@ -0,0 +1,16 @@
0
+/*
1
+ * To change this license header, choose License Headers in Project Properties.
2
+ * To change this template file, choose Tools | Templates
3
+ * and open the template in the editor.
4
+ */
5
+package im.bci.preumsor;
6
+
7
+/**
8
+ *
9
+ * @author devnewton
10
+ */
11
+public enum PostType {
12
+
13
+    preums,
14
+    deurns
15
+}
0 16
new file mode 100644
... ...
@@ -0,0 +1,20 @@
0
+package im.bci.preumsor.handlers;
1
+
2
+import im.bci.preumsor.MoulingDay;
3
+import im.bci.preumsor.MoulingDayHandler;
4
+import org.joda.time.Duration;
5
+
6
+/**
7
+ *
8
+ * @author devnewton
9
+ */
10
+public class MoulingDayPrinter implements MoulingDayHandler {
11
+
12
+    public void handleWorkingDay(MoulingDay d) {
13
+        System.out.println(d.getPreums().toString("yyyy-MM-dd") + "," + new Duration(d.getPreums(), d.getDeurns()).toStandardSeconds().getSeconds());
14
+    }
15
+
16
+    public void finish() {
17
+    }
18
+
19
+}
0 20
new file mode 100644
... ...
@@ -0,0 +1,24 @@
0
+package im.bci.preumsor.handlers;
1
+
2
+import im.bci.preumsor.MoulingDay;
3
+import im.bci.preumsor.MoulingDayHandler;
4
+import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
5
+import org.joda.time.Duration;
6
+
7
+/**
8
+ *
9
+ * @author devnewton
10
+ */
11
+public class MoulingDayStatsPrinter implements MoulingDayHandler {
12
+
13
+    private final DescriptiveStatistics stats = new DescriptiveStatistics();
14
+
15
+    public void handleWorkingDay(MoulingDay d) {
16
+        stats.addValue(new Duration(d.getPreums(), d.getDeurns()).toStandardSeconds().getSeconds() / 3600.0);
17
+    }
18
+
19
+    public void finish() {
20
+        System.out.println(stats);
21
+    }
22
+
23
+}