import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
/**
*
}
}
+ // In reverse order
List<StorageAsset> getChildContainers( StorageAsset parent) {
- return parent.list( ).stream( ).filter( StorageAsset::isContainer ).collect( Collectors.toList( ) );
+ final List<StorageAsset> children = parent.list( );
+ final int len = children.size( );
+ return IntStream.range( 0, children.size( ) ).mapToObj( i ->
+ children.get(len - i - 1)).filter( StorageAsset::isContainer ).collect( Collectors.toList( ) );
}
+ // In reverse order
List<StorageAsset> getChildFiles(StorageAsset parent) {
- return parent.list( ).stream( ).filter( StorageAsset::isLeaf ).collect( Collectors.toList( ) );
+ final List<StorageAsset> children = parent.list( );
+ final int len = children.size( );
+ return IntStream.range( 0, children.size( ) ).mapToObj( i ->
+ children.get(len - i - 1)).filter( StorageAsset::isLeaf ).collect( Collectors.toList( ) );
}
--- /dev/null
+package org.apache.archiva.repository.storage;
+
+/*
+ * 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.
+ */
+
+import org.apache.archiva.repository.storage.mock.MockAsset;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Test the AssetSpliterator class
+ *
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+class AssetSpliteratorTest
+{
+
+ private StorageAsset createTree() {
+ MockAsset root = new MockAsset( "" );
+ for (int i=0; i<10; i++) {
+ String name1 = "a" + String.format("%03d",i);
+ MockAsset parent1 = new MockAsset( root, name1 );
+ for (int k=0; k<15; k++) {
+ String name2 = name1 + String.format("%03d", k);
+ MockAsset parent2 = new MockAsset( parent1, name2 );
+ for (int u=0; u<5; u++) {
+ String name3 = name2 + String.format("%03d", u);
+ MockAsset parent3 = new MockAsset( parent2, name3 );
+ }
+ }
+ }
+ return root;
+ }
+
+ private class Status {
+ LinkedList<StorageAsset> visited = new LinkedList<>( );
+
+ Status() {
+
+ }
+
+ public void add(StorageAsset asset) {
+ visited.addLast( asset );
+ }
+
+ public StorageAsset getLast() {
+ return visited.getLast( );
+ }
+
+ public List<StorageAsset> getVisited() {
+ return visited;
+ }
+
+ public int size() {
+ return visited.size( );
+ }
+ }
+
+ @Test
+ void tryAdvance( )
+ {
+ StorageAsset root = createTree( );
+ AssetSpliterator spliterator = new AssetSpliterator( root );
+ final StorageAsset expectedTarget = root.list( ).get( 0 ).list( ).get( 0 ).list( ).get( 0 );
+ final Status status = new Status( );
+ spliterator.tryAdvance( a -> status.add( a ) );
+ assertEquals( expectedTarget, status.getLast( ) );
+ }
+
+ @Test
+ void forEachRemaining( )
+ {
+ }
+
+ @Test
+ void trySplit( )
+ {
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.apache.archiva.repository.storage.mock;
+
+/*
+ * 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.
+ */
+
+import org.apache.archiva.repository.storage.RepositoryStorage;
+import org.apache.archiva.repository.storage.StorageAsset;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.nio.file.Path;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class MockAsset implements StorageAsset
+{
+ private StorageAsset parent;
+ private String path;
+ private String name;
+ private LinkedHashMap<String, StorageAsset> children = new LinkedHashMap<>( );
+ private boolean container = false;
+
+ public MockAsset( String name ) {
+ this.name = name;
+ this.path = "";
+ }
+
+ public MockAsset( MockAsset parent, String name ) {
+ this.parent = parent;
+ this.path = parent.getPath( ) + "/" + name;
+ this.name = name;
+ parent.registerChild( this );
+ }
+
+ public void registerChild(StorageAsset child) {
+ children.putIfAbsent( child.getName(), child );
+ this.container = true;
+ }
+
+ @Override
+ public RepositoryStorage getStorage( )
+ {
+ return null;
+ }
+
+ @Override
+ public String getPath( )
+ {
+ return this.path;
+ }
+
+ @Override
+ public String getName( )
+ {
+ return this.name;
+ }
+
+ @Override
+ public Instant getModificationTime( )
+ {
+ return Instant.now();
+ }
+
+ @Override
+ public boolean isContainer( )
+ {
+ return this.container;
+ }
+
+ @Override
+ public boolean isLeaf( )
+ {
+ return !this.container;
+ }
+
+ @Override
+ public List<StorageAsset> list( )
+ {
+ return new ArrayList( children.values( ) );
+ }
+
+ @Override
+ public long getSize( )
+ {
+ return 0;
+ }
+
+ @Override
+ public InputStream getReadStream( ) throws IOException
+ {
+ return null;
+ }
+
+ @Override
+ public ReadableByteChannel getReadChannel( ) throws IOException
+ {
+ return null;
+ }
+
+ @Override
+ public OutputStream getWriteStream( boolean replace ) throws IOException
+ {
+ return null;
+ }
+
+ @Override
+ public WritableByteChannel getWriteChannel( boolean replace ) throws IOException
+ {
+ return null;
+ }
+
+ @Override
+ public boolean replaceDataFromFile( Path newData ) throws IOException
+ {
+ return false;
+ }
+
+ @Override
+ public boolean exists( )
+ {
+ return false;
+ }
+
+ @Override
+ public void create( ) throws IOException
+ {
+
+ }
+
+ @Override
+ public Path getFilePath( ) throws UnsupportedOperationException
+ {
+ return null;
+ }
+
+ @Override
+ public boolean isFileBased( )
+ {
+ return false;
+ }
+
+ @Override
+ public boolean hasParent( )
+ {
+ return this.parent != null;
+ }
+
+ @Override
+ public StorageAsset getParent( )
+ {
+ return this.parent;
+ }
+
+ @Override
+ public StorageAsset resolve( String toPath )
+ {
+ if (children.containsKey( toPath )) {
+ return children.get( toPath );
+ } else {
+ return null;
+ }
+ }
+
+ @Override
+ public String toString( )
+ {
+ return getPath();
+ }
+}