1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.util.converter;
  27 
  28 import java.time.LocalTime;
  29 import java.time.format.DateTimeFormatter;
  30 import java.time.format.FormatStyle;
  31 import java.util.Locale;
  32 
  33 import javafx.util.StringConverter;
  34 import javafx.util.converter.LocalDateTimeStringConverter.LdtConverter;
  35 
  36 /**
  37  * <p>{@link StringConverter} implementation for {@link LocalTime} values.</p>
  38  *
  39  * @see LocalDateStringConverter
  40  * @see LocalDateTimeStringConverter
  41  * @since JavaFX 8u40
  42  */
  43 public class LocalTimeStringConverter extends StringConverter<LocalTime> {
  44 
  45     LdtConverter<LocalTime> ldtConverter;
  46 
  47     // ------------------------------------------------------------ Constructors
  48 
  49     /**
  50      * Create a {@link StringConverter} for {@link LocalTime} values, using a
  51      * default formatter and parser with {@link FormatStyle#SHORT}, and the
  52      * user's {@link Locale}.
  53      */
  54     public LocalTimeStringConverter() {
  55         ldtConverter = new LdtConverter<LocalTime>(LocalTime.class, null, null,
  56                                                   null, null, null, null);
  57     }
  58 
  59     /**
  60      * Create a {@link StringConverter} for {@link LocalTime} values, using a
  61      * default formatter and parser with the specified {@link FormatStyle} and
  62      * based on the user's {@link Locale}.
  63      *
  64      * @param timeStyle The {@link FormatStyle} that will be used by the default
  65      * formatter and parser. If null then {@link FormatStyle#SHORT} will be used.
  66      */
  67     public LocalTimeStringConverter(FormatStyle timeStyle) {
  68         ldtConverter = new LdtConverter<LocalTime>(LocalTime.class, null, null,
  69                                                   null, timeStyle, null, null);
  70     }
  71 
  72     /**
  73      * Create a StringConverter for {@link LocalTime} values, using a
  74      * default formatter and parser with the specified {@link FormatStyle}
  75      * and {@link Locale}.
  76      *
  77      * @param timeStyle The {@link FormatStyle} that will be used by the default
  78      * formatter and parser. If null then {@link FormatStyle#SHORT} will be used.
  79      * @param locale The {@link Locale} that will be used by the default
  80      * formatter and parser. If null then
  81      * {@code Locale.getDefault(Locale.Category.FORMAT)} will be used.
  82      */
  83     public LocalTimeStringConverter(FormatStyle timeStyle, Locale locale) {
  84         ldtConverter = new LdtConverter<LocalTime>(LocalTime.class, null, null,
  85                                                   null, timeStyle, locale, null);
  86     }
  87 
  88     /**
  89      * Create a StringConverter for {@link LocalTime} values using the
  90      * supplied formatter and parser, which are responsible for
  91      * choosing the desired {@link Locale}.
  92      *
  93      * <p>For example, a fixed pattern can be used for converting both ways:</p>
  94      * <blockquote><pre>
  95      * String pattern = "HH:mm:ss";
  96      * DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
  97      * StringConverter<LocalTime> converter =
  98      *     DateTimeStringConverter.getLocalTimeConverter(formatter, null);
  99      * </pre></blockquote>
 100      *
 101      * @param formatter An instance of {@link DateTimeFormatter} which
 102      * will be used for formatting by the toString() method. If null
 103      * then a default formatter will be used.
 104      * @param parser An instance of {@link DateTimeFormatter} which
 105      * will be used for parsing by the fromString() method. This can
 106      * be identical to formatter. If null, then formatter will be
 107      * used, and if that is also null, then a default parser will be
 108      * used.
 109      */
 110     public LocalTimeStringConverter(DateTimeFormatter formatter, DateTimeFormatter parser) {
 111         ldtConverter = new LdtConverter<LocalTime>(LocalTime.class, formatter, parser,
 112                                                    null, null, null, null);
 113     }
 114 
 115     // ------------------------------------------------------- Converter Methods
 116 
 117     /** {@inheritDoc} */
 118     @Override public LocalTime fromString(String value) {
 119         return ldtConverter.fromString(value);
 120     }
 121 
 122     /** {@inheritDoc} */
 123     @Override public String toString(LocalTime value) {
 124         return ldtConverter.toString(value);
 125     }
 126 }