|
| 1 | +/* |
| 2 | + * Copyright 2015-2016 Canoo Engineering AG. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package dev.rico.internal.remoting.converters; |
| 17 | + |
| 18 | + |
| 19 | +import dev.rico.remoting.converter.Converter; |
| 20 | +import dev.rico.remoting.converter.ValueConverterException; |
| 21 | + |
| 22 | +import java.time.LocalTime; |
| 23 | +import java.time.format.DateTimeFormatter; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by hendrikebbers on 25.10.16. |
| 29 | + */ |
| 30 | +public class LocalTimeConverterFactory extends AbstractConverterFactory { |
| 31 | + |
| 32 | + @SuppressWarnings("rawtypes") |
| 33 | + private final static Converter CONVERTER = new LocalTimeConverter(); |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean supportsType(Class<?> cls) { |
| 37 | + return LocalTime.class.isAssignableFrom(cls); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public List<Class> getSupportedTypes() { |
| 42 | + return Collections.singletonList(LocalTime.class); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public int getTypeIdentifier() { |
| 47 | + return 1200; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Converter getConverterForType(Class<?> cls) { |
| 52 | + return CONVERTER; |
| 53 | + } |
| 54 | + |
| 55 | + private static class LocalTimeConverter extends AbstractStringConverter<LocalTime> { |
| 56 | + |
| 57 | + @Override |
| 58 | + public LocalTime convertFromRemoting(String value) throws ValueConverterException { |
| 59 | + if (value == null) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + try { |
| 63 | + return LocalTime.from(DateTimeFormatter.ISO_TIME.parse(value)); |
| 64 | + } catch (Exception e) { |
| 65 | + throw new ValueConverterException("Can not convert to LocalTime", e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String convertToRemoting(LocalTime value) throws ValueConverterException { |
| 71 | + if (value == null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + try { |
| 75 | + return value.toString(); |
| 76 | + } catch (Exception e) { |
| 77 | + throw new ValueConverterException("Can not convert from LocalTime", e); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments