简体   繁体   English

使用unitils ReflectionComparator忽略字符串中的大小写

[英]Ignoring case in strings with unitils ReflectionComparator

I'm using unitils tool for deep objects comparing, via ReflectionComparator : 我使用unitils工具通过ReflectionComparator进行深层对象比较:

ReflectionComparatorMode[] modes = {ReflectionComparatorMode.LENIENT_ORDER, ReflectionComparatorMode.IGNORE_DEFAULTS};
ReflectionComparator comparator = ReflectionComparatorFactory.createRefectionComparator(modes);
Difference difference = comparator.getDifference(oldObject, newObject);

It turns out that this ReflectionComparator doesn't ignore case in String fields values. 事实证明,此ReflectionComparator不会忽略String字段值中的大小写。 And there isn't sprecial mode for this purpose in ReflectionComparatorMode enum: ReflectionComparatorMode枚举中,没有用于此目的的串行模式:

public enum ReflectionComparatorMode {
    IGNORE_DEFAULTS,
    LENIENT_DATES,
    LENIENT_ORDER
}

Any ideas, how it could be achieved? 有什么想法,如何实现?

ReflectionComparatorMode has no mode to mimic ignore_case behavior. ReflectionComparatorMode没有模拟模仿ignore_case行为的模式。 You do not specify the types of oldObject and newObject but I guess you can either ' normalize ' them before passing them to ReflectionComparator (convert all String fields to either upper or lower case) or implement your own Java Comparator based on the specific type of oldObject and newObject . 您没有指定oldObjectnewObject的类型,但我想您可以在将它们传递给ReflectionComparator之前(将所有String字段都转换为大写或小写)进行“ 规范化 ”,或者根据oldObject的特定类型实现自己的Java Comparator和newObject

Check out this . 看看这个

Investigation of how ReflectionComparator works gave me this workable solution. ReflectionComparator工作方式进行调查后,得出了这个可行的解决方案。 Saying in brief, we have to add another one special Comparator object for dealing with String objects in comparators chain. 简而言之,我们必须添加另一个特殊的Comparator对象来处理比较器链中的String对象。

Also we have to do some bedlam with extracting one needed protected static method from ReflectionComparatorFactory in order to reduce code doubling. 此外,我们必须做一些喧闹与提取一个需要protected从静态方法ReflectionComparatorFactory为了减少代码加倍。

ReflectionComparatorMode[] modes = {ReflectionComparatorMode.LENIENT_ORDER, ReflectionComparatorMode.IGNORE_DEFAULTS};

List<org.unitils.reflectionassert.comparator.Comparator> comparators = new ArrayList<>();
    comparators.add(new Comparator() {
         @Override
         public boolean canCompare(Object left, Object right) {
               return left instanceof String && right instanceof String;
         }

         @Override
         public Difference compare(Object left, Object right, boolean onlyFirstDifference, ReflectionComparator reflectionComparator) {
               return  ((String) left).equalsIgnoreCase((String) right) ? null : new Difference("Non equal values: ", left, right);
         }
});

comparators.addAll(
    new ReflectionComparatorFactory() {
        public List<Comparator> getComparatorChainNonStatic(Set<ReflectionComparatorMode> modes) {
               return getComparatorChain(modes);
        }
    }.getComparatorChainNonStatic(asSet(modes)));

ReflectionComparator comparator = new ReflectionComparator(comparators);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM