001package ball.databind;
002/*-
003 * ##########################################################################
004 * Data Binding Utilities
005 * $Id: ObjectMapperFeature.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-databind/trunk/src/main/java/ball/databind/ObjectMapperFeature.java $
007 * %%
008 * Copyright (C) 2016 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.util.EnumLookupMap;
024import com.fasterxml.jackson.core.JsonGenerator;
025import com.fasterxml.jackson.core.JsonParser;
026import com.fasterxml.jackson.databind.DeserializationFeature;
027import com.fasterxml.jackson.databind.MapperFeature;
028import com.fasterxml.jackson.databind.ObjectMapper;
029import com.fasterxml.jackson.databind.SerializationFeature;
030import java.util.SortedMap;
031
032import static java.util.Collections.unmodifiableSortedMap;
033
034/**
035 * Abstract class with static {@link SortedMap} ({@link EnumLookupMap})
036 * member of all {@link ObjectMapper} features.
037 *
038 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
039 * @version $Revision: 5285 $
040 */
041public abstract class ObjectMapperFeature {
042
043    /**
044     * The {@link SortedMap} of feature names to their corresponding
045     * {@link Enum}.
046     */
047    public static final SortedMap<String,Enum<?>> MAP =
048        unmodifiableSortedMap(new EnumLookupMap(DeserializationFeature.class,
049                                                JsonGenerator.Feature.class,
050                                                JsonParser.Feature.class,
051                                                MapperFeature.class,
052                                                SerializationFeature.class));
053
054    /**
055     * Static method to configure an {@link ObjectMapper} feature.
056     *
057     * @param   mapper          The {@link ObjectMapper} to configure.
058     * @param   feature         The feature {@link Enum}.
059     * @param   value           The {@link Boolean} value.
060     *
061     * @throws  IllegalArgumentException
062     *                          If the feature {@link Enum} is not
063     *                          recognized.
064     */
065    public static void configure(ObjectMapper mapper,
066                                 Enum<?> feature, boolean value) {
067        if (feature instanceof MapperFeature) {
068            mapper.configure((MapperFeature) feature, value);
069        } else if (feature instanceof JsonGenerator.Feature) {
070            mapper.configure((JsonGenerator.Feature) feature, value);
071        } else if (feature instanceof JsonParser.Feature) {
072            mapper.configure((JsonParser.Feature) feature, value);
073        } else if (feature instanceof DeserializationFeature) {
074            mapper.configure((DeserializationFeature) feature, value);
075        } else if (feature instanceof SerializationFeature) {
076            mapper.configure((SerializationFeature) feature, value);
077        } else {
078            throw new IllegalArgumentException("Unrecognized feature `"
079                                               + feature.getClass().getName()
080                                               + "." + feature.name() + "'");
081        }
082    }
083
084    private ObjectMapperFeature() { }
085}