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.internal;
019
020import static java.util.Map.entry;
021import static org.apiguardian.api.API.Status.INTERNAL;
022import static org.tquadrat.foundation.config.cli.CmdLineValueHandler.MSGKEY_InvalidParameter;
023import static org.tquadrat.foundation.config.cli.CmdLineValueHandler.MSG_InvalidParameter;
024
025import java.util.Map;
026
027import org.apiguardian.api.API;
028import org.tquadrat.foundation.annotation.ClassVersion;
029import org.tquadrat.foundation.annotation.UtilityClass;
030import org.tquadrat.foundation.config.CmdLineException;
031import org.tquadrat.foundation.exception.PrivateConstructorForStaticClassCalledError;
032import org.tquadrat.foundation.i18n.Message;
033import org.tquadrat.foundation.i18n.Translation;
034import org.tquadrat.foundation.lang.StringConverter;
035import org.tquadrat.foundation.util.stringconverter.BigDecimalStringConverter;
036import org.tquadrat.foundation.util.stringconverter.BigIntegerStringConverter;
037import org.tquadrat.foundation.util.stringconverter.ByteStringConverter;
038import org.tquadrat.foundation.util.stringconverter.CharacterStringConverter;
039import org.tquadrat.foundation.util.stringconverter.CharsetStringConverter;
040import org.tquadrat.foundation.util.stringconverter.ClassStringConverter;
041import org.tquadrat.foundation.util.stringconverter.DoubleStringConverter;
042import org.tquadrat.foundation.util.stringconverter.DurationStringConverter;
043import org.tquadrat.foundation.util.stringconverter.FileStringConverter;
044import org.tquadrat.foundation.util.stringconverter.FloatStringConverter;
045import org.tquadrat.foundation.util.stringconverter.InetAddressStringConverter;
046import org.tquadrat.foundation.util.stringconverter.IntegerStringConverter;
047import org.tquadrat.foundation.util.stringconverter.LongStringConverter;
048import org.tquadrat.foundation.util.stringconverter.NumberStringConverter;
049import org.tquadrat.foundation.util.stringconverter.PathStringConverter;
050import org.tquadrat.foundation.util.stringconverter.PatternStringConverter;
051import org.tquadrat.foundation.util.stringconverter.PeriodStringConverter;
052import org.tquadrat.foundation.util.stringconverter.ShortStringConverter;
053import org.tquadrat.foundation.util.stringconverter.TimeZoneStringConverter;
054import org.tquadrat.foundation.util.stringconverter.URIStringConverter;
055import org.tquadrat.foundation.util.stringconverter.URLStringConverter;
056import org.tquadrat.foundation.util.stringconverter.UUIDStringConverter;
057import org.tquadrat.foundation.util.stringconverter.ZoneIdStringConverter;
058
059/**
060 *  This class is meant as a place to hold the specific error messages for the
061 *  failed conversion of command line entries.
062 *
063 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
064 *  @version $Id: MessageRegistry.java 1042 2022-12-26 14:05:06Z tquadrat $
065 *
066 *  @UMLGraph.link
067 *  @since 0.1.0
068 */
069@SuppressWarnings( "OverlyCoupledClass" )
070@UtilityClass
071@ClassVersion( sourceVersion = "$Id: MessageRegistry.java 1042 2022-12-26 14:05:06Z tquadrat $" )
072@API( status = INTERNAL, since = "0.1.0" )
073public final class MessageRegistry
074{
075        /*---------------*\
076    ====** Inner Classes **====================================================
077        \*---------------*/
078    /**
079     *  An entry for the registry.
080     *
081     *  @param  message The default message.
082     *  @param  key The key for the alternative/localised message.
083     *
084     *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
085     *  @version $Id: MessageRegistry.java 1042 2022-12-26 14:05:06Z tquadrat $
086     *
087     *  @UMLGraph.link
088     *  @since 0.1.0
089     */
090    public record MessageRegistryEntry( String message, int key ) {}
091
092        /*-----------*\
093    ====** Constants **========================================================
094        \*-----------*/
095    /**
096     *  The message key for the error message about an illegal
097     *  {@link java.nio.charset.Charset}
098     *  name on the command line.
099     *
100     *  @see CharsetStringConverter#MSG_IllegalCharsetName
101     */
102    @Message
103    (
104        description = "The error message about an illegal charset name on the command line.",
105        translations =
106        {
107            @Translation( language = "en", text = CharsetStringConverter.MSG_IllegalCharsetName ),
108            @Translation( language = "de", text = "'%1$s' ist kein bekanntes Charset" )
109        }
110    )
111    public static final int MSGKEY_IllegalCharsetName = 14;
112
113    /**
114     *  The message key for the error message about an invalid IP address or an
115     *  unknown host name on the command line.
116     *
117     *  @see InetAddressStringConverter#MSG_InvalidAddress
118     */
119    @Message
120    (
121        description = "The error message about an invalid IP address or an unknown host name on the command line.",
122        translations =
123        {
124            @Translation( language = "en", text = InetAddressStringConverter.MSG_InvalidAddress ),
125            @Translation( language = "de", text = "'%1$s' ist ungültig oder unbekannt" )
126        }
127    )
128    public static final int MSGKEY_InvalidAddress = 15;
129
130    /**
131     *  The message key for the error message about an invalid character on
132     *  the command line.
133     *
134     *  @see CharacterStringConverter#MSG_InvalidCharacter
135     */
136    @Message
137    (
138        description = "The error message about an invalid character on the command line.",
139        translations =
140        {
141            @Translation( language = "en", text = CharacterStringConverter.MSG_InvalidCharacter ),
142            @Translation( language = "de", text = "Kein gültiges Zeichen: %1$s" )
143        }
144    )
145    public static final int MSGKEY_InvalidCharacter = 16;
146
147    /**
148     *  The message key for the error message about an invalid duration String
149     *  on the command line.
150     *
151     *  @see DurationStringConverter#MSG_InvalidDuration
152     */
153    @Message
154    (
155        description = "The error message about an invalid duration String on the command line.",
156        translations =
157        {
158            @Translation( language = "en", text = DurationStringConverter.MSG_InvalidDuration ),
159            @Translation( language = "de", text = "Keine gültige Zeitdauer: %1$s" )
160        }
161    )
162    public static final int MSGKEY_InvalidDuration = 17;
163
164    /**
165     *  The message key for the error message about an invalid regular
166     *  expression on the command line.
167     *
168     *  @see PatternStringConverter#MSG_InvalidExpression
169     */
170    @Message
171    (
172        description = "The error message about an invalid regular expression on the command line.",
173        translations =
174        {
175            @Translation( language = "en", text = PatternStringConverter.MSG_InvalidExpression ),
176            @Translation( language = "de", text = "'%1$s' ist kein gültiger regulärer Ausdruck" )
177        }
178    )
179    public static final int MSGKEY_InvalidExpression = 18;
180
181    /**
182     *  The message key for the error message about an invalid number format on
183     *  the command line.
184     *
185     *  @see NumberStringConverter#MSG_InvalidNumberFormat
186     */
187    @Message
188    (
189        description = "The error message about an invalid number format on the command line.",
190        translations =
191        {
192            @Translation( language = "en", text = NumberStringConverter.MSG_InvalidNumberFormat ),
193            @Translation( language = "de", text = "'%1$s' ist kein gültiges Zahlenformat" )
194        }
195    )
196    public static final int MSGKEY_InvalidNumberFormat = 19;
197
198    /**
199     *  The message key for the error message about an invalid period on the
200     *  command line.
201     *
202     *  @see PeriodStringConverter#MSG_InvalidPeriod
203     */
204    @Message
205    (
206        description = "The error message about an invalid period on the command line.",
207        translations =
208        {
209            @Translation( language = "en", text = PeriodStringConverter.MSG_InvalidPeriod ),
210            @Translation( language = "de", text = "Kein gültiger Zeitraum: %1$s" )
211        }
212    )
213    public static final int MSGKEY_InvalidPeriod = 20;
214
215    /**
216     *  The message key for the error message about an invalid URI on the
217     *  command line.
218     *
219     *  @see URIStringConverter#MSG_InvalidURI
220     */
221    @Message
222    (
223        description = "The error message about an invalid URI on the command line.",
224        translations =
225        {
226            @Translation( language = "en", text = URIStringConverter.MSG_InvalidURI ),
227            @Translation( language = "de", text = "Ungültige URI: %1$s" )
228        }
229    )
230    public static final int MSGKEY_InvalidURI = 21;
231
232    /**
233     *  The message key for the error message about an invalid URL on the
234     *  command line.
235     *
236     *  @see URLStringConverter#MSG_InvalidURL
237     */
238    @Message
239    (
240        description = "The error message about an invalid URL on the command line.",
241        translations =
242        {
243            @Translation( language = "en", text = URLStringConverter.MSG_InvalidURL ),
244            @Translation( language = "de", text = "Ungültige URL: %1$s" )
245        }
246    )
247    public static final int MSGKEY_InvalidURL = 22;
248
249    /**
250     *  The message key for the error message about an invalid UUID on the
251     *  command line.
252     *
253     *  @see UUIDStringConverter#MSG_InvalidUUIDFormat
254     */
255    @Message
256        (
257            description = "The error message about an invalid UUID on the command line.",
258            translations =
259                {
260                    @Translation( language = "en", text = UUIDStringConverter.MSG_InvalidUUIDFormat ),
261                    @Translation( language = "de", text = "Keine gültige UUID: %1$s" )
262                }
263        )
264    public static final int MSGKEY_InvalidUUIDFormat = 23;
265
266    /**
267     *  The message key for the error message about an invalid zone id on the
268     *  command line.
269     *
270     *  @see ZoneIdStringConverter#MSG_InvalidZoneId
271     */
272    @Message
273    (
274        description = "The error message about an invalid zone id on the command line.",
275        translations =
276        {
277            @Translation( language = "en", text = ZoneIdStringConverter.MSG_InvalidZoneId ),
278            @Translation( language = "de", text = "Keine gültige Id für eine Zeitzone: %1$s" )
279        }
280    )
281    public static final int MSGKEY_InvalidZoneId = 24;
282
283    /**
284     *  The message key for the error message about an unknown class name on
285     *  the command line.
286     *
287     *  @see ClassStringConverter#MSG_UnknownClass
288     */
289    @Message
290    (
291        description = "The error message about an unknown class name on the command line.",
292        translations =
293        {
294            @Translation( language = "en", text = ClassStringConverter.MSG_UnknownClass ),
295            @Translation( language = "de", text = "'%1$s' ist kein gültiges Zahlenformat" )
296        }
297    )
298    public static final int MSGKEY_UnknownClass = 25;
299
300    /**
301     *  The message key for the error message about an unknown timezone name on
302     *  the command line.
303     *
304     *  @see TimeZoneStringConverter#MSG_UnknownTimeZone
305     */
306    @Message
307        (
308            description = "The error message about an unknown timezone name on the command line.",
309            translations =
310                {
311                    @Translation( language = "en", text = TimeZoneStringConverter.MSG_UnknownTimeZone ),
312                    @Translation( language = "de", text = "Unbekannte Zeitzone: %1$s" )
313                }
314        )
315    public static final int MSGKEY_UnknownTimeZone = 26;
316
317        /*------------------------*\
318    ====** Static Initialisations **===========================================
319        \*------------------------*/
320    /**
321     *  The fall-back entry.
322     */
323    @API( status = INTERNAL, since = "0.1.0" )
324    public static final MessageRegistryEntry MSG_REGISTRY_FALLBACK;
325
326    /**
327     *  The message registry.
328     */
329    @SuppressWarnings( {"rawtypes", "StaticCollection"} )
330    @API( status = INTERNAL, since = "0.1.0" )
331    public static final Map<Class<? extends StringConverter>,MessageRegistryEntry> m_MessageRegistry;
332
333    static
334    {
335        final var numberMessage = new MessageRegistryEntry( NumberStringConverter.MSG_InvalidNumberFormat, MSGKEY_InvalidNumberFormat );
336        final var fileMessage = new MessageRegistryEntry( FileStringConverter.MSG_InvalidFileName, CmdLineException.MSGKEY_InvalidFileName );
337
338        MSG_REGISTRY_FALLBACK = new MessageRegistryEntry( MSG_InvalidParameter, MSGKEY_InvalidParameter );
339
340        m_MessageRegistry = Map.ofEntries
341        (
342            entry( BigDecimalStringConverter.class, numberMessage ),
343            entry( BigIntegerStringConverter.class, numberMessage ),
344            entry( ByteStringConverter.class, numberMessage ),
345            entry( CharacterStringConverter.class, new MessageRegistryEntry( CharacterStringConverter.MSG_InvalidCharacter, MSGKEY_InvalidCharacter ) ),
346            entry( CharsetStringConverter.class, new MessageRegistryEntry( CharsetStringConverter.MSG_IllegalCharsetName, MSGKEY_IllegalCharsetName ) ),
347            entry( ClassStringConverter.class, new MessageRegistryEntry( ClassStringConverter.MSG_UnknownClass, MSGKEY_UnknownClass ) ),
348            entry( DoubleStringConverter.class, numberMessage ),
349            entry( DurationStringConverter.class, new MessageRegistryEntry( DurationStringConverter.MSG_InvalidDuration, MSGKEY_InvalidDuration ) ),
350            entry( FileStringConverter.class, fileMessage ),
351            entry( FloatStringConverter.class, numberMessage ),
352            entry( InetAddressStringConverter.class, new MessageRegistryEntry( InetAddressStringConverter.MSG_InvalidAddress, MSGKEY_InvalidAddress ) ),
353            entry( IntegerStringConverter.class, numberMessage ),
354            entry( LongStringConverter.class, numberMessage ),
355            entry( PathStringConverter.class, fileMessage ),
356            entry( PatternStringConverter.class, new MessageRegistryEntry( PatternStringConverter.MSG_InvalidExpression, MSGKEY_InvalidExpression ) ),
357            entry( PeriodStringConverter.class, new MessageRegistryEntry( PeriodStringConverter.MSG_InvalidPeriod, MSGKEY_InvalidPeriod ) ),
358            entry( ShortStringConverter.class, numberMessage ),
359            entry( TimeZoneStringConverter.class, new MessageRegistryEntry( TimeZoneStringConverter.MSG_UnknownTimeZone, MSGKEY_UnknownTimeZone ) ),
360            entry( URIStringConverter.class, new MessageRegistryEntry( URIStringConverter.MSG_InvalidURI, MSGKEY_InvalidURI ) ),
361            entry( URLStringConverter.class, new MessageRegistryEntry( URLStringConverter.MSG_InvalidURL, MSGKEY_InvalidURL ) ),
362            entry( UUIDStringConverter.class, new MessageRegistryEntry( UUIDStringConverter.MSG_InvalidUUIDFormat, MSGKEY_InvalidUUIDFormat ) ),
363            entry( ZoneIdStringConverter.class, new MessageRegistryEntry( ZoneIdStringConverter.MSG_InvalidZoneId, MSGKEY_InvalidZoneId ) )
364        );
365    }
366
367        /*--------------*\
368    ====** Constructors **=====================================================
369        \*--------------*/
370    /**
371     *  No instance allowed for this class.
372     */
373    private MessageRegistry() { throw new PrivateConstructorForStaticClassCalledError( MessageRegistry.class ); }
374}
375//  class MessageRegistry
376
377/*
378 *  End of File
379 */