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.*;
019import java.util.regex.*;
020
021import javax.xml.datatype.*;
022
023import org.apache.juneau.reflect.*;
024import org.apache.juneau.transforms.*;
025
026/**
027 * Maintain the list of default PojoSwaps used by all serializers and parsers.
028 */
029public class DefaultSwaps {
030
031   private static final Map<Class<?>,PojoSwap<?,?>> POJO_SWAPS = new ConcurrentHashMap<>();
032   static {
033      POJO_SWAPS.put(Enumeration.class, new EnumerationSwap());
034      POJO_SWAPS.put(Iterator.class, new IteratorSwap());
035      POJO_SWAPS.put(Locale.class, new LocaleSwap());
036      POJO_SWAPS.put(Class.class, new ClassSwap());
037      POJO_SWAPS.put(Calendar.class, new TemporalCalendarSwap.IsoOffsetDateTime());
038      POJO_SWAPS.put(Date.class, new TemporalDateSwap.IsoLocalDateTime());
039      POJO_SWAPS.put(Instant.class, new TemporalSwap.IsoInstant());
040      POJO_SWAPS.put(ZonedDateTime.class, new TemporalSwap.IsoOffsetDateTime());
041      POJO_SWAPS.put(LocalDate.class, new TemporalSwap.IsoLocalDate());
042      POJO_SWAPS.put(LocalDateTime.class, new TemporalSwap.IsoLocalDateTime());
043      POJO_SWAPS.put(LocalTime.class, new TemporalSwap.IsoLocalTime());
044      POJO_SWAPS.put(OffsetDateTime.class, new TemporalSwap.IsoOffsetDateTime());
045      POJO_SWAPS.put(OffsetTime.class, new TemporalSwap.IsoOffsetTime());
046      POJO_SWAPS.put(StackTraceElement.class, new StackTraceElementSwap());
047      POJO_SWAPS.put(Year.class, new TemporalSwap.IsoYear());
048      POJO_SWAPS.put(YearMonth.class, new TemporalSwap.IsoYearMonth());
049      POJO_SWAPS.put(Temporal.class, new TemporalSwap.IsoInstant());
050      POJO_SWAPS.put(TimeZone.class, new TimeZoneSwap());
051      POJO_SWAPS.put(XMLGregorianCalendar.class, new XMLGregorianCalendarSwap());
052      POJO_SWAPS.put(ZoneId.class, new ZoneIdSwap());
053      POJO_SWAPS.put(MatchResult.class, new MatchResultSwap());
054   }
055
056   /**
057    * Find the default PojoSwap for the specified class.
058    *
059    * @param ci The class to find the swap for.
060    * @return The matched swap, or <jk>null</jk> if it couldn't be found.
061    */
062   public static PojoSwap<?,?> find(ClassInfo ci) {
063      for (ClassInfo ci2 : ci.getAllParentsChildFirst()) {
064         PojoSwap<?,?> ps = POJO_SWAPS.get(ci2.inner());
065         if (ps != null)
066            return ps;
067      }
068      return null;
069   }
070}