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