-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from arago/development
Development
- Loading branch information
Showing
18 changed files
with
783 additions
and
201 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.3 | ||
0.2.1 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# v0.2.1 | ||
|
||
* Refactoring of ExpiringStore | ||
|
||
# v0.1.0 | ||
|
||
* Initial release | ||
|
This file contains 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
134 changes: 134 additions & 0 deletions
134
collections/src/main/java/co/arago/util/collections/expiringstore/AbstractExpiringStore.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package co.arago.util.collections.expiringstore; | ||
|
||
import co.arago.util.collections.expiringstore.exceptions.StoreItemExistsException; | ||
import co.arago.util.collections.expiringstore.exceptions.StoreItemExpiredException; | ||
import co.arago.util.collections.expiringstore.messages.ExpiringMessage; | ||
|
||
import java.time.Instant; | ||
import java.util.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* Abstract root class for expiring stores. | ||
* <p> | ||
* Contains the store items with an "expiresAt" after which the items are automatically removed from the store. | ||
* | ||
* @param <T> Type of items to store | ||
* @param <M> Type of ExpiringMessages to use. | ||
*/ | ||
public abstract class AbstractExpiringStore<T, M extends ExpiringMessage<T>> implements AutoCloseable { | ||
|
||
protected static final AtomicInteger counter = new AtomicInteger(0); | ||
|
||
private final Timer timer; | ||
private final String name; | ||
|
||
protected final Map<String, M> storeMap = new HashMap<>(); | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param name Name of the ExpiringStore. | ||
*/ | ||
public AbstractExpiringStore(String name) { | ||
this.name = name; | ||
this.timer = new Timer("Timer for " + name); | ||
} | ||
|
||
/** | ||
* Add the message | ||
* | ||
* @param expiringMessage Message to add | ||
* @throws StoreItemExistsException When the expiringMessage already exists. | ||
*/ | ||
protected void addInternal(M expiringMessage) throws StoreItemExistsException { | ||
M existingMessage = storeMap.putIfAbsent(expiringMessage.getId(), expiringMessage); | ||
if (existingMessage != null) { | ||
throw new StoreItemExistsException("Not adding " + expiringMessage.getMessage().getClass().getSimpleName() + | ||
" " + expiringMessage.getId() + " because it already exists."); | ||
} | ||
} | ||
|
||
/** | ||
* Put the message, allowing to overwrite messages with the same id. Overwritten messages will be canceled. | ||
* | ||
* @param expiringMessage Message to put | ||
*/ | ||
protected void putInternal(M expiringMessage) { | ||
M existingMessage = storeMap.put(expiringMessage.getId(), expiringMessage); | ||
if (existingMessage != null) | ||
existingMessage.cancel(); | ||
} | ||
|
||
/** | ||
* Add a message to the store if it does not exist | ||
* | ||
* @param expiresAt Timestamp after which the message expires | ||
* @param id The unique id of the message | ||
* @param message The message itself to store | ||
* @throws StoreItemExpiredException When the expiresAt is already expired. | ||
* @throws StoreItemExistsException When the message already exists. | ||
*/ | ||
public abstract void add(Instant expiresAt, String id, T message) throws StoreItemExpiredException, StoreItemExistsException; | ||
|
||
/** | ||
* Put a message to the store, possibly overwriting existing messages. | ||
* | ||
* @param expiresAt Timestamp after which the message expires | ||
* @param id The unique id of the message | ||
* @param message The message itself to store | ||
* @throws StoreItemExpiredException When the expiresAt is already expired. | ||
*/ | ||
public abstract void put(Instant expiresAt, String id, T message) throws StoreItemExpiredException; | ||
|
||
/** | ||
* Remove a message from the storeMap and cancel its TimerTask. | ||
* | ||
* @param id Id of the message | ||
*/ | ||
public synchronized void remove(String id) { | ||
M message = storeMap.remove(id); | ||
if (message != null) | ||
message.cancel(); | ||
} | ||
|
||
/** | ||
* Getter | ||
* | ||
* @param id Id of the message | ||
* @return The stored message | ||
*/ | ||
public synchronized T get(String id) { | ||
M message = storeMap.get(id); | ||
return (message != null ? message.getMessage() : null); | ||
} | ||
|
||
/** | ||
* Getter | ||
* | ||
* @return The name of this store. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Schedule an expiry with the {@link #timer}, | ||
* | ||
* @param timerTask The timerTask to schedule. | ||
* @param date The timestamp when the timerTask will be called. | ||
*/ | ||
public void schedule(TimerTask timerTask, Date date) { | ||
timer.schedule(timerTask, date); | ||
} | ||
|
||
/** | ||
* Cancel the {@link #timer} and clear the {@link #storeMap}. This Store cannot be used thereafter. | ||
*/ | ||
@Override | ||
public synchronized void close() { | ||
timer.cancel(); | ||
storeMap.clear(); | ||
} | ||
|
||
} |
This file contains 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
Oops, something went wrong.