Skip to content

Commit

Permalink
Make API more direct (i.e. no functional interfaces)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolai Parlog committed Jun 16, 2020
1 parent 901aa9d commit 0c72122
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import org.junit.jupiter.api.extension.AfterAllCallback;
Expand Down Expand Up @@ -60,19 +57,19 @@ abstract class AbstractEntryBasedExtension<K, V>
protected abstract Map<K, V> entriesToSet(ExtensionContext context);

/**
* @return How to clear an entry based on its key.
* Clear an entry based on its key.
*/
protected abstract Consumer<? super K> clearer();
protected abstract void clearEntry(K key);

/**
* @return How to get an entry value based on its key.
* Get an entry value based on its key.
*/
protected abstract Function<? super K, ? extends V> getter();
protected abstract V getValue(K key);

/**
* @return How to set an entry value based on its key.
* Put a key/value pair as an entry.
*/
protected abstract BiConsumer<? super K, ? super V> setter();
protected abstract void put(K key, V value);

@Override
public void beforeAll(ExtensionContext context) throws Exception {
Expand Down Expand Up @@ -123,11 +120,11 @@ private void storeOriginalEntries(ExtensionContext context, Collection<K> entrie
}

private void clearEntries(Collection<K> entriesToClear) {
entriesToClear.forEach(clearer());
entriesToClear.forEach(this::clearEntry);
}

private void setEntries(Map<K, V> entriesToSet) {
entriesToSet.forEach(setter());
entriesToSet.forEach(this::put);
}

@Override
Expand All @@ -152,7 +149,7 @@ private class EntriesBackup {

public EntriesBackup(Collection<K> entriesToClear, Collection<K> entriesToSet) {
Stream.concat(entriesToClear.stream(), entriesToSet.stream()).forEach(entry -> {
V backup = AbstractEntryBasedExtension.this.getter().apply(entry);
V backup = AbstractEntryBasedExtension.this.getValue(entry);
if (backup == null)
this.entriesToClear.add(entry);
else
Expand All @@ -161,8 +158,8 @@ public EntriesBackup(Collection<K> entriesToClear, Collection<K> entriesToSet) {
}

public void restoreBackup() {
entriesToClear.forEach(AbstractEntryBasedExtension.this.clearer());
entriesToSet.forEach(AbstractEntryBasedExtension.this.setter());
entriesToClear.forEach(AbstractEntryBasedExtension.this::clearEntry);
entriesToSet.forEach(AbstractEntryBasedExtension.this::put);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import org.junit.jupiter.api.extension.ExtensionContext;

Expand Down Expand Up @@ -58,18 +55,19 @@ private void reportWarning(ExtensionContext context) {
context.publishReportEntry(WARNING_KEY, WARNING_VALUE);
}

protected Consumer<String> clearer() {
return EnvironmentVariableUtils::clear;
@Override
protected void clearEntry(String key) {
EnvironmentVariableUtils.clear(key);
}

@Override
protected Function<String, String> getter() {
return System::getenv;
protected String getValue(String key) {
return System.getenv(key);
}

@Override
protected BiConsumer<String, String> setter() {
return EnvironmentVariableUtils::set;
protected void put(String key, String value) {
EnvironmentVariableUtils.set(key, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import org.junit.jupiter.api.extension.ExtensionContext;

Expand Down Expand Up @@ -44,18 +41,18 @@ protected Map<String, String> entriesToSet(ExtensionContext context) {
}

@Override
protected Consumer<String> clearer() {
return System::clearProperty;
protected void clearEntry(String key) {
System.clearProperty(key);
}

@Override
protected Function<String, String> getter() {
return System::getProperty;
protected String getValue(String key) {
return System.getProperty(key);
}

@Override
protected BiConsumer<String, String> setter() {
return System::setProperty;
protected void put(String key, String value) {
System.setProperty(key, value);
}

}

0 comments on commit 0c72122

Please sign in to comment.