PathToLockId: Refactor to use a FunctionalInterface Define a FunctionalInterface, and provide a static instance of the converter. This allows to call it directly instead of getting an instance injected. Inspired-by: David Ostrovsky <david@ostrovsky.org> Change-Id: I8d51c2989152672e3bbaae14c58f2973421c3732
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java index 1e75677..66ee924 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsLocksHandler.java
@@ -67,14 +67,10 @@ }; } - private final PathToLockId toLockId; private final LoadingCache<Project.NameKey, LfsProjectLocks> projectLocks; @Inject - LfsLocksHandler( - PathToLockId toLockId, - @Named(CACHE_NAME) LoadingCache<Project.NameKey, LfsProjectLocks> projectLocks) { - this.toLockId = toLockId; + LfsLocksHandler(@Named(CACHE_NAME) LoadingCache<Project.NameKey, LfsProjectLocks> projectLocks) { this.projectLocks = projectLocks; } @@ -122,8 +118,7 @@ LfsGetLocksResponse listLocksByPath(Project.NameKey project, String path) { log.atFine().log("Get lock for %s path in %s project", path, project); - String lockId = toLockId.apply(path); - return listLocksById(project, lockId); + return listLocksById(project, PathToLockId.CONVERTER.convert(path)); } LfsGetLocksResponse listLocksById(Project.NameKey project, String id) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java index 4526fa4..e8987f9 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/LfsProjectLocks.java
@@ -43,19 +43,13 @@ private static final FluentLogger log = FluentLogger.forEnclosingClass(); private final LfsGson gson; - private final PathToLockId toLockId; private final String project; private final Path locksPath; private final Cache<String, LfsLock> locks; @Inject - LfsProjectLocks( - LfsGson gson, - PathToLockId toLockId, - LfsLocksPathProvider locksPath, - @Assisted Project.NameKey project) { + LfsProjectLocks(LfsGson gson, LfsLocksPathProvider locksPath, @Assisted Project.NameKey project) { this.gson = gson; - this.toLockId = toLockId; this.project = project.get(); this.locksPath = Paths.get(locksPath.get(), this.project); this.locks = CacheBuilder.newBuilder().build(); @@ -94,7 +88,7 @@ LfsLock createLock(CurrentUser user, LfsCreateLockInput input) throws LfsException { log.atFine().log("Create lock for %s in project %s", input.path, project); - String lockId = toLockId.apply(input.path); + String lockId = PathToLockId.CONVERTER.convert(input.path); LfsLock lock = locks.getIfPresent(lockId); if (lock != null) { throw new LfsLockExistsException(lock);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java index d90ebb8..4449469 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java +++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/locks/PathToLockId.java
@@ -14,16 +14,22 @@ package com.googlesource.gerrit.plugins.lfs.locks; -import com.google.common.base.Function; -import com.google.common.hash.HashCode; +import static java.nio.charset.StandardCharsets.UTF_8; + import com.google.common.hash.Hashing; import com.google.common.io.BaseEncoding; -import java.nio.charset.StandardCharsets; -public class PathToLockId implements Function<String, String> { - @Override - public String apply(String path) { - HashCode hash = Hashing.sha256().hashString(path, StandardCharsets.UTF_8); - return BaseEncoding.base16().lowerCase().encode(hash.asBytes()); +public class PathToLockId { + private PathToLockId() {} + + @FunctionalInterface + public interface PathToLockIdInterface { + String convert(String path); } + + static final PathToLockIdInterface CONVERTER = + path -> + BaseEncoding.base16() + .lowerCase() + .encode(Hashing.sha256().hashString(path, UTF_8).asBytes()); }