001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
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,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ninja.leaping.configurate.objectmapping;
018
019import com.google.common.reflect.TypeToken;
020import org.checkerframework.checker.nullness.qual.NonNull;
021
022/**
023 * A factory to produce {@link ObjectMapper} instances
024 */
025public interface ObjectMapperFactory {
026
027    /**
028     * Creates an {@link ObjectMapper} for the given type.
029     *
030     * @param type The type
031     * @param <T> The type
032     * @return An object mapper
033     * @throws ObjectMappingException If an exception occured whilst mapping
034     */
035    @NonNull
036    <T> ObjectMapper<T> getMapper(@NonNull Class<T> type) throws ObjectMappingException;
037
038    /**
039     * Creates an {@link ObjectMapper} for the given type.
040     *
041     * @param type The type
042     * @param <T> The type
043     * @return An object mapper
044     * @throws ObjectMappingException If an exception occured whilst mapping
045     */
046    @NonNull
047    @SuppressWarnings("unchecked")
048    default <T> ObjectMapper<T> getMapper(@NonNull TypeToken<T> type) throws ObjectMappingException {
049        return getMapper((Class<T>) type.getRawType());
050    }
051
052}