From deaea3e4e60b67d5302f50b5d98886c6acfd0923 Mon Sep 17 00:00:00 2001 From: Ivan Bessonov Date: Wed, 13 Dec 2017 17:37:03 +0300 Subject: [PATCH] maven - usage of computeIfAbsent in MavenIndex --- .../idea/maven/indices/MavenIndex.java | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenIndex.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenIndex.java index 63c31967fe6d..9457a4ab8f1c 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenIndex.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/indices/MavenIndex.java @@ -496,12 +496,7 @@ public class MavenIndex { } private static Set getOrCreate(Map> map, String key) { - Set result = map.get(key); - if (result == null) { - result = new THashSet<>(); - map.put(key, result); - } - return result; + return map.computeIfAbsent(key, k -> new THashSet<>()); } private static void persist(Map map, PersistentHashMap persistentMap) throws IOException { @@ -601,29 +596,14 @@ public class MavenIndex { if (isBroken) return false; final String groupWithArtifactWithVersion = groupId + ":" + artifactId + ':' + version; - - Boolean res = myData.hasVersionCache.get(groupWithArtifactWithVersion); - if (res == null) { - String groupWithArtifact = groupWithArtifactWithVersion.substring(0, groupWithArtifactWithVersion.length() - version.length() - 1); - res = doIndexTask(() -> { - Set set = myData.groupWithArtifactToVersionMap.get(groupWithArtifact); - return set != null && set.contains(version); - }, false); - - myData.hasVersionCache.put(groupWithArtifactWithVersion, res); - } - - return res; + String groupWithArtifact = groupWithArtifactWithVersion.substring(0, groupWithArtifactWithVersion.length() - version.length() - 1); + return myData.hasVersionCache.computeIfAbsent(groupWithArtifactWithVersion, gav -> doIndexTask( + () -> notNullize(myData.groupWithArtifactToVersionMap.get(groupWithArtifact)).contains(version), + false)); } private boolean hasValue(final PersistentHashMap map, Map cache, final String value) { - Boolean res = cache.get(value); - if (res == null) { - res = doIndexTask(() -> map.tryEnumerate(value) != 0, false); - cache.put(value, res); - } - - return res; + return cache.computeIfAbsent(value, v -> doIndexTask(() -> map.tryEnumerate(v) != 0, false)); } public synchronized Set search(final Query query, final int maxResult) {