mirror of
https://github.com/Alfresco/SearchServices.git
synced 2026-09-16 18:12:56 +00:00
Merge branch 'fix/SEARCH_875' into 'master'
Fix/search 875 See merge request search_discovery/insightengine!222
This commit is contained in:
@@ -18,11 +18,18 @@
|
||||
*/
|
||||
package org.alfresco.solr.utils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public abstract class Utils
|
||||
{
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
/**
|
||||
* Returns the same input collection if that is not null, otherwise a new empty collection.
|
||||
* Provides a safe way for iterating over a returned collection (which could be null).
|
||||
@@ -53,4 +60,41 @@ public abstract class Utils
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Silently closes the given {@link Closeable} resource without raising any exception.
|
||||
* This utility method is specifically useful when we have to close a resource in a lamba statement: since the
|
||||
* close() method could throw an {@link IOException} the compiler requires an enclosing try / catch block which
|
||||
* makes the code less readable.
|
||||
*
|
||||
* <br/><br/>
|
||||
* <p>
|
||||
* <code>
|
||||
* try { if (resource != null) resource.close } catch (IOException exception) { ... }
|
||||
* </code>
|
||||
* </p>
|
||||
* <br/>
|
||||
*
|
||||
* In these contexts a call to this method reduces the amount of code needed:
|
||||
*
|
||||
* <br/><br/>
|
||||
* <p>
|
||||
* <code>
|
||||
* silentlyClose(resource);
|
||||
* </code>
|
||||
* </p>
|
||||
*
|
||||
* @param resource the {@link Closeable} resource we want to silently close.
|
||||
*/
|
||||
public static void silentyClose(Closeable resource)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (resource != null) resource.close();
|
||||
}
|
||||
catch(IOException ignore)
|
||||
{
|
||||
LOGGER.warn("Unable to properly close the resource instance {}. See the stacktrace below for further details.", resource, ignore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user