Apply Some Java 8 Code Changes

* Make most functional interfaces as `@FunctionalInterface`
* Convert some abstract classes to `@FunctionalInterface` with `default` methods
* Apply Lambda style implementation in some places
* Remove `Function` in favor of similar in Java 8

*  Remove redundant code from `DefaultAmqpHeaderMapper` since we are already on Spring AMQP-2.0
* Add several ctors to the `ExpressionEvaluatingMessageListProcessor`
* Populate explicit `Boolean.class` `expectedType` from the `ExpressionEvaluatingReleaseStrategy`
This commit is contained in:
Artem Bilan
2016-08-26 14:52:23 -04:00
committed by Gary Russell
parent 89e0d134f5
commit 370be4853d
58 changed files with 208 additions and 270 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import org.springframework.messaging.Message;
*
* @author Mark Fisher
*/
@FunctionalInterface
public interface FileNameGenerator {
String generateFileName(Message<?> message);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import java.util.List;
*
* @since 1.0.0
*/
@FunctionalInterface
public interface FileListFilter<F> {
/**

View File

@@ -29,6 +29,7 @@ import org.springframework.integration.file.remote.session.Session;
* @since 4.1
*
*/
@FunctionalInterface
public interface ClientCallback<C, T> {
/**

View File

@@ -22,15 +22,17 @@ package org.springframework.integration.file.remote;
* access to lower level methods where no result is returned.
*
* @author Gary Russell
* @author Artem Bilan
*
* @param <C> The type of the underlying client object.
* @since 4.1
*
*/
public abstract class ClientCallbackWithoutResult<C> implements ClientCallback<C, Object> {
@FunctionalInterface
public interface ClientCallbackWithoutResult<C> extends ClientCallback<C, Object> {
@Override
public Object doWithClient(C client) {
default Object doWithClient(C client) {
doWithClientWithoutResult(client);
return null;
}
@@ -43,6 +45,6 @@ public abstract class ClientCallbackWithoutResult<C> implements ClientCallback<C
* operations.
* @param client The client.
*/
protected abstract void doWithClientWithoutResult(C client);
void doWithClientWithoutResult(C client);
}

View File

@@ -26,13 +26,13 @@ import java.io.InputStream;
* @since 3.0
*
*/
@FunctionalInterface
public interface InputStreamCallback {
/**
* Called with the InputStream for the remote file. The caller will
* take care of closing the stream and finalizing the file retrieval operation after
* this method exits.
*
* @param stream The InputStream.
* @throws IOException Any IOException.
*/

View File

@@ -54,6 +54,7 @@ import org.springframework.util.StringUtils;
* @author Oleg Zhurakousky
* @author David Turanski
* @author Gary Russell
* @author Artem Bilan
* @since 3.0
*
*/
@@ -139,7 +140,8 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
*/
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {
Assert.notNull(remoteDirectoryExpression, "remoteDirectoryExpression must not be null");
this.directoryExpressionProcessor = new ExpressionEvaluatingMessageProcessor<String>(remoteDirectoryExpression, String.class);
this.directoryExpressionProcessor =
new ExpressionEvaluatingMessageProcessor<>(remoteDirectoryExpression, String.class);
}
/**
@@ -150,7 +152,8 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
*/
public void setTemporaryRemoteDirectoryExpression(Expression temporaryRemoteDirectoryExpression) {
Assert.notNull(temporaryRemoteDirectoryExpression, "temporaryRemoteDirectoryExpression must not be null");
this.temporaryDirectoryExpressionProcessor = new ExpressionEvaluatingMessageProcessor<String>(temporaryRemoteDirectoryExpression, String.class);
this.temporaryDirectoryExpressionProcessor =
new ExpressionEvaluatingMessageProcessor<>(temporaryRemoteDirectoryExpression, String.class);
}
/**
@@ -161,7 +164,7 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
*/
public void setFileNameExpression(Expression fileNameExpression) {
Assert.notNull(fileNameExpression, "fileNameExpression must not be null");
this.fileNameProcessor = new ExpressionEvaluatingMessageProcessor<String>(fileNameExpression, String.class);
this.fileNameProcessor = new ExpressionEvaluatingMessageProcessor<>(fileNameExpression, String.class);
}
/**
@@ -243,10 +246,12 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
}
}
if (this.autoCreateDirectory) {
Assert.hasText(this.remoteFileSeparator, "'remoteFileSeparator' must not be empty when 'autoCreateDirectory' is set to 'true'");
Assert.hasText(this.remoteFileSeparator,
"'remoteFileSeparator' must not be empty when 'autoCreateDirectory' is set to 'true'");
}
if (this.hasExplicitlySetSuffix && !this.useTemporaryFileName) {
this.logger.warn("Since 'use-temporary-file-name' is set to 'false' the value of 'temporary-file-suffix' has no effect");
this.logger.warn("Since 'use-temporary-file-name' is set to 'false' " +
"the value of 'temporary-file-suffix' has no effect");
}
}
@@ -280,46 +285,42 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
final StreamHolder inputStreamHolder = this.payloadToInputStream(message);
if (inputStreamHolder != null) {
try {
return this.execute(new SessionCallback<F, String>() {
@Override
public String doInSession(Session<F> session) throws IOException {
String fileName = inputStreamHolder.getName();
try {
String remoteDirectory = RemoteFileTemplate.this.directoryExpressionProcessor
return this.execute(session -> {
String fileName = inputStreamHolder.getName();
try {
String remoteDirectory = RemoteFileTemplate.this.directoryExpressionProcessor
.processMessage(message);
remoteDirectory = RemoteFileTemplate.this.normalizeDirectoryPath(remoteDirectory);
if (StringUtils.hasText(subDirectory)) {
if (subDirectory.startsWith(RemoteFileTemplate.this.remoteFileSeparator)) {
remoteDirectory += subDirectory.substring(1);
}
else {
remoteDirectory += RemoteFileTemplate.this.normalizeDirectoryPath(subDirectory);
}
}
String temporaryRemoteDirectory = remoteDirectory;
if (RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor != null) {
temporaryRemoteDirectory = RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor
.processMessage(message);
remoteDirectory = RemoteFileTemplate.this.normalizeDirectoryPath(remoteDirectory);
if (StringUtils.hasText(subDirectory)) {
if (subDirectory.startsWith(RemoteFileTemplate.this.remoteFileSeparator)) {
remoteDirectory += subDirectory.substring(1);
}
else {
remoteDirectory += RemoteFileTemplate.this.normalizeDirectoryPath(subDirectory);
}
}
String temporaryRemoteDirectory = remoteDirectory;
if (RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor != null) {
temporaryRemoteDirectory = RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor
.processMessage(message);
}
fileName = RemoteFileTemplate.this.fileNameGenerator.generateFileName(message);
RemoteFileTemplate.this.sendFileToRemoteDirectory(inputStreamHolder.getStream(),
temporaryRemoteDirectory, remoteDirectory, fileName, session, mode);
return remoteDirectory + fileName;
}
catch (FileNotFoundException e) {
throw new MessageDeliveryException(message, "File [" + inputStreamHolder.getName()
+ "] not found in local working directory; it was moved or deleted unexpectedly.", e);
}
catch (IOException e) {
throw new MessageDeliveryException(message, "Failed to transfer file ["
+ inputStreamHolder.getName() + " -> " + fileName
+ "] from local directory to remote directory.", e);
}
catch (Exception e) {
throw new MessageDeliveryException(message, "Error handling message for file ["
+ inputStreamHolder.getName() + " -> " + fileName + "]", e);
}
fileName = RemoteFileTemplate.this.fileNameGenerator.generateFileName(message);
RemoteFileTemplate.this.sendFileToRemoteDirectory(inputStreamHolder.getStream(),
temporaryRemoteDirectory, remoteDirectory, fileName, session, mode);
return remoteDirectory + fileName;
}
catch (FileNotFoundException e) {
throw new MessageDeliveryException(message, "File [" + inputStreamHolder.getName()
+ "] not found in local working directory; it was moved or deleted unexpectedly.", e);
}
catch (IOException e) {
throw new MessageDeliveryException(message, "Failed to transfer file ["
+ inputStreamHolder.getName() + " -> " + fileName
+ "] from local directory to remote directory.", e);
}
catch (Exception e) {
throw new MessageDeliveryException(message, "Error handling message for file ["
+ inputStreamHolder.getName() + " -> " + fileName + "]", e);
}
});
}
@@ -342,24 +343,12 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
@Override
public boolean exists(final String path) {
return this.execute(new SessionCallback<F, Boolean>() {
@Override
public Boolean doInSession(Session<F> session) throws IOException {
return session.exists(path);
}
});
return execute(session -> session.exists(path));
}
@Override
public boolean remove(final String path) {
return this.execute(new SessionCallback<F, Boolean>() {
@Override
public Boolean doInSession(Session<F> session) throws IOException {
return session.remove(path);
}
});
return execute(session -> session.remove(path));
}
@Override
@@ -367,10 +356,7 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
Assert.hasText(fromPath, "Old filename cannot be null or empty");
Assert.hasText(toPath, "New filename cannot be null or empty");
this.execute(new SessionCallbackWithoutResult<F>() {
@Override
public void doInSessionWithoutResult(Session<F> session) throws IOException {
this.execute((SessionCallbackWithoutResult<F>) session -> {
int lastSeparator = toPath.lastIndexOf(RemoteFileTemplate.this.remoteFileSeparator);
if (lastSeparator > 0) {
String remoteFileDirectory = toPath.substring(0, lastSeparator + 1);
@@ -378,7 +364,6 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
RemoteFileTemplate.this.remoteFileSeparator, RemoteFileTemplate.this.logger);
}
session.rename(fromPath, toPath);
}
});
}
@@ -395,29 +380,18 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
@Override
public boolean get(final String remotePath, final InputStreamCallback callback) {
Assert.notNull(remotePath, "'remotePath' cannot be null");
return this.execute(new SessionCallback<F, Boolean>() {
@Override
public Boolean doInSession(Session<F> session) throws IOException {
InputStream inputStream = session.readRaw(remotePath);
callback.doWithInputStream(inputStream);
inputStream.close();
return session.finalizeRaw();
}
return this.execute(session -> {
InputStream inputStream = session.readRaw(remotePath);
callback.doWithInputStream(inputStream);
inputStream.close();
return session.finalizeRaw();
});
}
@Override
public F[] list(final String path) {
return this.execute(new SessionCallback<F, F[]>() {
@Override
public F[] doInSession(Session<F> session) throws IOException {
return session.list(path);
}
});
public F[] list(String path) {
return execute(session -> session.list(path));
}
@Override
@@ -425,7 +399,6 @@ public class RemoteFileTemplate<F> implements RemoteFileOperations<F>, Initializ
return this.sessionFactory.getSession();
}
@SuppressWarnings("rawtypes")
@Override
public <T> T execute(SessionCallback<F, T> callback) {
Session<F> session = null;

View File

@@ -28,6 +28,7 @@ import org.springframework.integration.file.remote.session.Session;
* @since 3.0
*
*/
@FunctionalInterface
public interface SessionCallback<F, T> {
/**

View File

@@ -25,13 +25,15 @@ import org.springframework.integration.file.remote.session.Session;
* no result is returned.
*
* @author Gary Russell
* @author Artem Bilan
* @since 3.0
*
*/
public abstract class SessionCallbackWithoutResult<F> implements SessionCallback<F, Object> {
@FunctionalInterface
public interface SessionCallbackWithoutResult<F> extends SessionCallback<F, Object> {
@Override
public Object doInSession(Session<F> session) throws IOException {
default Object doInSession(Session<F> session) throws IOException {
doInSessionWithoutResult(session);
return null;
}
@@ -40,10 +42,9 @@ public abstract class SessionCallbackWithoutResult<F> implements SessionCallback
* Called within the context of a session.
* Perform some operation(s) on the session. The caller will take
* care of closing the session after this method exits.
*
* @param session The session.
* @throws IOException Any IOException.
*/
protected abstract void doInSessionWithoutResult(Session<F> session) throws IOException;
void doInSessionWithoutResult(Session<F> session) throws IOException;
}

View File

@@ -62,7 +62,7 @@ public class FileTransferringMessageHandler<F> extends AbstractMessageHandler {
/**
* @param autoCreateDirectory true to automatically create the direcotory.
* @param autoCreateDirectory true to automatically create the directory.
* @see RemoteFileTemplate#setAutoCreateDirectory(boolean)
*/
public void setAutoCreateDirectory(boolean autoCreateDirectory) {

View File

@@ -22,6 +22,7 @@ package org.springframework.integration.file.remote.session;
* @author Mark Fisher
* @since 2.0
*/
@FunctionalInterface
public interface SessionFactory<F> {
Session<F> getSession();

View File

@@ -24,6 +24,7 @@ package org.springframework.integration.file.remote.session;
* @since 4.2
*
*/
@FunctionalInterface
public interface SessionFactoryLocator<F> {
/**