LocalTimePropertyEditor.java
001 /*
002  * Copyright 2008-2016 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.core.editors;
017 
018 import griffon.core.formatters.Formatter;
019 import griffon.core.formatters.LocalTimeFormatter;
020 import griffon.metadata.PropertyEditorFor;
021 
022 import java.time.LocalDateTime;
023 import java.time.LocalTime;
024 import java.time.format.DateTimeParseException;
025 import java.util.ArrayList;
026 import java.util.Calendar;
027 import java.util.Date;
028 import java.util.List;
029 
030 import static griffon.util.GriffonNameUtils.isBlank;
031 
032 /**
033  @author Andres Almiray
034  @since 2.4.0
035  */
036 @PropertyEditorFor(LocalTime.class)
037 public class LocalTimePropertyEditor extends AbstractPropertyEditor {
038     protected void setValueInternal(Object value) {
039         if (null == value) {
040             super.setValueInternal(null);
041         else if (value instanceof CharSequence) {
042             handleAsString(String.valueOf(value));
043         else if (value instanceof LocalTime) {
044             super.setValueInternal(value);
045         else if (value instanceof LocalDateTime) {
046             super.setValueInternal(((LocalDateTimevalue).toLocalTime());
047         else if (value instanceof Date) {
048             handleAsDate((Datevalue);
049         else if (value instanceof Calendar) {
050             handleAsCalendar((Calendarvalue);
051         else if (value instanceof Number) {
052             handleAsDate(new Date(((Numbervalue).longValue()));
053         else if (value instanceof List) {
054             handleAsList((Listvalue);
055         else {
056             throw illegalValue(value, LocalTime.class);
057         }
058     }
059 
060     protected void handleAsDate(Date date) {
061         Calendar c = Calendar.getInstance();
062         c.setTime(date);
063         handleAsCalendar(c);
064     }
065 
066     protected void handleAsCalendar(Calendar value) {
067         int h = value.get(Calendar.HOUR);
068         int i = value.get(Calendar.MINUTE);
069         int s = value.get(Calendar.SECOND);
070         int n = value.get(Calendar.MILLISECOND1000;
071         super.setValueInternal(LocalTime.of(h, i, s, n));
072     }
073 
074     protected void handleAsString(String str) {
075         if (isBlank(str)) {
076             super.setValueInternal(null);
077             return;
078         }
079 
080         try {
081             super.setValueInternal(LocalTime.parse(str));
082         catch (DateTimeParseException dtpe) {
083             throw illegalValue(str, LocalTime.class, dtpe);
084         }
085     }
086 
087     protected Formatter<LocalTime> resolveFormatter() {
088         return isBlank(getFormat()) null new LocalTimeFormatter(getFormat());
089     }
090 
091     protected void handleAsList(List<?> list) {
092         if (list.isEmpty()) {
093             super.setValueInternal(null);
094             return;
095         }
096 
097         List<Object> values = new ArrayList<>();
098         values.addAll(list);
099         switch (values.size()) {
100             case 4:
101                 // ok
102                 break;
103             case 3:
104                 values.add(0);
105                 break;
106             default:
107                 throw illegalValue(list, LocalTime.class);
108         }
109 
110         for (int i = 0, valuesSize = values.size(); i < valuesSize; i++) {
111             Object val = values.get(i);
112             if (val instanceof Number) {
113                 values.set(i, parse((Numberval));
114             else if (val instanceof CharSequence) {
115                 values.set(i, parse(String.valueOf(val)));
116             else {
117                 throw illegalValue(list, LocalTime.class);
118             }
119         }
120         super.setValueInternal(
121             LocalTime.of(
122                 (Integervalues.get(0),
123                 (Integervalues.get(1),
124                 (Integervalues.get(2),
125                 (Integervalues.get(3)
126             )
127         );
128     }
129 
130     protected int parse(String val) {
131         try {
132             return Integer.parseInt(val.trim());
133         catch (NumberFormatException e) {
134             throw illegalValue(val, LocalTime.class, e);
135         }
136     }
137 
138     protected int parse(Number val) {
139         return val.intValue();
140     }
141 }