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 org.spongepowered.configurate.interfaces;
018
019import org.spongepowered.configurate.ConfigurationOptions;
020import org.spongepowered.configurate.serialize.TypeSerializerCollection;
021
022/**
023 * This class has the default {@link ConfigurationOptions}
024 * with {@link InterfaceTypeSerializer} added to the serializers.
025 *
026 * @since 4.2.0
027 */
028public final class InterfaceDefaultOptions {
029
030    private static final TypeSerializerCollection DEFAULTS =
031                TypeSerializerCollection.builder()
032                    .registerAnnotated(InterfaceTypeSerializer::applicable, InterfaceTypeSerializer.INSTANCE)
033                    .registerAnnotatedObjects(InterfaceMiddleware.buildObjectMapperWithMiddleware())
034                    .build();
035
036    private InterfaceDefaultOptions() {
037    }
038
039    /**
040     * The default ConfigurationOptions with {@link InterfaceTypeSerializer} added to the serializers.
041     *
042     * @return the default ConfigurationOptions with {@link InterfaceTypeSerializer} added to the serializers.
043     * @since 4.2.0
044     */
045    public static ConfigurationOptions defaults() {
046        return addTo(ConfigurationOptions.defaults());
047    }
048
049    /**
050     * Sets the default configuration options to be used by the resultant loader
051     * by providing a function which takes interface's default serializer
052     * collection and applies any desired changes.
053     *
054     * @param options to transform the existing default options
055     * @return the default options with the applied changes
056     * @since 4.2.0
057     */
058    public static ConfigurationOptions addTo(final ConfigurationOptions options) {
059        // This creates a new TypeSerializerCollection with 'options' as parent. Child takes priority over parent.
060        return options.serializers(serializers -> serializers.registerAll(DEFAULTS));
061    }
062
063}