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