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