cleanup
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / util / formatter / PartialFormatter.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.util.formatter;
10
11 import org.joda.time.Partial;
12 import org.joda.time.format.DateTimeFormatter;
13 import org.joda.time.format.ISODateTimeFormat;
14
15 import eu.etaxonomy.cdm.model.common.TimePeriod;
16
17 /**
18 * @author a.kohlbecker
19 * @since Apr 28, 2017
20 */
21 public class PartialFormatter {
22
23 private DateTimeFormat format;
24
25 private static final DateTimeFormatter dmyDotDayMonthYear = org.joda.time.format.DateTimeFormat.forPattern("dd.MM.y");
26
27 private static final DateTimeFormatter dmyDotMonthYear = org.joda.time.format.DateTimeFormat.forPattern("MM.y");
28
29 private static final DateTimeFormatter dmyDotYear = org.joda.time.format.DateTimeFormat.forPattern("y");
30
31 public PartialFormatter(DateTimeFormat format) {
32 this.format = format;
33 }
34
35 public String print(Partial partial) {
36 switch (format) {
37 case DMY_DOT:
38 return partial.toString(determine_DMY_DOT_Formatter(partial));
39 case ISO8601_DATE:
40 default:
41 return partial.toString(determine_ISO860_Formatter(partial));
42 }
43 }
44
45 private DateTimeFormatter determine_ISO860_Formatter(Partial partial) {
46 if (partial.isSupported(TimePeriod.DAY_TYPE)) {
47 return ISODateTimeFormat.yearMonthDay();
48 }
49 if (partial.isSupported(TimePeriod.MONTH_TYPE)) {
50 return ISODateTimeFormat.yearMonth();
51 }
52 return ISODateTimeFormat.year();
53 }
54
55 private DateTimeFormatter determine_DMY_DOT_Formatter(Partial partial) {
56 if (partial.isSupported(TimePeriod.DAY_TYPE)) {
57 return dmyDotDayMonthYear;
58 }
59 if (partial.isSupported(TimePeriod.MONTH_TYPE)) {
60 return dmyDotMonthYear;
61 }
62 return dmyDotYear;
63 }
64 }