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.swap;
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.swaps.*;
025
026/**
027 * Maintain the list of default swaps used by all serializers and parsers.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.Swaps">Swaps</a>
031 * </ul>
032 */
033public class DefaultSwaps {
034
035   private static final Map<Class<?>,ObjectSwap<?,?>> SWAPS = new ConcurrentHashMap<>();
036   static {
037      SWAPS.put(Enumeration.class, new EnumerationSwap());
038      SWAPS.put(Iterator.class, new IteratorSwap());
039      SWAPS.put(Locale.class, new LocaleSwap());
040      SWAPS.put(Class.class, new ClassSwap());
041      SWAPS.put(Calendar.class, new TemporalCalendarSwap.IsoOffsetDateTime());
042      SWAPS.put(Date.class, new TemporalDateSwap.IsoLocalDateTime());
043      SWAPS.put(Instant.class, new TemporalSwap.IsoInstant());
044      SWAPS.put(ZonedDateTime.class, new TemporalSwap.IsoOffsetDateTime());
045      SWAPS.put(LocalDate.class, new TemporalSwap.IsoLocalDate());
046      SWAPS.put(LocalDateTime.class, new TemporalSwap.IsoLocalDateTime());
047      SWAPS.put(LocalTime.class, new TemporalSwap.IsoLocalTime());
048      SWAPS.put(OffsetDateTime.class, new TemporalSwap.IsoOffsetDateTime());
049      SWAPS.put(OffsetTime.class, new TemporalSwap.IsoOffsetTime());
050      SWAPS.put(StackTraceElement.class, new StackTraceElementSwap());
051      SWAPS.put(Year.class, new TemporalSwap.IsoYear());
052      SWAPS.put(YearMonth.class, new TemporalSwap.IsoYearMonth());
053      SWAPS.put(Temporal.class, new TemporalSwap.IsoInstant());
054      SWAPS.put(TimeZone.class, new TimeZoneSwap());
055      SWAPS.put(XMLGregorianCalendar.class, new XMLGregorianCalendarSwap());
056      SWAPS.put(ZoneId.class, new ZoneIdSwap());
057      SWAPS.put(MatchResult.class, new MatchResultSwap());
058   }
059
060   /**
061    * Find the default ObjectSwap for the specified class.
062    *
063    * @param ci The class to find the swap for.
064    * @return The matched swap, or <jk>null</jk> if it couldn't be found.
065    */
066   public static ObjectSwap<?,?> find(ClassInfo ci) {
067      ClassInfo ci2 = ci.getAnyParent(x -> SWAPS.get(x.inner()) != null);
068      return ci2 == null ? null : SWAPS.get(ci2.inner());
069   }
070}