001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.transform;
014
015import java.time.*;
016import java.time.temporal.*;
017import java.util.*;
018import java.util.concurrent.*;
019
020import javax.xml.datatype.*;
021
022import org.apache.juneau.reflect.*;
023import org.apache.juneau.transforms.*;
024
025/**
026 * Maintain the list of default PojoSwaps used by all serializers and parsers.
027 */
028public class DefaultSwaps {
029
030   private static final Map<Class<?>,PojoSwap<?,?>> POJO_SWAPS = new ConcurrentHashMap<>();
031   static {
032      POJO_SWAPS.put(Enumeration.class, new EnumerationSwap());
033      POJO_SWAPS.put(Iterator.class, new IteratorSwap());
034      POJO_SWAPS.put(Locale.class, new LocaleSwap());
035      POJO_SWAPS.put(Class.class, new ClassSwap());
036      POJO_SWAPS.put(Calendar.class, new TemporalCalendarSwap.IsoOffsetDateTime());
037      POJO_SWAPS.put(Date.class, new TemporalDateSwap.IsoLocalDateTime());
038      POJO_SWAPS.put(Instant.class, new TemporalSwap.IsoInstant());
039      POJO_SWAPS.put(ZonedDateTime.class, new TemporalSwap.IsoOffsetDateTime());
040      POJO_SWAPS.put(LocalDate.class, new TemporalSwap.IsoLocalDate());
041      POJO_SWAPS.put(LocalDateTime.class, new TemporalSwap.IsoLocalDateTime());
042      POJO_SWAPS.put(LocalTime.class, new TemporalSwap.IsoLocalTime());
043      POJO_SWAPS.put(OffsetDateTime.class, new TemporalSwap.IsoOffsetDateTime());
044      POJO_SWAPS.put(OffsetTime.class, new TemporalSwap.IsoOffsetTime());
045      POJO_SWAPS.put(Year.class, new TemporalSwap.IsoYear());
046      POJO_SWAPS.put(YearMonth.class, new TemporalSwap.IsoYearMonth());
047      POJO_SWAPS.put(Temporal.class, new TemporalSwap.IsoInstant());
048      POJO_SWAPS.put(TimeZone.class, new TimeZoneSwap());
049      POJO_SWAPS.put(XMLGregorianCalendar.class, new XMLGregorianCalendarSwap());
050      POJO_SWAPS.put(ZoneId.class, new ZoneIdSwap());
051   }
052
053   /**
054    * Find the default PojoSwap for the specified class.
055    *
056    * @param ci The class to find the swap for.
057    * @return The matched swap, or <jk>null</jk> if it couldn't be found.
058    */
059   public static PojoSwap<?,?> find(ClassInfo ci) {
060      for (ClassInfo ci2 : ci.getAllParents()) {
061         PojoSwap<?,?> ps = POJO_SWAPS.get(ci2.inner());
062         if (ps != null)
063            return ps;
064      }
065      return null;
066   }
067}