com.feilong.context.converter.IfStringToBeanConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.context.converter;
import org.apache.commons.collections4.Predicate;
import com.feilong.lib.collection4.functors.IfClosure;
import com.feilong.lib.collection4.functors.IfTransformer;
/**
* 根据predicate 条件,来判断是使用 trueStringToBeanConverter 还是falseStringToBeanConverter来转换结果.
*
* 使用场景:
*
*
* 比如支付宝查询接口, 正常返回的结果是这样子的
*
*
{@code
*
* T
*
* 2010073000030344
* single_trade_query
* 2088002007018916
*
*
*
* 合同催款通知
* 2088102002723445
* 0.00
* 2010-07-30 12:30:29
* F
* 1280463992953
*
*
* 56ae9c3286886f76e57e0993625c71fe
* MD5
*
* }
*
*
* 发生异常返回的结果是这样子的
*
*
* {@code
*
* F
* TRADE_IS_NOT_EXIST
*
* }
*
*
*
* 此时xml 可以配置成
*
*
* {@code
}
*
*
*
*
* @author feilong
* @param
* the generic type
* @see IfTransformer
* @see IfClosure
* @since 1.14.3
*/
public class IfStringToBeanConverter extends AbstractStringToBeanConverter{
/** 指定的判定条件. */
private Predicate predicate;
/** 如果满足条件,使用的对应的字符串转bean converter. */
private StringToBeanConverter trueStringToBeanConverter;
/** 如果不满足条件,使用的对应的字符串转bean converter. */
private StringToBeanConverter falseStringToBeanConverter;
//---------------------------------------------------------------
/**
* Instantiates a new if string to bean converter.
*/
public IfStringToBeanConverter(){
}
/**
* Instantiates a new if string to bean converter.
*
* @param predicate
* the predicate
* @param trueStringToBeanConverter
* the true string to bean converter
* @param falseStringToBeanConverter
* the false string to bean converter
*/
public IfStringToBeanConverter(Predicate predicate, StringToBeanConverter trueStringToBeanConverter,
StringToBeanConverter falseStringToBeanConverter){
super();
this.predicate = predicate;
this.trueStringToBeanConverter = trueStringToBeanConverter;
this.falseStringToBeanConverter = falseStringToBeanConverter;
}
//---------------------------------------------------------------
/**
* Transforms the input using the true or false transformer based to the result of the predicate.
*
* @param inputString
* the input string
* @return the transformed result
*/
@Override
protected T handler(String inputString){
if (predicate.evaluate(inputString)){
return trueStringToBeanConverter.convert(inputString);
}
return falseStringToBeanConverter.convert(inputString);
}
//---------------------------------------------------------------
/**
* 设置 指定的判定条件.
*
* @param predicate
* the new 指定的判定条件
*/
public void setPredicate(Predicate predicate){
this.predicate = predicate;
}
/**
* 设置 如果满足条件,使用的对应的字符串转bean converter.
*
* @param trueStringToBeanConverter
* the new 如果满足条件,使用的对应的字符串转bean converter
*/
public void setTrueStringToBeanConverter(StringToBeanConverter trueStringToBeanConverter){
this.trueStringToBeanConverter = trueStringToBeanConverter;
}
/**
* 设置 如果不满足条件,使用的对应的字符串转bean converter.
*
* @param falseStringToBeanConverter
* the new 如果不满足条件,使用的对应的字符串转bean converter
*/
public void setFalseStringToBeanConverter(StringToBeanConverter falseStringToBeanConverter){
this.falseStringToBeanConverter = falseStringToBeanConverter;
}
}