Make reverse() more efficient, fix checkstyle, add test
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java index 94f0911..56ade60 100644 --- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java +++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/add/AddRequestTest.java
@@ -713,11 +713,11 @@ Dn dn = new Dn( "cn=test,ou=users,ou=system" ); originalAddRequest.setEntryDn( dn ); Entry entry = new DefaultEntry( dn ); - for ( int attributeIndex = 0; attributeIndex < 10000; attributeIndex++ ) + for ( int attributeIndex = 0; attributeIndex < 100000; attributeIndex++ ) { entry.add( "objectclass" + attributeIndex, "top", "person" ); } - String[] values = IntStream.range( 0, 10000 ).boxed().map( i -> "value" + i ).toArray( String[]::new ); + String[] values = IntStream.range( 0, 100000 ).boxed().map( i -> "value" + i ).toArray( String[]::new ); entry.add( "objectclass", values ); originalAddRequest.setEntry( entry );
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/modify/ModifyRequestTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/modify/ModifyRequestTest.java index 7ee38b8..c19a61f 100644 --- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/modify/ModifyRequestTest.java +++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/modify/ModifyRequestTest.java
@@ -1242,12 +1242,12 @@ originalModifyRequest.setMessageId( 3 ); Dn dn = new Dn( "cn=test,ou=users,ou=system" ); originalModifyRequest.setName( dn ); - for ( int modIndex = 0; modIndex < 10000; modIndex++ ) + for ( int modIndex = 0; modIndex < 100000; modIndex++ ) { originalModifyRequest.addModification( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, "objectclass" + modIndex, "top", "person" ) ); } - String[] values = IntStream.range( 0, 10000 ).boxed().map( i -> "value" + i ).toArray( String[]::new ); + String[] values = IntStream.range( 0, 100000 ).boxed().map( i -> "value" + i ).toArray( String[]::new ); originalModifyRequest.addModification( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, "objectclass", values ) );
diff --git a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/search/SearchResultEntryTest.java b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/search/SearchResultEntryTest.java index 33f8748..5f2b00d 100644 --- a/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/search/SearchResultEntryTest.java +++ b/ldap/codec/core/src/test/java/org/apache/directory/api/ldap/codec/search/SearchResultEntryTest.java
@@ -990,11 +990,11 @@ Dn dn = new Dn( "cn=test,ou=users,ou=system" ); originalSearchResultEntry.setObjectName( dn ); Entry entry = new DefaultEntry( dn ); - for ( int attributeIndex = 0; attributeIndex < 10000; attributeIndex++ ) + for ( int attributeIndex = 0; attributeIndex < 100000; attributeIndex++ ) { entry.add( "objectclass" + attributeIndex, "top", "person" ); } - String[] values = IntStream.range( 0, 10000 ).boxed().map( i -> "value" + i ).toArray( String[]::new ); + String[] values = IntStream.range( 0, 100000 ).boxed().map( i -> "value" + i ).toArray( String[]::new ); entry.add( "objectclass", values ); originalSearchResultEntry.setEntry( entry );
diff --git a/util/src/main/java/org/apache/directory/api/util/CollectionUtils.java b/util/src/main/java/org/apache/directory/api/util/CollectionUtils.java index acc5b43..c36a47d 100644 --- a/util/src/main/java/org/apache/directory/api/util/CollectionUtils.java +++ b/util/src/main/java/org/apache/directory/api/util/CollectionUtils.java
@@ -20,9 +20,8 @@ package org.apache.directory.api.util; -import java.util.ArrayList; +import java.util.ArrayDeque; import java.util.Iterator; -import java.util.List; /** @@ -32,13 +31,18 @@ */ public final class CollectionUtils { + private CollectionUtils() + { + } + + public static <T> Iterator<T> reverse( Iterator<T> iterator ) { - List<T> rev = new ArrayList<>(); + ArrayDeque<T> deque = new ArrayDeque<>(); while ( iterator.hasNext() ) { - rev.add( 0, iterator.next() ); + deque.addLast( iterator.next() ); } - return rev.iterator(); + return deque.descendingIterator(); } }
diff --git a/util/src/test/java/org/apache/directory/api/util/CollectionUtilsTest.java b/util/src/test/java/org/apache/directory/api/util/CollectionUtilsTest.java new file mode 100644 index 0000000..a73b2c3 --- /dev/null +++ b/util/src/test/java/org/apache/directory/api/util/CollectionUtilsTest.java
@@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.directory.api.util; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.junit.jupiter.api.Test; + + +/** + * A test case for CollectionUtils. + * + * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> + */ +class CollectionUtilsTest +{ + + @Test + void testReverse() + { + List<Integer> original = Arrays.asList( 1, 2, 3 ); + + Iterator<Integer> reverse = CollectionUtils.reverse( original.iterator() ); + + assertTrue( reverse.hasNext() ); + assertEquals( 3, reverse.next() ); + assertTrue( reverse.hasNext() ); + assertEquals( 2, reverse.next() ); + assertTrue( reverse.hasNext() ); + assertEquals( 1, reverse.next() ); + assertFalse( reverse.hasNext() ); + } + +}