From a642779aa0674bbdce8c68de14931f004ac30b14 Mon Sep 17 00:00:00 2001 From: John DeRegnaucourt Date: Sat, 4 May 2024 14:24:49 -0400 Subject: [PATCH] Added comments indicating which methods are immutable/mutable --- .../java/com/cedarsoftware/util/LRUCache.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/cedarsoftware/util/LRUCache.java b/src/main/java/com/cedarsoftware/util/LRUCache.java index 7009e502..d1ae6248 100644 --- a/src/main/java/com/cedarsoftware/util/LRUCache.java +++ b/src/main/java/com/cedarsoftware/util/LRUCache.java @@ -63,9 +63,15 @@ protected boolean removeEldestEntry(Map.Entry eldest) { public Set keySet() { return readOperation(() -> new Set() { + // Immutable APIs public int size() { return readOperation(cache::size); } public boolean isEmpty() { return readOperation(cache::isEmpty); } public boolean contains(Object o) { return readOperation(() -> cache.containsKey(o)); } + public boolean containsAll(Collection c) { return readOperation(() -> cache.keySet().containsAll(c)); } + public Object[] toArray() { return readOperation(() -> cache.keySet().toArray()); } + public T[] toArray(T[] a) { return readOperation(() -> cache.keySet().toArray(a)); } + + // Mutable APIs public Iterator iterator() { return new Iterator() { private final Iterator it = cache.keySet().iterator(); @@ -76,7 +82,7 @@ public Iterator iterator() { public void remove() { writeOperation(() -> { if (current == NO_ENTRY) { - throw new IllegalStateException("Next not called or already removed"); + throw new IllegalStateException("Next not called or key already removed"); } it.remove(); // Remove from the underlying map current = (K)NO_ENTRY; @@ -85,11 +91,8 @@ public void remove() { } }; } - public Object[] toArray() { return readOperation(() -> cache.keySet().toArray()); } - public T[] toArray(T[] a) { return readOperation(() -> cache.keySet().toArray(a)); } public boolean add(K k) { throw new UnsupportedOperationException("add() not supported on .keySet() of a Map"); } public boolean remove(Object o) { return writeOperation(() -> cache.remove(o) != null); } - public boolean containsAll(Collection c) { return readOperation(() -> cache.keySet().containsAll(c)); } public boolean addAll(Collection c) { throw new UnsupportedOperationException("addAll() not supported on .keySet() of a Map"); } public boolean retainAll(Collection c) { return writeOperation(() -> cache.keySet().retainAll(c)); } public boolean removeAll(Collection c) { return writeOperation(() -> cache.keySet().removeAll(c)); } @@ -99,9 +102,15 @@ public void remove() { public Collection values() { return readOperation(() -> new Collection() { + // Immutable APIs public int size() { return readOperation(cache::size); } public boolean isEmpty() { return readOperation(cache::isEmpty); } public boolean contains(Object o) { return readOperation(() -> cache.containsValue(o)); } + public boolean containsAll(Collection c) { return readOperation(() -> cache.values().containsAll(c)); } + public Object[] toArray() { return readOperation(() -> cache.values().toArray()); } + public T[] toArray(T[] a) { return readOperation(() -> cache.values().toArray(a)); } + + // Mutable APIs public Iterator iterator() { return new Iterator() { private final Iterator it = cache.values().iterator(); @@ -112,7 +121,7 @@ public Iterator iterator() { public void remove() { writeOperation(() -> { if (current == NO_ENTRY) { - throw new IllegalStateException("Next not called or already removed"); + throw new IllegalStateException("Next not called or entry already removed"); } it.remove(); // Remove from the underlying map current = (V)NO_ENTRY; @@ -121,11 +130,8 @@ public void remove() { } }; } - public Object[] toArray() { return readOperation(() -> cache.values().toArray()); } - public T[] toArray(T[] a) { return readOperation(() -> cache.values().toArray(a)); } public boolean add(V value) { throw new UnsupportedOperationException("add() not supported on values() of a Map"); } public boolean remove(Object o) { return writeOperation(() -> cache.values().remove(o)); } - public boolean containsAll(Collection c) { return readOperation(() -> cache.values().containsAll(c)); } public boolean addAll(Collection c) { throw new UnsupportedOperationException("addAll() not supported on values() of a Map"); } public boolean removeAll(Collection c) { return writeOperation(() -> cache.values().removeAll(c)); } public boolean retainAll(Collection c) { return writeOperation(() -> cache.values().retainAll(c)); } @@ -135,9 +141,15 @@ public void remove() { public Set> entrySet() { return readOperation(() -> new Set>() { + // Immutable APIs public int size() { return readOperation(cache::size); } public boolean isEmpty() { return readOperation(cache::isEmpty); } public boolean contains(Object o) { return readOperation(() -> cache.entrySet().contains(o)); } + public boolean containsAll(Collection c) { return readOperation(() -> cache.entrySet().containsAll(c)); } + public Object[] toArray() { return readOperation(() -> cache.entrySet().toArray()); } + public T[] toArray(T[] a) { return readOperation(() -> cache.entrySet().toArray(a)); } + + // Mutable APIs public Iterator> iterator() { return new Iterator>() { private final Iterator> it = cache.entrySet().iterator(); @@ -148,7 +160,7 @@ public Iterator> iterator() { public void remove() { writeOperation(() -> { if (current == NO_ENTRY) { - throw new IllegalStateException("Next not called or already removed"); + throw new IllegalStateException("Next not called or entry already removed"); } it.remove(); current = (Entry) NO_ENTRY; @@ -158,11 +170,8 @@ public void remove() { }; } - public Object[] toArray() { return readOperation(() -> cache.entrySet().toArray()); } - public T[] toArray(T[] a) { return readOperation(() -> cache.entrySet().toArray(a)); } public boolean add(Entry kvEntry) { throw new UnsupportedOperationException("add() not supported on entrySet() of a Map"); } public boolean remove(Object o) { return writeOperation(() -> cache.entrySet().remove(o)); } - public boolean containsAll(Collection c) { return readOperation(() -> cache.entrySet().containsAll(c)); } public boolean addAll(Collection> c) { throw new UnsupportedOperationException("addAll() not supported on entrySet() of a Map"); } public boolean retainAll(Collection c) { return writeOperation(() -> cache.entrySet().retainAll(c)); } public boolean removeAll(Collection c) { return writeOperation(() -> cache.entrySet().removeAll(c)); }