Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Added stream oriented iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
krosenvold committed Dec 13, 2014
1 parent b2c10bd commit b77c098
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<artifactId>plexus-io</artifactId>
<version>2.4.2-SNAPSHOT</version>
<version>2.5-SNAPSHOT</version>

<name>Plexus IO Components</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public static void chmod( @Nonnull File file, int mode )
}
}

public static @Nonnull Set<PosixFilePermission> getPermissions( int mode )
@Nonnull
public static Set<PosixFilePermission> getPermissions( int mode )
{
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
//add owners permission
Expand Down Expand Up @@ -106,17 +107,24 @@ public static void chmod( @Nonnull File file, int mode )
return perms;
}

public static @Nonnull PosixFileAttributes getPosixFileAttributes( @Nonnull File file )
@Nonnull
public static PosixFileAttributes getPosixFileAttributes( @Nonnull File file )
throws IOException
{
return Files.readAttributes( file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
}

public static @Nonnull BasicFileAttributes getFileAttributes( @Nonnull File file )
@Nonnull
public static BasicFileAttributes getFileAttributes( @Nonnull File file )
throws IOException
{
final Path path = file.toPath();
if (path.getFileSystem().supportedFileAttributeViews().contains( "posix" ))
return getFileAttributes( file.toPath() );
}

public static BasicFileAttributes getFileAttributes( Path path )
throws IOException
{
if ( path.getFileSystem().supportedFileAttributeViews().contains( "posix" ) )
{

try
Expand All @@ -131,7 +139,8 @@ public static void chmod( @Nonnull File file, int mode )
return Files.readAttributes( path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS );
}

public static @Nullable FileOwnerAttributeView getFileOwnershipInfo( @Nonnull File file )
@Nullable
public static FileOwnerAttributeView getFileOwnershipInfo( @Nonnull File file )
throws IOException
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.codehaus.plexus.components.io.functions;
/*
* Copyright 2014 The Codehaus Foundation.
*
* Licensed 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 java.io.IOException;

import org.codehaus.plexus.components.io.resources.PlexusIoResource;

/**
* Consume a PlexusIoResource
* @author Kristian Rosenvold
*/
public interface PlexusIoResourceConsumer
{
void accept( PlexusIoResource resource ) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;

import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;

/**
* Default implementation of {@link PlexusIoFileResourceCollection} for
Expand Down Expand Up @@ -82,6 +83,31 @@ public Iterator<PlexusIoResource> getResources() throws IOException
return result.iterator();
}

public Stream stream()
{
return new Stream()
{
public void forEach( PlexusIoResourceConsumer resourceConsumer )
throws IOException
{

final Iterator<PlexusIoResource> it = getEntries();
while( it.hasNext())
{
final PlexusIoResource res = it.next();
if ( isSelected( res ) )
{
resourceConsumer.accept( res );
}
}
if (it instanceof Closeable )
{
((Closeable)it).close();
}
}
};
}

public long getLastModified() throws IOException
{
File f = getFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.functions.ContentSupplier;
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;

import javax.annotation.Nonnull;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -75,6 +77,27 @@ public void setStreamTransformer( InputStreamTransformer streamTransformers )
this.streamTransformers = streamTransformers;
}

public Stream stream()
{
return new Stream()
{
public void forEach( PlexusIoResourceConsumer resourceConsumer )
throws IOException
{

final Iterator<PlexusIoResource> it = getResources();
while( it.hasNext())
{
resourceConsumer.accept( it.next() );
}
if (it instanceof Closeable )
{
((Closeable)it).close();
}
}
};
}

public Iterator<PlexusIoResource> getResources()
throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -199,6 +201,30 @@ private void addResourcesJava7( List<PlexusIoResource> result, String[] resource
}
}

public Stream stream()
{
return new Stream()
{
public void forEach( PlexusIoResourceConsumer resourceConsumer )
throws IOException
{
Iterator<PlexusIoResource> resources = getResources();
while (resources.hasNext()){
PlexusIoResource next = resources.next();
if (isSelected( next ))
{
resourceConsumer.accept( next );
}
}
if (resources instanceof Closeable )
{
((Closeable)resources).close();
}

}
};
}

public Iterator<PlexusIoResource> getResources()
throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.util.Iterator;

import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;

/**
* A resource collection is a set of {@link PlexusIoResource} instances.
Expand All @@ -45,12 +46,20 @@ public interface PlexusIoResourceCollection extends Iterable<PlexusIoResource>
Iterator<PlexusIoResource> getResources() throws IOException;

/**
* Returns the resources suggested name. This is used for
* integrating file mappers.
* @param resource A resource, which has been obtained by
* calling {@link #getResources()}.
* @return The resource name. If it is a file, it should be normalized to platform separators
* Returns the resources as a stream.
* @return A stream for functional iteration
*/
public Stream stream();



/**
* Returns the resources suggested name. This is used for
* integrating file mappers.
* @param resource A resource, which has been obtained by
* calling {@link #getResources()}.
* @return The resource name. If it is a file, it should be normalized to platform separators
*/
String getName( PlexusIoResource resource );

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.codehaus.plexus.components.io.resources;
/*
* Copyright 2014 The Codehaus Foundation.
*
* Licensed 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.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;

import java.io.IOException;

public interface Stream
{
/**
* Invokes the #PlexusIoResourceConsumer for each resource in this collection
* @param resourceConsumer The consumer of the resource
* @throws java.io.IOException .
*/
public void forEach(PlexusIoResourceConsumer resourceConsumer) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
* limitations under the License.
*/

import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.attributes.SimpleResourceAttributes;
import org.codehaus.plexus.components.io.filemappers.FileMapper;
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
import org.codehaus.plexus.components.io.functions.NameSupplier;
import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;
import org.codehaus.plexus.components.io.functions.ResourceAttributeSupplier;
import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResourceCollection;
import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResourceCollectionWithAttributes;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
import org.codehaus.plexus.components.io.resources.Stream;

import javax.annotation.Nonnull;
import java.io.Closeable;
Expand Down Expand Up @@ -198,6 +199,11 @@ public PlexusIoResourceAttributes getAttributes()
}
}

public Stream stream()
{
return getSrc().stream();
}

public Iterator<PlexusIoResource> getResources()
throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import junit.framework.TestCase;
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;

import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
Expand All @@ -27,6 +28,12 @@ public Iterator<PlexusIoResource> getResources()
{
return Arrays.asList(getResource( "r1" ), getResource( "r2" )).iterator();
}

public Stream stream()
{
throw new UnsupportedOperationException();
}

};

sut.setStreamTransformer( new InputStreamTransformer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/

import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.components.io.functions.PlexusIoResourceConsumer;
import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResource;
import org.codehaus.plexus.components.io.resources.AbstractPlexusIoResourceCollection;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.components.io.resources.Stream;

import javax.annotation.Nonnull;
import java.io.Closeable;
Expand Down Expand Up @@ -115,6 +117,11 @@ public Iterator<PlexusIoResource> getResources()
{
return closeableIterator;
}

public Stream stream()
{
throw new UnsupportedOperationException();
}
} );
Iterator<PlexusIoResource> resources1 = resCol.getResources();
resources1.hasNext();
Expand Down

0 comments on commit b77c098

Please sign in to comment.