mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 23:45:44 +01:00
Clean up generics
This commit is contained in:
@@ -2,7 +2,7 @@ package sonia.scm.repository.util;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class CloseableWrapper<T> implements AutoCloseable {
|
public class CloseableWrapper<T extends AutoCloseable> implements AutoCloseable {
|
||||||
|
|
||||||
private final T wrapped;
|
private final T wrapped;
|
||||||
private final Consumer<T> cleanup;
|
private final Consumer<T> cleanup;
|
||||||
|
|||||||
@@ -9,28 +9,31 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
public abstract class SimpleWorkdirFactory<T extends AutoCloseable, C> {
|
public abstract class SimpleWorkdirFactory<R extends AutoCloseable, C> implements WorkdirFactory<R, C> {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SimpleWorkdirFactory.class);
|
private static final Logger logger = LoggerFactory.getLogger(SimpleWorkdirFactory.class);
|
||||||
|
|
||||||
private final File poolDirectory;
|
private final File poolDirectory;
|
||||||
|
|
||||||
private final CloneProvider<T, C> cloneProvider;
|
private final CloneProvider<R, C> cloneProvider;
|
||||||
|
|
||||||
public SimpleWorkdirFactory(CloneProvider<T, C> cloneProvider) {
|
public SimpleWorkdirFactory(CloneProvider<R, C> cloneProvider) {
|
||||||
this(new File(System.getProperty("java.io.tmpdir"), "scmm-work-pool"), cloneProvider);
|
this(new File(System.getProperty("java.io.tmpdir"), "scmm-work-pool"), cloneProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleWorkdirFactory(File poolDirectory, CloneProvider<T, C> cloneProvider) {
|
public SimpleWorkdirFactory(File poolDirectory, CloneProvider<R, C> cloneProvider) {
|
||||||
this.poolDirectory = poolDirectory;
|
this.poolDirectory = poolDirectory;
|
||||||
this.cloneProvider = cloneProvider;
|
this.cloneProvider = cloneProvider;
|
||||||
poolDirectory.mkdirs();
|
if (!poolDirectory.exists() && !poolDirectory.mkdirs()) {
|
||||||
|
throw new IllegalStateException("could not create pool directory " + poolDirectory);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public WorkingCopy<T> createWorkingCopy(C context) {
|
@Override
|
||||||
|
public WorkingCopy<R> createWorkingCopy(C context) {
|
||||||
try {
|
try {
|
||||||
File directory = createNewWorkdir();
|
File directory = createNewWorkdir();
|
||||||
T clone = cloneProvider.cloneRepository(context, directory);
|
R clone = cloneProvider.cloneRepository(context, directory);
|
||||||
return new WorkingCopy<>(clone, this::close, directory);
|
return new WorkingCopy<>(clone, this::close, directory);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new InternalRepositoryException(getRepository(context), "could not create temporary directory for clone of repository", e);
|
throw new InternalRepositoryException(getRepository(context), "could not create temporary directory for clone of repository", e);
|
||||||
@@ -43,7 +46,7 @@ public abstract class SimpleWorkdirFactory<T extends AutoCloseable, C> {
|
|||||||
return Files.createTempDirectory(poolDirectory.toPath(),"workdir").toFile();
|
return Files.createTempDirectory(poolDirectory.toPath(),"workdir").toFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void close(T repository) {
|
private void close(R repository) {
|
||||||
try {
|
try {
|
||||||
repository.close();
|
repository.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -51,7 +54,7 @@ public abstract class SimpleWorkdirFactory<T extends AutoCloseable, C> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface CloneProvider<T, C> {
|
public interface CloneProvider<R, C> {
|
||||||
T cloneRepository(C context, File target) throws IOException;
|
R cloneRepository(C context, File target) throws IOException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package sonia.scm.repository.util;
|
package sonia.scm.repository.util;
|
||||||
|
|
||||||
public interface WorkdirFactory<T, C> {
|
public interface WorkdirFactory<R extends AutoCloseable, C> {
|
||||||
WorkingCopy<T> createWorkingCopy(C gitContext);
|
WorkingCopy<R> createWorkingCopy(C context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class WorkingCopy<T> extends CloseableWrapper<T> {
|
public class WorkingCopy<R extends AutoCloseable> extends CloseableWrapper<R> {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(WorkingCopy.class);
|
private static final Logger LOG = LoggerFactory.getLogger(WorkingCopy.class);
|
||||||
|
|
||||||
private final File directory;
|
private final File directory;
|
||||||
|
|
||||||
public WorkingCopy(T wrapped, Consumer<T> cleanup, File directory) {
|
public WorkingCopy(R wrappedRepository, Consumer<R> cleanup, File directory) {
|
||||||
super(wrapped, cleanup);
|
super(wrappedRepository, cleanup);
|
||||||
this.directory = directory;
|
this.directory = directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,19 +12,20 @@ public class CloseableWrapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldExecuteGivenMethodAtClose() {
|
public void shouldExecuteGivenMethodAtClose() {
|
||||||
Consumer<String> wrapped = new Consumer<String>() {
|
Consumer<AutoCloseable> wrapped = new Consumer<AutoCloseable>() {
|
||||||
// no this cannot be replaced with a lambda because otherwise we could not use Mockito#spy
|
// no this cannot be replaced with a lambda because otherwise we could not use Mockito#spy
|
||||||
@Override
|
@Override
|
||||||
public void accept(String s) {
|
public void accept(AutoCloseable s) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Consumer<String> closer = spy(wrapped);
|
Consumer<AutoCloseable> closer = spy(wrapped);
|
||||||
|
|
||||||
try (CloseableWrapper<String> wrapper = new CloseableWrapper<>("test", closer)) {
|
AutoCloseable autoCloseable = () -> {};
|
||||||
|
try (CloseableWrapper<AutoCloseable> wrapper = new CloseableWrapper<>(autoCloseable, closer)) {
|
||||||
// nothing to do here
|
// nothing to do here
|
||||||
}
|
}
|
||||||
|
|
||||||
verify(closer).accept("test");
|
verify(closer).accept(autoCloseable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user