BrightSide Workbench Full Report + Source Code
OptionDataType.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.polls;
19 
20 import java.util.Date;
21 import org.turro.string.ObjectString;
22 
27 public enum OptionDataType {
28 
29  OPTION_STRING("String"),
30  OPTION_DATETIME("DateTime"),
31  OPTION_DATE("Date"),
32  OPTION_TIME("Time"),
33  OPTION_LONG("Long");
34 
35  private String type;
36 
37  private OptionDataType(String type) {
38  this.type = type;
39  }
40 
41  public String getType() {
42  return type;
43  }
44 
45  public Object getValue(String value) {
46  switch (this) {
47  case OPTION_DATETIME:
48  return (Date) ObjectString.parseString((String) value, POLL_DATETIME_PATTERN, Date.class, false);
49  case OPTION_DATE:
50  return (Date) ObjectString.parseString((String) value, POLL_DATE_PATTERN, Date.class, false);
51  case OPTION_TIME:
52  return (Date) ObjectString.parseString((String) value, POLL_TIME_PATTERN, Date.class, false);
53  case OPTION_LONG:
54  return (Long) ObjectString.parseString((String) value, Long.class, true);
55  default:
56  return value;
57  }
58  }
59 
60  public String getString(Object value) {
61  switch (this) {
62  case OPTION_DATETIME:
63  return ObjectString.formatObject(value, POLL_DATETIME_PATTERN, false);
64  case OPTION_DATE:
65  return ObjectString.formatObject(value, POLL_DATE_PATTERN, false);
66  case OPTION_TIME:
67  return ObjectString.formatObject(value, POLL_TIME_PATTERN, false);
68  case OPTION_LONG:
69  return ObjectString.formatObject(value, false);
70  default:
71  return (String) value;
72  }
73  }
74 
75  public static final String
76  POLL_DATETIME_PATTERN = "yyyy-MM-dd HH:mm",
77  POLL_DATE_PATTERN = "yyyy-MM-dd",
78  POLL_TIME_PATTERN = "HH:mm";
79 
80  public static OptionDataType getOptionType(String value) {
81  for(OptionDataType odt : values()) {
82  if(odt.getType().equals(value)) {
83  return odt;
84  }
85  }
86  return OPTION_STRING;
87  }
88 
89 }
String getString(Object value)
Object getValue(String value)
static OptionDataType getOptionType(String value)