Created
July 27, 2021 19:30
-
-
Save ova2/c174173abef34d0aebabbec589cfcae2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets cached values as Java Stream. Returned stream is not sorted. | |
*/ | |
public Stream<OVALUE> getValues() { | |
final Lock readLock = lock.readLock(); | |
readLock.lock(); | |
try { | |
return cache.values().stream().flatMap(cachedValue -> cachedValue.getValue().stream()); | |
} finally { | |
readLock.unlock(); | |
} | |
} | |
/** | |
* Gets cached value as Java Optional. | |
*/ | |
public Optional<OVALUE> getValue(KEY key) { | |
final Lock readLock = lock.readLock(); | |
readLock.lock(); | |
try { | |
return Optional.ofNullable(cache.get(key)).flatMap(CacheMonoValue::getValue); | |
} finally { | |
readLock.unlock(); | |
} | |
} | |
/** | |
* Removes the mapping for a key from this map if it is present. | |
*/ | |
public void remove(KEY key) { | |
final Lock writeLock = lock.writeLock(); | |
writeLock.lock(); | |
try { | |
cache.remove(key); | |
} finally { | |
writeLock.unlock(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment