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 org.apache.juneau.*;
016import org.apache.juneau.utils.*;
017
018/**
019 * A subclass of {@link ObjectSwap} that allows swap and unswap methods to be defined as functions.
020 *
021 * <p class='bjava'>
022 *    <jc>// Example</jc>
023 *    <jk>public class</jk> MyBeanSwap <jk>extends</jk> FunctionalSwap&lt;MyBean,String&gt; {
024 *       <jk>public</jk> MyBeanSwap() {
025 *          <jk>super</jk>(MyBean.<jk>class</jk>, String.<jk>class</jk>, <jv>x</jv> -&gt; <jsm>myStringifyier</jsm>(<jv>x</jv>), <jv>x</jv> -&gt; <jsm>myDeStringifier</jsm>(<jv>x</jv>));
026 *       }
027 *    }
028 * </p>
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.Swaps">Swaps</a>
032 * </ul>
033 *
034 * @param <T> The normal form of the class.
035 * @param <S> The swapped form of the class.
036 */
037public class FunctionalSwap<T,S> extends ObjectSwap<T,S> {
038
039   private final ThrowingFunction<T,S> swapFunction;
040   private final ThrowingFunction<S,T> unswapFunction;
041
042   /**
043    * Constructor.
044    *
045    * @param normalClass The normal class.
046    * @param swappedClass The swapped class.
047    * @param swapFunction The function for converting from normal to swapped.
048    */
049   public FunctionalSwap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction) {
050      this(normalClass, swappedClass, swapFunction, null);
051   }
052
053   /**
054    * Constructor.
055    *
056    * @param normalClass The normal class.
057    * @param swappedClass The swapped class.
058    * @param swapFunction The function for converting from normal to swapped.
059    * @param unswapFunction The function for converting swapped to normal.
060    */
061   public FunctionalSwap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction, ThrowingFunction<S,T> unswapFunction) {
062      super(normalClass, swappedClass);
063      this.swapFunction = swapFunction;
064      this.unswapFunction = unswapFunction;
065   }
066
067   @Override
068   public S swap(BeanSession session, T o, String template) throws Exception {
069      if (swapFunction == null)
070         return super.swap(session, o, template);
071      return swapFunction.applyThrows(o);
072   }
073
074   @Override
075   public T unswap(BeanSession session, S f, ClassMeta<?> hint, String template) throws Exception {
076      if (unswapFunction == null)
077         return super.unswap(session, f, hint, template);
078      return unswapFunction.applyThrows(f);
079   }
080}