001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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.apache.juneau.marshall;
018
019import java.util.*;
020
021import org.apache.juneau.commons.http.*;
022import org.apache.juneau.commons.reflect.*;
023import org.apache.juneau.commons.svl.*;
024
025/**
026 * Utility classes and methods for the {@link MarshalledConfig @MarshalledConfig} annotation.
027 *
028 */
029public class MarshalledConfigAnnotation {
030
031   /**
032    * Prevents instantiation.
033    */
034   private MarshalledConfigAnnotation() {}
035
036   /**
037    * Applies {@link MarshalledConfig} annotations to a {@link MarshallingContext.Builder}.
038    */
039   public static class Applier extends AnnotationApplier<MarshalledConfig,MarshallingContext.Builder> {
040
041      /**
042       * Constructor.
043       *
044       * @param vr The resolver for resolving values in annotations. Must not be <jk>null</jk>.
045       */
046      public Applier(VarResolverSession vr) {
047         super(MarshalledConfig.class, MarshallingContext.Builder.class, vr);
048      }
049
050      @Override
051      @SuppressWarnings({
052         "java:S3776" // Cognitive complexity acceptable for the flat per-attribute config-applier sequence.
053      })
054      public void apply(AnnotationInfo<MarshalledConfig> ai, MarshallingContext.Builder b) {
055         MarshalledConfig a = ai.inner();
056
057         // @formatter:off
058         bool(a.debug()).ifPresent(b::debug);
059         string(a.typePropertyName()).ifPresent(b::typePropertyName);
060         string(a.locale()).map(Locale::forLanguageTag).ifPresent(b::locale);
061         string(a.mediaType()).map(MediaType::of).ifPresent(b::mediaType);
062         string(a.timeZone()).map(TimeZone::getTimeZone).ifPresent(b::timeZone);
063         if (a.durationFormat() != DurationFormat.NOT_SET) b.durationFormat(a.durationFormat());
064         if (a.periodFormat() != PeriodFormat.NOT_SET) b.periodFormat(a.periodFormat());
065         if (a.calendarFormat() != CalendarFormat.NOT_SET) b.calendarFormat(a.calendarFormat());
066         if (a.dateFormat() != DateFormat.NOT_SET) b.dateFormat(a.dateFormat());
067         if (a.temporalFormat() != TemporalFormat.NOT_SET) b.temporalFormat(a.temporalFormat());
068         if (a.timeZoneFormat() != TimeZoneFormat.NOT_SET) b.timeZoneFormat(a.timeZoneFormat());
069         if (a.localeFormat() != LocaleFormat.NOT_SET) b.localeFormat(a.localeFormat());
070         if (a.binaryFormat() != BinaryFormat.NOT_SET) b.binaryFormat(a.binaryFormat());
071         if (a.enumFormat() != EnumFormat.NOT_SET) b.enumFormat(a.enumFormat());
072         if (a.uuidFormat() != UuidFormat.NOT_SET) b.uuidFormat(a.uuidFormat());
073         if (a.bitSetFormat() != BitSetFormat.NOT_SET) b.bitSetFormat(a.bitSetFormat());
074         if (a.bigNumberFormat() != BigNumberFormat.NOT_SET) b.bigNumberFormat(a.bigNumberFormat());
075         if (a.booleanFormat() != BooleanFormat.NOT_SET) b.booleanFormat(a.booleanFormat());
076         if (a.floatFormat() != FloatFormat.NOT_SET) b.floatFormat(a.floatFormat());
077         if (a.currencyFormat() != CurrencyFormat.NOT_SET) b.currencyFormat(a.currencyFormat());
078         if (a.classFormat() != ClassFormat.NOT_SET) b.classFormat(a.classFormat());
079         classes(a.dictionary()).ifPresent(b::beanDictionary);
080         classes(a.dictionary_replace()).ifPresent(x -> { b.beanDictionary().clear(); b.beanDictionary(x);});
081         classes(a.swaps()).ifPresent(b::swaps);
082         classes(a.swaps_replace()).ifPresent(x -> { b.swaps().clear(); b.swaps(x);});
083         // @formatter:on
084      }
085   }
086}