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.Nonnull; 020import javax.annotation.Nullable; 021import java.lang.reflect.Type; 022import java.util.Optional; 023 024/** 025 * Contract for converting objects from one type to another. 026 * <p> 027 * For example, you might have a {@code ValueConverter<String, List<Integer>>} which converts 028 * text like {@code "1,2,3"} to a list of numbers. 029 * <p> 030 * Value conversion is documented in detail at <a href="https://www.soklet.com/docs/value-conversions">https://www.soklet.com/docs/value-conversions</a>. 031 * 032 * @author <a href="https://www.revetkn.com">Mark Allen</a> 033 */ 034public interface ValueConverter<F, T> { 035 /** 036 * Converts {@code from} to an instance of {@code T}. 037 * 038 * @param from the value from which to convert. May be {@code null} 039 * @return the {@code T} representation of {@code from} 040 * @throws ValueConversionException if an error occurs during conversion 041 */ 042 @Nonnull 043 Optional<T> convert(@Nullable F from) throws ValueConversionException; 044 045 /** 046 * The 'converting from' type. 047 * 048 * @return the type represented by {@code F} 049 */ 050 @Nonnull 051 Type getFromType(); 052 053 /** 054 * The 'converting to' type. 055 * 056 * @return the type represented by {@code T} 057 */ 058 @Nonnull 059 Type getToType(); 060}