Skip to content

Commit ba49043

Browse files
committed
Better names for JvmLocalCache, and initial values were a bad idea.
1 parent cfc8291 commit ba49043

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JvmLocalCache.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.Map;
2323
import java.util.Objects;
2424

25-
import javax.annotation.Nullable;
26-
2725
import org.gradle.api.GradleException;
2826
import org.gradle.api.Task;
2927

@@ -36,24 +34,20 @@ private static GradleException cacheIsStale() {
3634
"To make this workaround obsolete, please upvote https://github.com/diffplug/spotless/issues/987");
3735
}
3836

39-
interface CacheKey<T> {
37+
interface LiveCache<T> {
4038
T get();
4139

4240
void set(T value);
4341
}
4442

45-
static <T> CacheKey<T> createCacheKey(Task task, String keyName, @Nullable T initialValue) {
46-
CacheKeyImpl key = new CacheKeyImpl<T>(new InternalCacheKey(task.getProject().getProjectDir(), task.getPath(), keyName));
47-
if (initialValue != null) {
48-
key.set(initialValue);
49-
}
50-
return key;
43+
static <T> LiveCache<T> createLive(Task task, String propertyName) {
44+
return new LiveCacheKeyImpl<T>(new InternalCacheKey(task.getProject().getProjectDir(), task.getPath(), propertyName));
5145
}
5246

53-
static class CacheKeyImpl<T> implements CacheKey<T>, Serializable {
47+
static class LiveCacheKeyImpl<T> implements LiveCache<T>, Serializable {
5448
InternalCacheKey internalKey;
5549

56-
CacheKeyImpl(InternalCacheKey internalKey) {
50+
LiveCacheKeyImpl(InternalCacheKey internalKey) {
5751
this.internalKey = internalKey;
5852
}
5953

@@ -79,12 +73,12 @@ public T get() {
7973
private static class InternalCacheKey implements Serializable {
8074
private File projectDir;
8175
private String taskPath;
82-
private String keyName;
76+
private String propertyName;
8377

8478
InternalCacheKey(File projectDir, String taskPath, String keyName) {
8579
this.projectDir = projectDir;
8680
this.taskPath = taskPath;
87-
this.keyName = keyName;
81+
this.propertyName = keyName;
8882
}
8983

9084
@Override
@@ -94,12 +88,12 @@ public boolean equals(Object o) {
9488
if (o == null || getClass() != o.getClass())
9589
return false;
9690
InternalCacheKey that = (InternalCacheKey) o;
97-
return projectDir.equals(that.projectDir) && taskPath.equals(that.taskPath) && keyName.equals(that.keyName);
91+
return projectDir.equals(that.projectDir) && taskPath.equals(that.taskPath) && propertyName.equals(that.propertyName);
9892
}
9993

10094
@Override
10195
public int hashCode() {
102-
return Objects.hash(projectDir, taskPath, keyName);
96+
return Objects.hash(projectDir, taskPath, propertyName);
10397
}
10498
}
10599
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.gradle.api.tasks.PathSensitivity;
3939
import org.gradle.work.Incremental;
4040

41-
import com.diffplug.gradle.spotless.JvmLocalCache.CacheKey;
41+
import com.diffplug.gradle.spotless.JvmLocalCache.LiveCache;
4242
import com.diffplug.spotless.FormatExceptionPolicy;
4343
import com.diffplug.spotless.FormatExceptionPolicyStrict;
4444
import com.diffplug.spotless.Formatter;
@@ -49,12 +49,8 @@ public abstract class SpotlessTask extends DefaultTask {
4949
@Internal
5050
abstract Property<SpotlessTaskService> getTaskService();
5151

52-
protected <T> CacheKey<T> createCacheKey(String keyName) {
53-
return createCacheKey(keyName, null);
54-
}
55-
56-
protected <T> CacheKey<T> createCacheKey(String keyName, @Nullable T initialValue) {
57-
return JvmLocalCache.createCacheKey(this, keyName, initialValue);
52+
protected <T> LiveCache<T> createLive(String keyName) {
53+
return JvmLocalCache.createLive(this, keyName);
5854
}
5955

6056
// set by SpotlessExtension, but possibly overridden by FormatExtension
@@ -69,7 +65,7 @@ public void setEncoding(String encoding) {
6965
this.encoding = Objects.requireNonNull(encoding);
7066
}
7167

72-
protected final CacheKey<LineEnding.Policy> lineEndingsPolicy = createCacheKey("lineEndingsPolicy");
68+
protected final LiveCache<LineEnding.Policy> lineEndingsPolicy = createLive("lineEndingsPolicy");
7369

7470
@Input
7571
public LineEnding.Policy getLineEndingsPolicy() {
@@ -162,7 +158,10 @@ public File getOutputDirectory() {
162158
return outputDirectory;
163159
}
164160

165-
protected final CacheKey<List<FormatterStep>> steps = createCacheKey("steps", new ArrayList<FormatterStep>());
161+
protected final LiveCache<List<FormatterStep>> steps = createLive("steps");
162+
{
163+
steps.set(new ArrayList<FormatterStep>());
164+
}
166165

167166
@Input
168167
public List<FormatterStep> getSteps() {

0 commit comments

Comments
 (0)