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 javax.lang.model.element.Modifier.FINAL; 021import static javax.lang.model.element.Modifier.PRIVATE; 022import static org.apiguardian.api.API.Status.STABLE; 023import static org.tquadrat.foundation.config.SpecialPropertyType.CONFIG_PROPERTY_RANDOM; 024import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.EXEMPT_FROM_TOSTRING; 025import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 026 027import java.nio.charset.Charset; 028import java.security.SecureRandom; 029import java.util.Optional; 030import java.util.Random; 031import java.util.function.BiFunction; 032 033import org.apiguardian.api.API; 034import org.tquadrat.foundation.annotation.ClassVersion; 035import org.tquadrat.foundation.config.SpecialPropertyType; 036import org.tquadrat.foundation.config.ap.impl.CodeBuilder; 037import org.tquadrat.foundation.config.ap.impl.PropertySpecImpl; 038import org.tquadrat.foundation.javacomposer.ClassName; 039import org.tquadrat.foundation.javacomposer.CodeBlock; 040import org.tquadrat.foundation.javacomposer.FieldSpec; 041import org.tquadrat.foundation.javacomposer.TypeName; 042 043/** 044 * The implementation of 045 * {@link SpecialPropertySpecBase} 046 * for 047 * {@link SpecialPropertyType#CONFIG_PROPERTY_RANDOM}. 048 * 049 * @version $Id: RandomProperty.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: RandomProperty.java 1061 2023-09-25 16:32:43Z tquadrat $" ) 055@API( status = STABLE, since = "0.1.0" ) 056public final class RandomProperty extends SpecialPropertySpecBase 057{ 058 /*--------------*\ 059 ====** Constructors **===================================================== 060 \*--------------*/ 061 /** 062 * Creates a new instance of {@code RandomProperty}. 063 */ 064 public RandomProperty() 065 { 066 super( CONFIG_PROPERTY_RANDOM, EXEMPT_FROM_TOSTRING ); 067 } // RandomProperty() 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 = new $2T()", property.getFieldName(), SecureRandom.class ) 094 .addStaticImport( Charset.class, "defaultCharset" ); 095 096 //---* Create the return value *--------------------------------------- 097 final var retValue = builder.build(); 098 099 //---* Done *---------------------------------------------------------- 100 return retValue; 101 } // composeConstructorFragment() 102 103 /** 104 * The method that composes a field for the 'random' property. 105 * 106 * @param codeBuilder The factory for the code generation. 107 * @param property The property. 108 * @return The field specification. 109 */ 110 @SuppressWarnings( "TypeMayBeWeakened" ) 111 private static FieldSpec composeField( final CodeBuilder codeBuilder, final PropertySpecImpl property ) 112 { 113 final var composer = requireNonNullArgument( codeBuilder, "codeBuilder" ).getComposer(); 114 115 final var builder = composer.fieldBuilder( property.getPropertyType(), property.getFieldName(), PRIVATE ) 116 .addJavadoc( 117 """ 118 Special Property: "$L". 119 """, property.getPropertyName() ) 120 .addModifiers( FINAL ); 121 122 //---* Create the return value *-------------------------------------- 123 final var retValue = builder.build(); 124 125 //---* Done *---------------------------------------------------------- 126 return retValue; 127 } // composeField() 128 129 /** 130 * {@inheritDoc} 131 */ 132 @Override 133 public final Optional<TypeName> getCLIValueHandlerClass() { return Optional.empty(); } 134 135 /** 136 * {@inheritDoc} 137 */ 138 @Override 139 public final Optional<BiFunction<CodeBuilder, PropertySpecImpl, CodeBlock>> getConstructorFragmentComposer() { return Optional.of( RandomProperty::composeConstructorFragment );} 140 141 /** 142 * {@inheritDoc} 143 */ 144 @Override 145 public final Optional<BiFunction<CodeBuilder,PropertySpecImpl, FieldSpec>> getFieldComposer() { return Optional.of( RandomProperty::composeField ); } 146 147 /** 148 * {@inheritDoc} 149 */ 150 @Override 151 public final Optional<TypeName> getPrefsAccessorClass() { return Optional.empty(); } 152 153 /** 154 * {@inheritDoc} 155 */ 156 @Override 157 public final TypeName getPropertyType() { return ClassName.from( Random.class ); } 158 159 /** 160 * {@inheritDoc} 161 */ 162 @Override 163 public final Optional<TypeName> getStringConverterClass() { return Optional.empty(); } 164} 165// class RandomProperty 166 167/* 168 * End of File 169 */