字段对字段的 Get/Set

【字段对字段的 Get/Set】

开发中,肯定遇到过前人留下的,某个类,需要对另外一个类,一个字段一个字段的 get/set

就像下面的这样

/* 有意写的比较繁琐,来突出 xx 效果,但其实今天见了更繁琐的 */
b.setF_1(a.getF1() == null ? 1 : a.getF1().shortValue());
b.setF2(a.getF2() == null || "".equals(a.getF2().trim()) ? "f2" : a.getF2().trim());
b.setF_3(a.getF3() == null ? 1 : a.getF3().shortValue());
b.setF4(a.getF4());

如果字段名类型都一致,只是个数有差异,直接可以用 BeanUtil.copyProperties,如果只是驼峰转下划线,下划线转驼峰之类的比较有规律的也还好说,配置下转 map 的规则,转了 map 再转回来就可以了。

遇到不仅字段名不一样,没有规则,类型还不一样的,那真是看着前人的代码也难受,自己再往里边儿填的时候也难受。得益于 Java 8 以后的 @nFunctionalInterface,这个功能可以实现的比较 好看 些。

首先要明确一件事情 get 方法其实就一种 Supplier,set 方法是一种 Consumer,那事情就简单了些。

基本思想就是把 get/set 方法当参数传进去,在函数内做转换就可以了。

/* 可以根据需要裁剪方法的参数更改设置为 defaultV 的条件 */
public static <A, B> void set(Consumer<A> setMethod,
                              Supplier<B> getMethod,
                              A defaultV,
                              Predicate<B> defaultValueCondition,
                              Function<B, A> convertFunction) {
    // 函数体的粗实现
    B v = getMethod.get();

    if (v == null || (defaultValueCondition != null && defaultValueCondition.test(v))) {
        setMethod.accept(defaultV);
    } else {
        if (convertFunction == null) {
            setMethod.accept((A) v);
        } else {
            setMethod.accept(convertFunction.apply(v));
        }
    }
}

public static <A, B> void set(Consumer<A> setMethod,
                              Supplier<B> getMethod,
                              A defaultV,
                              Function<B, A> convertFunction);

public static <A, B> void set(Consumer<A> setMethod,
                              Supplier<B> getMethod,
                              A defaultV,
                              Predicate<B> defaultValueCondition);

public static <A, B> void set(Consumer<A> setMethod,
                              Supplier<B> getMethod,
                              A defaultV);

public static <A, B> void set(Consumer<A> setMethod,
                              Supplier<B> getMethod);

最终效果

/*原有代码*/
b.setF_1(a.getF1() == null ? 1 : a.getF1().shortValue());
b.setF2(a.getF2() == null || "".equals(a.getF2().trim()) ? "f2" : a.getF2().trim());
b.setF_3(a.getF3() == null ? 1 : a.getF3().shortValue());
b.setF4(a.getF4());

/*修改后*/
GetSet.set(b::setF_1, a::getF1, (short) 1, Integer::shortValue);
GetSet.set(b::setF2,  a::getF2,      "f2", StringUtils::isBlank);
GetSet.set(b::setF_3, a::getF3, (short) 1, Integer::shortValue);
GetSet.set(b::setF4,  a::getF4);

对比之下的话,感觉下面的还是比较给力的。

另外还抽出来两个接口,专门用来表示 Get/Set 方法

@FunctionalInterface
public interface IGet<T> {
    T get();
}

@FunctionalInterface
public interface ISet<T> {
    void set(T t);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lixifun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值