001/* 002 * ============================================================================ 003 * Copyright © 2002-2024 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * 007 * Licensed to the public under the agreements of the GNU Lesser General Public 008 * License, version 3.0 (the "License"). You may obtain a copy of the License at 009 * 010 * http://www.gnu.org/licenses/lgpl.html 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 015 * License for the specific language governing permissions and limitations 016 * under the License. 017 */ 018 019package org.tquadrat.foundation.util.stringconverter; 020 021import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE; 022import static org.apiguardian.api.API.Status.STABLE; 023 024import java.io.Serial; 025import java.time.LocalDate; 026import java.time.format.DateTimeFormatter; 027import java.time.format.DateTimeParseException; 028import java.util.Optional; 029 030import org.apiguardian.api.API; 031import org.tquadrat.foundation.annotation.ClassVersion; 032 033/** 034 * The implementation of 035 * {@link TimeDateStringConverter} 036 * for {@code java.time.LocalDate}. 037 * 038 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 039 * @version $Id: LocalDateStringConverter.java 1130 2024-05-05 16:16:09Z tquadrat $ 040 * @since 0.0.6 041 * 042 * @UMLGraph.link 043 */ 044@ClassVersion( sourceVersion = "$Id: LocalDateStringConverter.java 1130 2024-05-05 16:16:09Z tquadrat $" ) 045@API( status = STABLE, since = "0.0.6" ) 046public class LocalDateStringConverter extends TimeDateStringConverter<LocalDate> 047{ 048 /*------------------------*\ 049 ====** Static Initialisations **=========================================== 050 \*------------------------*/ 051 /** 052 * The serial version UID for objects of this class: {@value}. 053 * 054 * @hidden 055 */ 056 @Serial 057 private static final long serialVersionUID = 1L; 058 059 /** 060 * An instance of this class. 061 */ 062 public static final LocalDateStringConverter INSTANCE = new LocalDateStringConverter(); 063 064 /*--------------*\ 065 ====** Constructors **===================================================== 066 \*--------------*/ 067 /** 068 * Creates a new {@code LocalDateStringConverter} instance. 069 */ 070 public LocalDateStringConverter() { super( LocalDate.class ); } 071 072 /** 073 * Creates a new {@code LocalDateStringConverter} instance. 074 * 075 * @note The formatter may not drop any part of the local date, otherwise 076 * {@code fromString()} may fail. This means that the formatter is 077 * only allowed to re-order the temporal fields. 078 * 079 * @param formatter The formatter for the temporal accessor. 080 */ 081 public LocalDateStringConverter( final DateTimeFormatter formatter ) 082 { 083 super( LocalDate.class, formatter ); 084 } // LocalDateStringConverter() 085 086 /*---------*\ 087 ====** Methods **========================================================== 088 \*---------*/ 089 /** 090 * {@inheritDoc} 091 */ 092 @Override 093 protected final LocalDate parseDateTime( final CharSequence source, final Optional<DateTimeFormatter> formatter ) throws DateTimeParseException 094 { 095 final var retValue = LocalDate.from( formatter.orElse( ISO_LOCAL_DATE ).parse( source ) ); 096 097 //---* Done *---------------------------------------------------------- 098 return retValue; 099 } // parseDateTime() 100 101 /** 102 * This method is used by the 103 * {@link java.util.ServiceLoader} 104 * to obtain the instance for this 105 * {@link org.tquadrat.foundation.lang.StringConverter} 106 * implementation. 107 * 108 * @return The instance for this {@code StringConverter} implementation. 109 */ 110 public static final LocalDateStringConverter provider() { return INSTANCE; } 111} 112// class LocalDateStringConverter 113 114/* 115 * End of File 116 */