Skip to content
Permalink
Browse files
Make reverse() more efficient, fix checkstyle, add test
  • Loading branch information
seelmann committed Apr 4, 2021
1 parent c5acb33 commit 286524c2a2bd90e2a771a150649f7aaff1981ff1
Showing 5 changed files with 74 additions and 11 deletions.
@@ -713,11 +713,11 @@ public void testEncodeDecodeLarge() throws DecoderException, EncoderException, L
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 );

@@ -1242,12 +1242,12 @@ public void testEncodeDecodeLarge() throws DecoderException, EncoderException, L
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 ) );

@@ -990,11 +990,11 @@ public void testEncodeDecodeLarge() throws DecoderException, EncoderException, L
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 );

@@ -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();
}
}
@@ -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() );
}

}

0 comments on commit 286524c

Please sign in to comment.