Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sagar Upadhyaya <[email protected]>
  • Loading branch information
sgup432 committed Apr 4, 2024
1 parent 77720bd commit 4a0a357
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ static class ConcatenatedIterables<K> implements Iterable<K> {
this.iterables = iterables;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({ "unchecked" })
@Override
public Iterator<K> iterator() {
Iterator<K>[] iterators = (Iterator<K>[]) new Iterator[iterables.length];
Iterator<K>[] iterators = (Iterator<K>[]) new Iterator<?>[iterables.length];
for (int i = 0; i < iterables.length; i++) {
iterators[i] = iterables[i].iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ public void invalidateAll() {

@Override
public Iterable<K> keys() {
return new Iterable<K>() {
@Override
public Iterator<K> iterator() {
return new CacheKeyIterator<>(cache, removalListener);
}
};
return () -> new CacheKeyIterator<>(cache, removalListener);
}

@Override
Expand Down Expand Up @@ -169,11 +164,11 @@ public Builder<K, V> setValueSerializer(Serializer<V, byte[]> valueSerializer) {
* @param <K> Type of key
* @param <V> Type of value
*/
class CacheKeyIterator<K, V> implements Iterator<K> {
static class CacheKeyIterator<K, V> implements Iterator<K> {
private final Iterator<Map.Entry<K, V>> entryIterator;
private final Map<K, V> cache;
private final RemovalListener<K, V> removalListener;
private K lastKey;
private K currentKey;

public CacheKeyIterator(Map<K, V> cache, RemovalListener<K, V> removalListener) {
this.entryIterator = cache.entrySet().iterator();
Expand All @@ -192,19 +187,19 @@ public K next() {
throw new NoSuchElementException();
}
Map.Entry<K, V> entry = entryIterator.next();
lastKey = entry.getKey();
return lastKey;
currentKey = entry.getKey();
return currentKey;
}

@Override
public void remove() {
if (lastKey == null) {
if (currentKey == null) {
throw new IllegalStateException("No element to remove");
}
V value = cache.get(lastKey);
cache.remove(lastKey);
this.removalListener.onRemoval(new RemovalNotification<>(lastKey, value, RemovalReason.INVALIDATED));
lastKey = null;
V value = cache.get(currentKey);
cache.remove(currentKey);
this.removalListener.onRemoval(new RemovalNotification<>(currentKey, value, RemovalReason.INVALIDATED));
currentKey = null;
}
}
}

0 comments on commit 4a0a357

Please sign in to comment.