Skip to content

Commit 81a7d01

Browse files
committed
Use Java 8 functional interfaces
1 parent 1cd57d4 commit 81a7d01

2 files changed

Lines changed: 53 additions & 89 deletions

File tree

src/main/java/com/thebuzzmedia/exiftool/ExifToolBuilder.java

Lines changed: 52 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import java.io.File;
3131
import java.util.ArrayList;
3232
import java.util.List;
33+
import java.util.function.Supplier;
3334

3435
import static com.thebuzzmedia.exiftool.core.schedulers.SchedulerDuration.millis;
35-
import static com.thebuzzmedia.exiftool.process.executor.CommandExecutors.newExecutor;
3636

3737
/**
3838
* Builder for {@link ExifTool} instance.
@@ -134,19 +134,56 @@ public class ExifToolBuilder {
134134
private static final Logger log = LoggerFactory.getLogger(ExifToolBuilder.class);
135135

136136
/**
137-
* Function to get default path value.
137+
* Gets the default path value.
138+
* <p>
139+
* The path value is the absolute path to the ExifTool executable on the host system running
140+
* this class as defined by the {@code exiftool.path} system property.
141+
* <p>
142+
* This system property can be set on startup with {@code -Dexiftool.path=/path/to/exiftool}
143+
* or by calling {@link System#setProperty(String, String)} before
144+
* this class is loaded.
145+
* <p>
146+
* On Windows be sure to double-escape the path to the tool,
147+
* for example: {@code -Dexiftool.path=C:\\Tools\\exiftool.exe}.
148+
* <p>
149+
* If the property is not defined it will use {@code exiftool}.
138150
*/
139-
private static final PathFunction PATH = new PathFunction();
151+
private static final Supplier<String> PATH = () -> System.getProperty("exiftool.path", "exiftool");
140152

141153
/**
142-
* Function to get default cleanup interval.
154+
* Gets the default cleanup interval.
155+
* <p>
156+
* The cleanup interval is the interval (in milliseconds) of inactivity before the cleanup thread wakes
157+
* up and cleans up the daemon ExifTool process and the read/write streams
158+
* used to communicate with it when the {@code stay_open} feature is
159+
* used.
160+
* <p>
161+
* Every time a call to {@link ExifTool#getImageMeta} is processed, the timer
162+
* keeping track of cleanup is reset; more specifically, this class has to
163+
* experience no activity for this duration of time before the cleanup
164+
* process is fired up and cleans up the host OS process and the stream
165+
* resources.
166+
* <p>
167+
* Any subsequent calls to {@link ExifTool#getImageMeta} after a cleanup simply
168+
* re-initializes the resources.
169+
* <p>
170+
* This system property can be set on startup with {@code -Dexiftool.processCleanupDelay=600000}
171+
* or by calling {@link System#setProperty(String, String)} before
172+
* this class is loaded.
173+
* <p>
174+
* Setting this value to 0 disables the automatic cleanup thread completely
175+
* and the caller will need to manually clean up the external ExifTool
176+
* process and read/write streams by calling {@link ExifTool#close} method.
177+
* <p>
178+
* If the property is not defined it will default to {@code 600000} (10 minutes).
143179
*/
144-
private static final DelayFunction DELAY = new DelayFunction();
180+
private static final Supplier<Long> DELAY = () -> Long.getLong("exiftool.processCleanupDelay", 600000);
145181

146182
/**
147-
* Function to get default executor environment.
183+
* Gets the default executor for the created ExifTool instance.
184+
* The default executor is the result of {@link CommandExecutors#newExecutor()} method.
148185
*/
149-
private static final ExecutorFunction EXECUTOR = new ExecutorFunction();
186+
private static final Supplier<CommandExecutor> EXECUTOR = CommandExecutors::newExecutor;
150187

151188
/**
152189
* ExifTool path.
@@ -426,86 +463,12 @@ public ExifTool build() {
426463
* </ul>
427464
*
428465
* @param value First value.
429-
* @param factory Function used to get non-null value.
466+
* @param supplier Function used to get non-null value.
430467
* @param <T> Type of values.
431468
* @return Non null value.
432469
*/
433-
private static <T> T firstNonNull(T value, FactoryFunction<T> factory) {
434-
return value == null ? factory.apply() : value;
435-
}
436-
437-
/**
438-
* Interface to return values.
439-
* This interface should be used by builder to lazily create
440-
* default settings parameters.
441-
*
442-
* @param <T> Type of settings.
443-
*/
444-
private interface FactoryFunction<T> {
445-
T apply();
446-
}
447-
448-
/**
449-
* Return the absolute path to the ExifTool executable on the host system running
450-
* this class as defined by the {@code exiftool.path} system property.
451-
* <p>
452-
* This system property can be set on startup with {@code -Dexiftool.path=/path/to/exiftool}
453-
* or by calling {@link System#setProperty(String, String)} before
454-
* this class is loaded.
455-
* <p>
456-
* On Windows be sure to double-escape the path to the tool,
457-
* for example: {@code -Dexiftool.path=C:\\Tools\\exiftool.exe}.
458-
* <p>
459-
* Default value is {@code exiftool}.
460-
*/
461-
private static class PathFunction implements FactoryFunction<String> {
462-
@Override
463-
public String apply() {
464-
return System.getProperty("exiftool.path", "exiftool");
465-
}
466-
}
467-
468-
/**
469-
* Return the interval (in milliseconds) of inactivity before the cleanup thread wakes
470-
* up and cleans up the daemon ExifTool process and the read/write streams
471-
* used to communicate with it when the {@code stay_open} feature is
472-
* used.
473-
* <p>
474-
* Every time a call to {@link ExifTool#getImageMeta} is processed, the timer
475-
* keeping track of cleanup is reset; more specifically, this class has to
476-
* experience no activity for this duration of time before the cleanup
477-
* process is fired up and cleans up the host OS process and the stream
478-
* resources.
479-
* <p>
480-
* Any subsequent calls to {@link ExifTool#getImageMeta} after a cleanup simply
481-
* re-initializes the resources.
482-
* <p>
483-
* This system property can be set on startup with {@code -Dexiftool.processCleanupDelay=600000}
484-
* or by calling {@link System#setProperty(String, String)} before
485-
* this class is loaded.
486-
* <p>
487-
* Setting this value to 0 disables the automatic cleanup thread completely
488-
* and the caller will need to manually clean up the external ExifTool
489-
* process and read/write streams by calling {@link ExifTool#close} method.
490-
* <p>
491-
* Default value is {@code 600000} (10 minutes).
492-
*/
493-
private static class DelayFunction implements FactoryFunction<Long> {
494-
@Override
495-
public Long apply() {
496-
return Long.getLong("exiftool.processCleanupDelay", 600000);
497-
}
498-
}
499-
500-
/**
501-
* Returns the default executor for the created ExifTool instance.
502-
* Default executor is the result of {@link CommandExecutors#newExecutor()} method.
503-
*/
504-
private static class ExecutorFunction implements FactoryFunction<CommandExecutor> {
505-
@Override
506-
public CommandExecutor apply() {
507-
return newExecutor();
508-
}
470+
private static <T> T firstNonNull(T value, Supplier<T> supplier) {
471+
return value == null ? supplier.get() : value;
509472
}
510473

511474
/**
@@ -516,15 +479,15 @@ public CommandExecutor apply() {
516479
* <li>If {@code delay} is greater than zero, then an instance of {@link DefaultScheduler} will be returned.</li>
517480
* </ul>
518481
*/
519-
private static class SchedulerFunction implements FactoryFunction<Scheduler> {
482+
private static class SchedulerFunction implements Supplier<Scheduler> {
520483
private final Long delay;
521484

522485
public SchedulerFunction(Long delay) {
523486
this.delay = delay;
524487
}
525488

526489
@Override
527-
public Scheduler apply() {
490+
public Scheduler get() {
528491
// Otherwise, this is the StayOpen strategy.
529492
// We have to look up the delay between automatic clean and create
530493
// the scheduler.
@@ -545,7 +508,7 @@ public Scheduler apply() {
545508
* a task to clean resources used by this strategy. This task will run automatically after a specified
546509
* delay.
547510
*/
548-
private static class StrategyFunction implements FactoryFunction<ExecutionStrategy> {
511+
private static class StrategyFunction implements Supplier<ExecutionStrategy> {
549512
private final Boolean stayOpen;
550513

551514
private final Long delay;
@@ -562,12 +525,12 @@ public StrategyFunction(Boolean stayOpen, Long delay, Scheduler scheduler, int p
562525
}
563526

564527
@Override
565-
public ExecutionStrategy apply() {
528+
public ExecutionStrategy get() {
566529
// First, try the pool strategy.
567530
if (poolSize > 0) {
568531
List<ExecutionStrategy> strategies = new ArrayList<>(poolSize);
569532
for (int i = 0; i < poolSize; i++) {
570-
Scheduler scheduler = new SchedulerFunction(delay).apply();
533+
Scheduler scheduler = new SchedulerFunction(delay).get();
571534
StayOpenStrategy strategy = new StayOpenStrategy(scheduler);
572535
strategies.add(strategy);
573536
}

src/main/java/com/thebuzzmedia/exiftool/core/strategies/PoolStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ private void processPool(ExecutionStrategyFunction function) throws Exception {
171171
}
172172
}
173173

174+
@FunctionalInterface
174175
private interface ExecutionStrategyFunction {
175176
void apply(ExecutionStrategy strategy, int i) throws Exception;
176177
}

0 commit comments

Comments
 (0)