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.marshall.swap.spi; 018 019import org.apache.juneau.commons.function.*; 020import org.apache.juneau.marshall.*; 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/Swaps">Swap</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 064 * The function for converting swapped to normal. 065 * Can be <jk>null</jk> if this swap is serialize-only (calling {@link #unswap(MarshallingSession, Object, ClassMeta, String)} then throws a {@link org.apache.juneau.marshall.parser.ParseException}). 066 */ 067 public FunctionalSwap(Class<T> normalClass, Class<S> swappedClass, ThrowingFunction<T,S> swapFunction, ThrowingFunction<S,T> unswapFunction) { 068 super(normalClass, swappedClass); 069 this.swapFunction = swapFunction; 070 this.unswapFunction = unswapFunction; 071 } 072 073 @Override 074 public S swap(MarshallingSession session, T o, String template) throws Exception { 075 if (swapFunction == null) 076 return super.swap(session, o, template); 077 return swapFunction.applyThrows(o); 078 } 079 080 @Override 081 public T unswap(MarshallingSession session, S f, ClassMeta<?> hint, String template) throws Exception { 082 if (unswapFunction == null) 083 return super.unswap(session, f, hint, template); 084 return unswapFunction.applyThrows(f); 085 } 086}