001/* 002 * ============================================================================ 003 * Copyright © 2002-2023 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.config.ap.impl.specialprops; 019 020import static org.apiguardian.api.API.Status.STABLE; 021import static org.tquadrat.foundation.config.SpecialPropertyType.CONFIG_PROPERTY_TIMEZONE; 022import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.ALLOWS_PREFERENCES; 023import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.PROPERTY_IS_MUTABLE; 024import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.SETTER_CHECK_NULL; 025import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 026 027import java.time.ZoneId; 028import java.util.Optional; 029import java.util.function.BiFunction; 030 031import org.apiguardian.api.API; 032import org.tquadrat.foundation.annotation.ClassVersion; 033import org.tquadrat.foundation.config.SpecialPropertyType; 034import org.tquadrat.foundation.config.ap.impl.CodeBuilder; 035import org.tquadrat.foundation.config.ap.impl.PropertySpecImpl; 036import org.tquadrat.foundation.config.cli.SimpleCmdLineValueHandler; 037import org.tquadrat.foundation.config.spi.prefs.SimplePreferenceAccessor; 038import org.tquadrat.foundation.javacomposer.ClassName; 039import org.tquadrat.foundation.javacomposer.CodeBlock; 040import org.tquadrat.foundation.javacomposer.TypeName; 041import org.tquadrat.foundation.util.stringconverter.ZoneIdStringConverter; 042 043/** 044 * The implementation of 045 * {@link SpecialPropertySpecBase} 046 * for 047 * {@link SpecialPropertyType#CONFIG_PROPERTY_TIMEZONE}. 048 * 049 * @version $Id: TimeZoneProperty.java 1061 2023-09-25 16:32:43Z tquadrat $ 050 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 051 * @UMLGraph.link 052 * @since 0.1.0 053 */ 054@ClassVersion( sourceVersion = "$Id: TimeZoneProperty.java 1061 2023-09-25 16:32:43Z tquadrat $" ) 055@API( status = STABLE, since = "0.1.0" ) 056public final class TimeZoneProperty extends SpecialPropertySpecBase 057{ 058 /*--------------*\ 059 ====** Constructors **===================================================== 060 \*--------------*/ 061 /** 062 * Creates a new instance of {@code ClockProperty}. 063 */ 064 public TimeZoneProperty() 065 { 066 super( CONFIG_PROPERTY_TIMEZONE, ALLOWS_PREFERENCES, PROPERTY_IS_MUTABLE, SETTER_CHECK_NULL ); 067 } // ClockProperty() 068 069 /*---------*\ 070 ====** Methods **========================================================== 071 \*---------*/ 072 /** 073 * Composes the constructor fragment for the initialisation of this 074 * property. 075 * 076 * @param codeBuilder The factory for the code generation. 077 * @param property The property. 078 * @return The field specification. 079 */ 080 @SuppressWarnings( "TypeMayBeWeakened" ) 081 private static final CodeBlock composeConstructorFragment( final CodeBuilder codeBuilder, @SuppressWarnings( "UseOfConcreteClass" ) final PropertySpecImpl property ) 082 { 083 final var builder = requireNonNullArgument( codeBuilder, "codeBuilder" ).getComposer() 084 .codeBlockBuilder() 085 .add( 086 """ 087 088 /* 089 * Initialise the property '$N'. 090 */ 091 """, property.getPropertyName() 092 ) 093 .addStatement( "$1N = $2T.systemDefault()", property.getFieldName(), ZoneId.class ); 094 095 //---* Create the return value *--------------------------------------- 096 final var retValue = builder.build(); 097 098 //---* Done *---------------------------------------------------------- 099 return retValue; 100 } // composeConstructorFragment() 101 102 /** 103 * {@inheritDoc} 104 */ 105 @Override 106 public final Optional<TypeName> getCLIValueHandlerClass() { return Optional.of( ClassName.from( SimpleCmdLineValueHandler.class ) ); } 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public final Optional<BiFunction<CodeBuilder, PropertySpecImpl, CodeBlock>> getConstructorFragmentComposer() { return Optional.of( TimeZoneProperty::composeConstructorFragment );} 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public final Optional<TypeName> getPrefsAccessorClass() { return Optional.of( ClassName.from( SimplePreferenceAccessor.class ) ); } 119 120 /** 121 * {@inheritDoc} 122 */ 123 @Override 124 public final TypeName getPropertyType() { return ClassName.from( ZoneId.class ); } 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override 130 public final Optional<TypeName> getStringConverterClass() { return Optional.of( ClassName.from( ZoneIdStringConverter.class ) ); } 131} 132// class ClockProperty 133 134/* 135 * End of File 136 */