001/*
002 * Copyright 2022-2025 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.soklet.converter;
018
019import javax.annotation.concurrent.ThreadSafe;
020
021/**
022 * Convenience superclass which provides default implementations of {@link ValueConverter} methods that convert from {@link String} to other types.
023 * <p>
024 * For example:
025 * <pre>{@code  public record Jwt(
026 *   String header,
027 *   String payload,
028 *   String signature
029 * ) {}
030 *
031 * ValueConverter<String, Jwt> jwtVc = new FromStringValueConverter<>() {
032 *   @Nonnull
033 *   public Optional<Jwt> performConversion(@Nullable String from) throws Exception {
034 *     if(from == null)
035 *       return Optional.empty();
036 *
037 *     // JWT is of the form "a.b.c", break it into pieces
038 *     String[] components = from.split("\\.");
039 *     Jwt jwt = new Jwt(components[0], components[1], components[2]);
040 *     return Optional.of(jwt);
041 *   }
042 * };}</pre>
043 * See detailed documentation at <a href="https://www.soklet.com/docs/value-conversions#custom-conversions">https://www.soklet.com/docs/value-conversions#custom-conversions</a>.
044 *
045 * @author <a href="https://www.revetkn.com">Mark Allen</a>
046 */
047@ThreadSafe
048public abstract class FromStringValueConverter<T> extends AbstractValueConverter<String, T> {
049        /**
050         * Ensures that the 'from' type of this converter is {@link String}.
051         */
052        public FromStringValueConverter() {
053                super(String.class);
054        }
055}