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.objectmapping.ObjectMapper; 021import org.spongepowered.configurate.serialize.TypeSerializerCollection; 022 023import java.util.function.Consumer; 024 025/** 026 * This class has the default {@link ConfigurationOptions} 027 * with {@link InterfaceTypeSerializer} added to the serializers. 028 * 029 * @since 4.2.0 030 */ 031public final class InterfaceDefaultOptions { 032 033 private static final TypeSerializerCollection DEFAULTS = 034 TypeSerializerCollection.builder() 035 .registerAnnotated(InterfaceTypeSerializer::applicable, InterfaceTypeSerializer.INSTANCE) 036 .registerAnnotatedObjects(InterfaceMiddleware.buildObjectMapperWithMiddleware().build()) 037 .build(); 038 039 private InterfaceDefaultOptions() { 040 } 041 042 /** 043 * The default ConfigurationOptions with {@link InterfaceTypeSerializer} added to the serializers. 044 * 045 * @return the default ConfigurationOptions with {@link InterfaceTypeSerializer} added to the serializers. 046 * @since 4.2.0 047 */ 048 public static ConfigurationOptions defaults() { 049 return addTo(ConfigurationOptions.defaults()); 050 } 051 052 /** 053 * Sets the default configuration options to be used by the resultant loader 054 * by providing a function which takes interface's default serializer 055 * collection and applies any desired changes. 056 * 057 * @param options to transform the existing default options 058 * @return the default options with the applied changes 059 * @since 4.2.0 060 */ 061 public static ConfigurationOptions addTo(final ConfigurationOptions options) { 062 // This creates a new TypeSerializerCollection with 'options' as parent. Child takes priority over parent. 063 return options.serializers(serializers -> serializers.registerAll(DEFAULTS)); 064 } 065 066 /** 067 * {@link #addTo(ConfigurationOptions)} with an option to customize the {@link ObjectMapper.Factory}. 068 * 069 * @param options to transform the existing default options 070 * @param objectMapperConsumer to transform the ObjectMapper factory 071 * @return the default options with the applied changes 072 * @since 4.2.0 073 */ 074 public static ConfigurationOptions addTo(final ConfigurationOptions options, 075 final Consumer<ObjectMapper.Factory.Builder> objectMapperConsumer) { 076 return options.serializers(serializers -> { 077 final ObjectMapper.Factory.Builder builder = InterfaceMiddleware.buildObjectMapperWithMiddleware(); 078 objectMapperConsumer.accept(builder); 079 serializers.registerAnnotated(InterfaceTypeSerializer::applicable, InterfaceTypeSerializer.INSTANCE) 080 .registerAnnotatedObjects(builder.build()); 081 }); 082 } 083 084}