From 661f3d38943f26a1a1ccd1f0080779046c9a8c55 Mon Sep 17 00:00:00 2001 From: Stefan Bischof Date: Fri, 3 Dec 2021 00:43:03 +0100 Subject: [PATCH] remove apache.commons.coll: CollectionUtils.filter, CompositeCollection ComparatorChain, ComparatorUtils Signed-off-by: Stefan Bischof --- .../mondrian/olap/fun/sort/SorterTest.java | 22 +++++++++++-------- .../java/mondrian/test/PerformanceTest.java | 2 +- .../src/main/java/mondrian/olap/Query.java | 15 +++++-------- .../java/mondrian/olap/fun/sort/Sorter.java | 22 +++++++++---------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/mondrian/src/it/java/mondrian/olap/fun/sort/SorterTest.java b/mondrian/src/it/java/mondrian/olap/fun/sort/SorterTest.java index edfd162581..040beec8ea 100644 --- a/mondrian/src/it/java/mondrian/olap/fun/sort/SorterTest.java +++ b/mondrian/src/it/java/mondrian/olap/fun/sort/SorterTest.java @@ -26,6 +26,7 @@ import mondrian.olap.fun.MemberOrderKeyFunDef; import mondrian.server.Execution; import mondrian.server.Statement; +import java.util.Comparator; import org.apache.commons.collections.comparators.ComparatorChain; import org.mockito.ArgumentCaptor; import org.mockito.Captor; @@ -57,8 +58,8 @@ public class SorterTest extends TestCase { @Mock Hierarchy hierarchy2; @Mock Calc calc1; @Mock Calc calc2; - @Mock ComparatorChain comparatorChain; - @Captor ArgumentCaptor comparatorCaptor; + @Mock Comparator comparatorChain; + @Captor ArgumentCaptor comparatorCaptor; public void setUp() throws Exception { @@ -83,31 +84,34 @@ public void testComparatorSelectionBrkOrderByKey() { setupSortKeyMocks( true, Sorter.Flag.BASC, Sorter.Flag.BDESC ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec1 ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec2 ); - verify( comparatorChain ).addComparator( any( TupleExpMemoComparator.BreakTupleComparator.class ), eq( false ) ); - verify( comparatorChain ).addComparator( any( TupleExpMemoComparator.BreakTupleComparator.class ), eq( true ) ); + verify( comparatorChain, times( 2 ) ).thenComparing( comparatorCaptor.capture()); + assertTrue( comparatorCaptor.getAllValues().get( 0 ) instanceof TupleExpMemoComparator.BreakTupleComparator ); + assertTrue( comparatorCaptor.getAllValues().get( 1 ).reversed() instanceof TupleExpMemoComparator.BreakTupleComparator ); } public void testComparatorSelectionBrkNotOrderByKey() { setupSortKeyMocks( false, Sorter.Flag.BASC, Sorter.Flag.BDESC ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec1 ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec2 ); - verify( comparatorChain ).addComparator( any( TupleExpMemoComparator.BreakTupleComparator.class ), eq( false ) ); - verify( comparatorChain ).addComparator( any( TupleExpMemoComparator.BreakTupleComparator.class ), eq( true ) ); + verify( comparatorChain, times( 2 )).thenComparing(comparatorCaptor.capture()); + assertTrue( comparatorCaptor.getAllValues().get( 0 ) instanceof TupleExpMemoComparator.BreakTupleComparator ); + assertTrue( comparatorCaptor.getAllValues().get( 1 ).reversed() instanceof TupleExpMemoComparator.BreakTupleComparator ); } public void testComparatorSelectionNotBreakingOrderByKey() { setupSortKeyMocks( true, Sorter.Flag.ASC, Sorter.Flag.DESC ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec1 ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec2 ); - verify( comparatorChain ).addComparator( any( HierarchicalTupleKeyComparator.class ), eq( false ) ); - verify( comparatorChain ).addComparator( any( HierarchicalTupleKeyComparator.class ), eq( true ) ); + verify( comparatorChain, times( 2 )).thenComparing(comparatorCaptor.capture()); + assertTrue( comparatorCaptor.getAllValues().get( 0 ) instanceof HierarchicalTupleKeyComparator ); + assertTrue( comparatorCaptor.getAllValues().get( 1 ).reversed() instanceof HierarchicalTupleKeyComparator ); } public void testComparatorSelectionNotBreaking() { setupSortKeyMocks( false, Sorter.Flag.ASC, Sorter.Flag.DESC ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec1 ); Sorter.applySortSpecToComparator( evaluator, 2, comparatorChain, sortKeySpec2 ); - verify( comparatorChain, times( 2 ) ).addComparator( comparatorCaptor.capture(), eq( false ) ); + verify( comparatorChain, times( 2 )).thenComparing( comparatorCaptor.capture() ); assertTrue( comparatorCaptor.getAllValues().get( 0 ) instanceof HierarchicalTupleComparator ); assertTrue( comparatorCaptor.getAllValues().get( 1 ) instanceof HierarchicalTupleComparator ); } diff --git a/mondrian/src/it/java/mondrian/test/PerformanceTest.java b/mondrian/src/it/java/mondrian/test/PerformanceTest.java index 50fe33b81b..0158bb320f 100644 --- a/mondrian/src/it/java/mondrian/test/PerformanceTest.java +++ b/mondrian/src/it/java/mondrian/test/PerformanceTest.java @@ -735,7 +735,7 @@ void foo( int runCount, List list, int limit ) { } } @SuppressWarnings( "unchecked" ) final Comparator comp = - ComparatorUtils.naturalComparator(); + Comparator.naturalOrder(); final long start = System.currentTimeMillis(); List x = sort( list, comp, limit ); statistician.record( start ); diff --git a/mondrian/src/main/java/mondrian/olap/Query.java b/mondrian/src/main/java/mondrian/olap/Query.java index ff65b594a0..303568d21f 100644 --- a/mondrian/src/main/java/mondrian/olap/Query.java +++ b/mondrian/src/main/java/mondrian/olap/Query.java @@ -20,8 +20,6 @@ import mondrian.spi.ProfileHandler; import mondrian.util.ArrayStack; -import org.apache.commons.collections.collection.CompositeCollection; - import org.olap4j.impl.*; import org.olap4j.mdx.IdentifierSegment; @@ -552,13 +550,12 @@ public void explain( PrintWriter pw ) { * @return Collection of all axes including slicer */ private Collection allAxes() { - if ( slicerAxis == null ) { - return Arrays.asList( axes ); - } else { - // noinspection unchecked - return new CompositeCollection( new Collection[] { Collections.singletonList( slicerAxis ), Arrays.asList( - axes ) } ); - } + List list=new ArrayList<>(); + list.addAll( Arrays.asList( axes )); + if ( slicerAxis != null ) { + list.add(slicerAxis); + } + return list; } public void unparse( PrintWriter pw ) { diff --git a/mondrian/src/main/java/mondrian/olap/fun/sort/Sorter.java b/mondrian/src/main/java/mondrian/olap/fun/sort/Sorter.java index f85b224b65..6dda98c53a 100644 --- a/mondrian/src/main/java/mondrian/olap/fun/sort/Sorter.java +++ b/mondrian/src/main/java/mondrian/olap/fun/sort/Sorter.java @@ -29,8 +29,6 @@ import mondrian.server.Execution; import mondrian.util.CancellationChecker; -import org.apache.commons.collections.ComparatorUtils; -import org.apache.commons.collections.comparators.ComparatorChain; import org.apache.log4j.Logger; import java.util.AbstractList; @@ -244,7 +242,7 @@ public static List sortMembers( } } - ComparatorChain chain = new ComparatorChain(); + Comparator baseComp = (o1, o2) -> 0; for ( SortKeySpec key : keySpecList ) { boolean brk = key.getDirection().brk; MemberComparator comp; @@ -256,10 +254,10 @@ public static List sortMembers( evaluator, key.getKey(), key.getDirection().descending ); } comp.preloadValues( memberList ); - chain.addComparator( comp.wrap(), false ); + baseComp=baseComp.thenComparing(comp.wrap()); } - memberList.sort( chain ); + memberList.sort( baseComp ); return memberList; } @@ -411,9 +409,9 @@ public static TupleList sortTuples( return tupleList; } - ComparatorChain chain = new ComparatorChain(); + Comparator> chain = (o1, o2) -> 0; for ( SortKeySpec key : keySpecList ) { - applySortSpecToComparator( evaluator, arity, chain, key ); + chain= applySortSpecToComparator( evaluator, arity, chain, key ); } tupleList.sort( chain ); logTuples( tupleList, "Sorter.sortTuples" ); @@ -421,7 +419,7 @@ public static TupleList sortTuples( } @VisibleForTesting - static void applySortSpecToComparator( Evaluator evaluator, int arity, ComparatorChain chain, + static Comparator> applySortSpecToComparator( Evaluator evaluator, int arity, Comparator> comparator, SortKeySpec key ) { boolean brk = key.getDirection().brk; boolean orderByKey = @@ -430,15 +428,15 @@ static void applySortSpecToComparator( Evaluator evaluator, int arity, Comparato if ( brk ) { TupleExpMemoComparator comp = new TupleExpMemoComparator.BreakTupleComparator( evaluator, key.getKey(), arity ); - chain.addComparator( comp, direction ); + return comparator.thenComparing(direction? comp.reversed():comp); } else if ( orderByKey ) { TupleExpMemoComparator comp = new HierarchicalTupleKeyComparator( evaluator, key.getKey(), arity ); - chain.addComparator( comp, direction ); + return comparator.thenComparing(direction? comp.reversed():comp); } else { TupleComparator.TupleExpComparator comp = new HierarchicalTupleComparator( evaluator, key.getKey(), arity, direction ); - chain.addComparator( comp, false ); // ordering handled in the comparator. + return comparator.thenComparing(comp); // ordering handled in the comparator. } } @@ -764,7 +762,7 @@ public static int compareSiblingMembers( Member m1, Member m2 ) { static void partialSort( T[] items, Comparator comp, int limit ) { if ( comp == null ) { //noinspection unchecked - comp = (Comparator) ComparatorUtils.naturalComparator(); + comp = (Comparator) Comparator.naturalOrder(); } new Quicksorter<>( items, comp ).partialSort( limit ); }