commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject [commons-vfs] branch master updated: Modify some loop use Stream API (#96)
Date Wed, 05 Aug 2020 12:52:54 GMT
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new 8de5087  Modify some loop use Stream API (#96)
8de5087 is described below

commit 8de5087e0b9a77e7a1d00c72151ad9662474452c
Author: Lee <55973914+PeterAlfredLee@users.noreply.github.com>
AuthorDate: Wed Aug 5 20:52:44 2020 +0800

    Modify some loop use Stream API (#96)
---
 .../commons/vfs2/impl/DefaultFileSystemManager.java | 16 +++++-----------
 .../commons/vfs2/provider/AbstractFileObject.java   |  8 +-------
 .../commons/vfs2/provider/AbstractFileProvider.java |  8 +++-----
 .../commons/vfs2/provider/AbstractVfsContainer.java |  9 +++------
 .../vfs2/provider/CompositeFileProvider.java        |  9 +++------
 .../commons/vfs2/provider/DelegateFileObject.java   |  7 ++-----
 .../commons/vfs2/provider/hdfs/HdfsFileObject.java  | 10 +++-------
 .../provider/hdfs/HdfsFileSystemConfigBuilder.java  |  7 ++-----
 .../vfs2/provider/http4/Http4FileProvider.java      |  4 +---
 .../vfs2/provider/http5/Http5FileProvider.java      |  9 ++-------
 .../provider/sftp/SftpFileSystemConfigBuilder.java  | 21 +++++----------------
 11 files changed, 30 insertions(+), 78 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
index 6fe6273..28ed9f4 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileSystemManager.java
@@ -24,6 +24,7 @@ import java.net.URL;
 import java.net.URLStreamHandler;
 import java.net.URLStreamHandlerFactory;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
@@ -200,9 +201,7 @@ public class DefaultFileSystemManager implements FileSystemManager {
         setupComponent(provider);
 
         // Add to map
-        for (final String scheme : urlSchemes) {
-            providers.put(scheme, provider);
-        }
+        Arrays.stream(urlSchemes).forEach(scheme -> providers.put(scheme, provider));
 
         if (provider instanceof LocalFileProvider && localFileProvider == null) {
             localFileProvider = (LocalFileProvider) provider;
@@ -523,9 +522,7 @@ public class DefaultFileSystemManager implements FileSystemManager {
         // are closed here
 
         // Close the file system providers.
-        for (final FileProvider provider : providers.values()) {
-            closeComponent(provider);
-        }
+        providers.values().forEach(this::closeComponent);
 
         // Close the other components
         closeComponent(vfsProvider);
@@ -540,11 +537,8 @@ public class DefaultFileSystemManager implements FileSystemManager {
         providers.clear();
 
         // FileOperations are components, too
-        for (final List<FileOperationProvider> opproviders : operationProviders.values())
{
-            for (final FileOperationProvider p : opproviders) {
-                closeComponent(p);
-            }
-        }
+        operationProviders.values().forEach(opproviders -> opproviders.forEach(this::closeComponent));
+
         // unregister all
         operationProviders.clear();
 
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
index 7ab1a50..1ac2677 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
@@ -971,13 +971,7 @@ public abstract class AbstractFileObject<AFS extends AbstractFileSystem>
impleme
         if (objects == null) {
             return null;
         }
-
-        final FileName[] names = new FileName[objects.length];
-        for (int iterObjects = 0; iterObjects < objects.length; iterObjects++) {
-            names[iterObjects] = objects[iterObjects].getName();
-        }
-
-        return names;
+        return Arrays.stream(objects).map(FileObject::getName).toArray(FileName[]::new);
     }
 
     @Override
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileProvider.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileProvider.java
index 23f1f14..f76646d 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileProvider.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileProvider.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.vfs2.provider;
 
+import java.util.Arrays;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -141,11 +142,8 @@ public abstract class AbstractFileProvider extends AbstractVfsContainer
implemen
         }
 
         // process snapshot outside lock
-        for (final AbstractFileSystem fs : abstractFileSystems) {
-            if (fs.isReleaseable()) {
-                fs.closeCommunicationLink();
-            }
-        }
+        Arrays.stream(abstractFileSystems).filter(AbstractFileSystem::isReleaseable)
+                                          .forEach(AbstractFileSystem::closeCommunicationLink);
     }
 
     /**
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractVfsContainer.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractVfsContainer.java
index b10a0a8..8ded1ac 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractVfsContainer.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractVfsContainer.java
@@ -17,6 +17,7 @@
 package org.apache.commons.vfs2.provider;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 
 import org.apache.commons.vfs2.FileSystemException;
 
@@ -81,11 +82,7 @@ public abstract class AbstractVfsContainer extends AbstractVfsComponent
{
         }
 
         // Close all components
-        for (final Object component : toclose) {
-            if (component instanceof VfsComponent) {
-                final VfsComponent vfsComponent = (VfsComponent) component;
-                vfsComponent.close();
-            }
-        }
+        Arrays.stream(toclose).filter(component -> component instanceof VfsComponent)
+                              .forEach(component -> ((VfsComponent) component).close());
     }
 }
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/CompositeFileProvider.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/CompositeFileProvider.java
index 607b0bc..9db2cd2 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/CompositeFileProvider.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/CompositeFileProvider.java
@@ -20,6 +20,8 @@ import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemOptions;
 
+import java.util.Arrays;
+
 /**
  * Description.
  */
@@ -53,12 +55,7 @@ public abstract class CompositeFileProvider extends AbstractFileProvider
{
         final StringBuilder buf = new StringBuilder(INITIAL_BUFSZ);
 
         UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buf);
-
-        final String[] schemes = getSchemes();
-        for (final String scheme : schemes) {
-            buf.insert(0, ":");
-            buf.insert(0, scheme);
-        }
+        Arrays.stream(getSchemes()).forEach(scheme -> buf.insert(0, scheme + ":"));
 
         final FileObject fo = getContext().getFileSystemManager().resolveFile(buf.toString(),
fileSystemOptions);
         return fo;
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DelegateFileObject.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DelegateFileObject.java
index 8d60dae..f3113b8 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DelegateFileObject.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DelegateFileObject.java
@@ -19,6 +19,7 @@ package org.apache.commons.vfs2.provider;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.cert.Certificate;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -188,11 +189,7 @@ public class DelegateFileObject<AFS extends AbstractFileSystem>
extends Abstract
                 throw new FileNotFolderException(getName(), e);
             }
 
-            final String[] childNames = new String[children.length];
-            for (int i = 0; i < children.length; i++) {
-                childNames[i] = children[i].getName().getBaseName();
-            }
-            return childNames;
+            return Arrays.stream(children).map(child -> child.getName().getBaseName()).toArray(String[]::new);
         }
         return children.toArray(new String[children.size()]);
     }
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileObject.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileObject.java
index 8be836f..cac98a0 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileObject.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileObject.java
@@ -18,6 +18,7 @@ package org.apache.commons.vfs2.provider.hdfs;
 
 import java.io.FileNotFoundException;
 import java.io.InputStream;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -192,13 +193,8 @@ public class HdfsFileObject extends AbstractFileObject<HdfsFileSystem>
{
             throw new FileNotFolderException(this);
         }
 
-        final FileStatus[] files = this.hdfs.listStatus(this.path);
-        final String[] children = new String[files.length];
-        int i = 0;
-        for (final FileStatus status : files) {
-            children[i++] = status.getPath().getName();
-        }
-        return children;
+        final FileStatus[] fileStatuses = this.hdfs.listStatus(this.path);
+        return Arrays.stream(fileStatuses).map(status -> status.getPath().getName()).toArray(String[]::new);
     }
 
     /**
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystemConfigBuilder.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystemConfigBuilder.java
index de28927..6e0114a 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystemConfigBuilder.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystemConfigBuilder.java
@@ -19,6 +19,7 @@ package org.apache.commons.vfs2.provider.hdfs;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Arrays;
 
 import org.apache.commons.vfs2.FileSystem;
 import org.apache.commons.vfs2.FileSystemConfigBuilder;
@@ -122,11 +123,7 @@ public final class HdfsFileSystemConfigBuilder extends FileSystemConfigBuilder
{
             return null;
         }
         final String[] paths = pathNames.split(",");
-        final Path[] realPaths = new Path[paths.length];
-        for (int i = 0; i < paths.length; i++) {
-            realPaths[i] = new Path(paths[i]);
-        }
-        return realPaths;
+        return Arrays.stream(paths).map(Path::new).toArray(Path[]::new);
     }
 
     /**
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
index ddb501a..09cc701 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java
@@ -349,9 +349,7 @@ public class Http4FileProvider extends AbstractOriginatingFileProvider
{
         final Cookie[] cookies = builder.getCookies(fileSystemOptions);
 
         if (cookies != null) {
-            for (final Cookie cookie : cookies) {
-                cookieStore.addCookie(cookie);
-            }
+            Arrays.stream(cookies).forEach(cookieStore::addCookie);
         }
 
         return cookieStore;
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java
index b10d36b..7295e6a 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java
@@ -307,11 +307,8 @@ public class Http5FileProvider extends AbstractOriginatingFileProvider
{
                 .build();
 
         final String[] tlsVersions = builder.getTlsVersions(fileSystemOptions).split("\\s*,\\s*");
-        final TLS[] tlsArray = new TLS[tlsVersions.length];
 
-        for (int i = 0; i < tlsVersions.length; i++) {
-            tlsArray[i] = TLS.valueOf(tlsVersions[i]);
-        }
+        final TLS[] tlsArray = Arrays.stream(tlsVersions).map(TLS::valueOf).toArray(TLS[]::new);
 
         final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
                 .setSslContext(createSSLContext(builder, fileSystemOptions))
@@ -363,9 +360,7 @@ public class Http5FileProvider extends AbstractOriginatingFileProvider
{
         final Cookie[] cookies = builder.getCookies(fileSystemOptions);
 
         if (cookies != null) {
-            for (final Cookie cookie : cookies) {
-                cookieStore.addCookie(cookie);
-            }
+            Arrays.stream(cookies).forEach(cookieStore::addCookie);
         }
 
         return cookieStore;
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java
index 7b4ee64..8c4da17 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystemConfigBuilder.java
@@ -19,6 +19,7 @@ package org.apache.commons.vfs2.provider.sftp;
 import java.io.File;
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.apache.commons.vfs2.FileSystem;
@@ -206,11 +207,7 @@ public final class SftpFileSystemConfigBuilder extends FileSystemConfigBuilder
{
     public File[] getIdentities(final FileSystemOptions opts) {
         final IdentityInfo[] info = getIdentityInfo(opts);
         if (info != null) {
-            final File[] files = new File[info.length];
-            for (int i = 0; i < files.length; ++i) {
-                files[i] = info[i].getPrivateKey();
-            }
-            return files;
+            return Arrays.stream(info).map(IdentityInfo::getPrivateKey).toArray(File[]::new);
         }
         return null;
     }
@@ -225,13 +222,8 @@ public final class SftpFileSystemConfigBuilder extends FileSystemConfigBuilder
{
     public IdentityInfo[] getIdentityInfo(final FileSystemOptions opts) {
         final IdentityProvider[] infos = getIdentityProvider(opts);
         if (infos != null) {
-            final List<IdentityInfo> list = new ArrayList<>(infos.length);
-            for (final IdentityProvider identityProvider : infos) {
-                if (identityProvider instanceof IdentityInfo) {
-                    list.add((IdentityInfo) identityProvider);
-                }
-            }
-            return list.toArray(new IdentityInfo[list.size()]);
+            return Arrays.stream(infos).filter(info -> info instanceof IdentityInfo)
+                                       .map(info -> (IdentityInfo) info).toArray(IdentityInfo[]::new);
         }
         return null;
     }
@@ -535,10 +527,7 @@ public final class SftpFileSystemConfigBuilder extends FileSystemConfigBuilder
{
     public void setIdentities(final FileSystemOptions opts, final File... identityFiles)
throws FileSystemException {
         IdentityProvider[] info = null;
         if (identityFiles != null) {
-            info = new IdentityProvider[identityFiles.length];
-            for (int i = 0; i < identityFiles.length; i++) {
-                info[i] = new IdentityInfo(identityFiles[i]);
-            }
+            info = Arrays.stream(identityFiles).map(IdentityInfo::new).toArray(IdentityProvider[]::new);
         }
         this.setParam(opts, IDENTITIES, info);
     }


Mime
View raw message