001/*
002 * ============================================================================
003 *  Copyright © 2002-2021 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 java.util.Arrays.asList;
021import static org.apiguardian.api.API.Status.MAINTAINED;
022import static org.tquadrat.foundation.config.ap.CollectionKind.NO_COLLECTION;
023import static org.tquadrat.foundation.config.ap.PropertySpec.PropertyFlag.PROPERTY_IS_SPECIAL;
024import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument;
025
026import javax.lang.model.element.Name;
027import java.util.EnumSet;
028import java.util.List;
029import java.util.Optional;
030import java.util.OptionalInt;
031import java.util.Set;
032import java.util.function.BiFunction;
033
034import org.apiguardian.api.API;
035import org.tquadrat.foundation.annotation.ClassVersion;
036import org.tquadrat.foundation.annotation.MountPoint;
037import org.tquadrat.foundation.config.SpecialPropertyType;
038import org.tquadrat.foundation.config.ap.CollectionKind;
039import org.tquadrat.foundation.config.ap.PropertySpec;
040import org.tquadrat.foundation.config.ap.impl.CodeBuilder;
041import org.tquadrat.foundation.config.ap.impl.PropertySpecImpl;
042import org.tquadrat.foundation.config.ap.impl.SpecialPropertySpec;
043import org.tquadrat.foundation.config.ap.impl.codebuilders.CodeGeneratorContext;
044import org.tquadrat.foundation.exception.IllegalOperationException;
045import org.tquadrat.foundation.javacomposer.CodeBlock;
046import org.tquadrat.foundation.javacomposer.FieldSpec;
047import org.tquadrat.foundation.javacomposer.MethodSpec;
048import org.tquadrat.foundation.javacomposer.MethodSpec.Builder;
049import org.tquadrat.foundation.javacomposer.TypeName;
050
051/**
052 *  The base class for the special property specifications.
053 *
054 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
055 *  @version $Id: SpecialPropertySpecBase.java 1010 2022-02-05 19:28:36Z tquadrat $
056 *  @UMLGraph.link
057 *  @since 0.1.0
058 */
059@ClassVersion( sourceVersion = "$Id: SpecialPropertySpecBase.java 1010 2022-02-05 19:28:36Z tquadrat $" )
060@API( status = MAINTAINED, since = "0.1.0" )
061abstract sealed class SpecialPropertySpecBase implements SpecialPropertySpec
062    permits CharsetProperty, ClockProperty, LocaleProperty, MessagePrefixProperty, ProcessIdProperty, RandomProperty, ResourceBundleProperty, SessionKeyProperty, TimeZoneProperty
063{
064        /*------------*\
065    ====** Attributes **=======================================================
066        \*------------*/
067    /**
068     *  The property flags.
069     */
070    private final Set<PropertyFlag> m_PropertyFlags = EnumSet.noneOf( PropertyFlag.class );
071
072    /**
073     *  The type of the special property.
074     */
075    private final SpecialPropertyType m_Type;
076
077        /*--------------*\
078    ====** Constructors **=====================================================
079        \*--------------*/
080    /**
081     *  Creates a new instance of {@code SpecialPropertySpecBase}.
082     *
083     *  @param  type    The type of the special property.
084     *  @param  flags   The flags for this special property.
085     */
086    protected SpecialPropertySpecBase( final SpecialPropertyType type, final PropertyFlag... flags )
087    {
088        m_Type = requireNonNullArgument( type, "type" );
089        m_PropertyFlags.addAll( asList( requireNonNullArgument( flags, "flags" ) ) );
090        m_PropertyFlags.add( PROPERTY_IS_SPECIAL );
091    }   //  SpecialPropertySpecBase()
092
093        /*---------*\
094    ====** Methods **==========================================================
095        \*---------*/
096    /**
097     *  {@inheritDoc}
098     */
099    @Override
100    public final Optional<MethodSpec> createAddMethod( final CodeBuilder codeBuilder ) { return Optional.empty(); }
101
102    /**
103     *  {@inheritDoc}
104     */
105    @Override
106    public final Optional<CodeBlock> createConstructorFragment( final CodeBuilder codeBuilder ) { return Optional.empty(); }
107
108    /**
109     *  {@inheritDoc}
110     */
111    @Override
112    public final Optional<FieldSpec> createField( final CodeBuilder codeBuilder ) { return Optional.empty(); }
113
114    /**
115     *  {@inheritDoc}
116     */
117    @Override
118    public final Optional<MethodSpec> createGetter( final CodeBuilder codeBuilder ) { return Optional.empty(); }
119
120    /**
121     *  {@inheritDoc}
122     */
123    @Override
124    public final Optional<MethodSpec> createSetter( final CodeBuilder codeBuilder ) { return Optional.empty(); }
125
126    /**
127     * {@inheritDoc}
128     */
129    @Override
130    public final Name getAddMethodArgumentName() { return null; }
131
132    /**
133     * {@inheritDoc}
134     */
135    @Override
136    @MountPoint
137    public Optional<BiFunction<CodeBuilder,PropertySpecImpl,MethodSpec>> getAddMethodComposer() { return Optional.empty(); }
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public final Optional<Name> getAddMethodName() { return Optional.empty(); }
144
145    /**
146     * {@inheritDoc}
147     */
148    @Override
149    public final EnumSet<PropertyFlag> getAllFlags() { return EnumSet.copyOf( m_PropertyFlags ); }
150
151    /**
152     *  {@inheritDoc}
153     */
154    @Override
155    public final OptionalInt getCLIArgumentIndex() { return OptionalInt.empty(); }
156
157    /**
158     *  {@inheritDoc}
159     */
160    @Override
161    public final Optional<String> getCLIFormat() { return Optional.empty(); }
162
163    /**
164     * {@inheritDoc}
165     */
166    @Override
167    public final Optional<String> getCLIMetaVar() { return Optional.empty(); }
168
169    /**
170     * {@inheritDoc}
171     */
172    @SuppressWarnings( "OptionalContainsCollection" )
173    @Override
174    public final Optional<List<String>> getCLIOptionNames() { return Optional.empty(); }
175
176    /**
177     * {@inheritDoc}
178     */
179    @Override
180    public final Optional<String> getCLIUsage() { return Optional.empty(); }
181
182    /**
183     * {@inheritDoc}
184     */
185    @Override
186    public final Optional<String> getCLIUsageKey() { return Optional.empty(); }
187
188    /**
189     *  {@inheritDoc}
190     */
191    @Override
192    @MountPoint
193    public CollectionKind getCollectionKind() { return NO_COLLECTION; }
194
195    /**
196     *  {@inheritDoc}
197     */
198    @Override
199    @MountPoint
200    public Optional<String> getEnvironmentDefaultValue() { return Optional.empty(); }
201
202    /**
203     *  {@inheritDoc}
204     */
205    @Override
206    @MountPoint
207    public Optional<BiFunction<CodeBuilder, PropertySpecImpl, CodeBlock>> getConstructorFragmentComposer() { return Optional.empty(); }
208
209    /**
210     * {@inheritDoc}
211     */
212    @Override
213    public final Optional<String> getEnvironmentVariableName() { return Optional.empty(); }
214
215    /**
216     * {@inheritDoc}
217     */
218    @Override
219    @MountPoint
220    public Optional<BiFunction<CodeBuilder,PropertySpecImpl,FieldSpec>> getFieldComposer() { return Optional.of( CodeGeneratorContext.getFieldComposer() ); }
221
222    /**
223     * {@inheritDoc}
224     */
225    @Override
226    @MountPoint
227    public Optional<Builder> getGetterBuilder() { return Optional.empty(); }
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    @MountPoint
234    public Optional<BiFunction<CodeBuilder,PropertySpecImpl,MethodSpec>> getGetterComposer() { return Optional.of( CodeGeneratorContext.getGetterComposer() ); }
235
236    /**
237     * {@inheritDoc}
238     */
239    @Override
240    public final Optional<Name> getGetterMethodName() { return Optional.empty(); }
241
242    /**
243     *  {@inheritDoc}
244     */
245    @Override
246    @MountPoint
247    public TypeName getGetterReturnType() { return getPropertyType(); }
248
249    /**
250     *  {@inheritDoc}
251     */
252    @Override
253    public final Optional<String> getINIComment() { return Optional.empty(); }
254
255    /**
256     *  {@inheritDoc}
257     */
258    @Override
259    public final Optional<String> getINIGroup() { return Optional.empty(); }
260
261    /**
262     *  {@inheritDoc}
263     */
264    @Override
265    public final Optional<String> getINIKey() { return Optional.empty(); }
266
267    /**
268     * {@inheritDoc}
269     */
270    @Override
271    public final Optional<String> getPrefsKey() { return Optional.of( m_Type.getPropertyName() ); }
272
273    /**
274     * {@inheritDoc}
275     */
276    @Override
277    public final String getPropertyName() { return m_Type.getPropertyName(); }
278
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public final Name getSetterArgumentName() { return null; }
284
285    /**
286     * {@inheritDoc}
287     */
288    @Override
289    @MountPoint
290    public Optional<Builder> getSetterBuilder() { return Optional.empty(); }
291
292    /**
293     * {@inheritDoc}
294     */
295    @Override
296    @MountPoint
297    public Optional<BiFunction<CodeBuilder,PropertySpecImpl,MethodSpec>> getSetterComposer() { return Optional.empty(); }
298
299    /**
300     * {@inheritDoc}
301     */
302    @Override
303    public final Optional<Name> getSetterMethodName() { return Optional.empty(); }
304
305    /**
306     * {@inheritDoc}
307     */
308    @Override
309    public final Optional<SpecialPropertyType> getSpecialPropertyType() { return Optional.of( m_Type ); }
310
311    /**
312     * {@inheritDoc}
313     */
314    @Override
315    public final Optional<String> getSystemPrefsPath() { return Optional.empty(); }
316
317    /**
318     * {@inheritDoc}
319     */
320    @Override
321    public final Optional<String> getSystemPropertyName() { return Optional.empty(); }
322
323    /**
324     * {@inheritDoc}
325     */
326    @Override
327    public final boolean hasFlag( final PropertyFlag flag ) { return m_PropertyFlags.contains( requireNonNullArgument( flag, "flag" ) ); }
328
329    /**
330     *  {@inheritDoc}
331     */
332    @MountPoint
333    @Override
334    public boolean isEnum() { return false; }
335
336    /**
337     * {@inheritDoc}
338     */
339    @Override
340    public final PropertySpec merge() { throw new IllegalOperationException( "Not allowed for a SpecialProperty" ); }
341}
342//  class SpecialPropertySpecBase
343
344/*
345 *  End of File
346 */