|
| 1 | +package today.bonfire.oss.sop; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents a wrapper for pooled objects that maintains state information about the object's usage. |
| 7 | + * This class tracks whether an object is borrowed, broken, or idle, and manages its usage timestamps. |
| 8 | + * |
| 9 | + * @param <T> The type of the pooled object, must implement PoolObject for proper identification |
| 10 | + */ |
| 11 | +public class PooledObject<T extends PoolObject> { |
| 12 | + private final T object; |
| 13 | + private final long creationTime; |
| 14 | + private volatile long lastBorrowedTime; |
| 15 | + private volatile long idleFromTime; |
| 16 | + private volatile boolean broken; |
| 17 | + private volatile boolean borrowed; |
| 18 | + private long timesBorrowed; |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates a new PooledEntity wrapping the given object with a specified ID. |
| 22 | + * Initializes the object in an idle, non-broken state with current timestamp. |
| 23 | + * |
| 24 | + * @param object The object to be pooled |
| 25 | + * @param id The unique identifier to assign to this pooled entity |
| 26 | + */ |
| 27 | + PooledObject(T object, Long id) { |
| 28 | + Objects.requireNonNull(object, "Pool object cannot be null"); |
| 29 | + this.object = object; |
| 30 | + object.setEntityId(id); |
| 31 | + this.creationTime = System.currentTimeMillis(); |
| 32 | + this.borrowed = false; |
| 33 | + this.broken = false; // Initialize broken state |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the number of times this object has been borrowed from the pool. |
| 38 | + * |
| 39 | + * @return The number of times this object has been borrowed. |
| 40 | + */ |
| 41 | + public long timesBorrowed() { |
| 42 | + return timesBorrowed; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the timestamp when this object was created. |
| 47 | + * |
| 48 | + * @return The creation timestamp in milliseconds. |
| 49 | + */ |
| 50 | + public long creationTime() { |
| 51 | + return creationTime; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the timestamp when this object was last borrowed from the pool. |
| 56 | + * |
| 57 | + * @return The last borrowed timestamp in milliseconds. |
| 58 | + */ |
| 59 | + public long lastBorrowedTime() { |
| 60 | + return lastBorrowedTime; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Checks if this object is marked as broken. |
| 65 | + * |
| 66 | + * @return {@code true} if the object is broken, {@code false} otherwise. |
| 67 | + */ |
| 68 | + public boolean isBroken() { |
| 69 | + return broken; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Sets the broken state of this object. |
| 74 | + * |
| 75 | + * @param broken {@code true} if the object should be marked as broken, {@code false} otherwise. |
| 76 | + */ |
| 77 | + public void broken(boolean broken) { |
| 78 | + this.broken = broken; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the underlying pooled object. |
| 83 | + * |
| 84 | + * @return The pooled object. |
| 85 | + */ |
| 86 | + public T object() { |
| 87 | + return object; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Retrieves the entity ID of the wrapped object. |
| 92 | + * |
| 93 | + * @return The unique identifier of the wrapped object. |
| 94 | + */ |
| 95 | + Long id() { |
| 96 | + return object.getEntityId(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Marks this object as borrowed and updates its last borrowed timestamp. |
| 101 | + * This method should be called when the object is taken from the pool. |
| 102 | + */ |
| 103 | + void borrow() { |
| 104 | + borrowed = true; |
| 105 | + lastBorrowedTime = System.currentTimeMillis(); |
| 106 | + timesBorrowed++; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Marks this object as idle and updates its idle timestamp. |
| 111 | + * This method should be called when the object is returned to the pool. |
| 112 | + */ |
| 113 | + void markIdle() { |
| 114 | + borrowed = false; |
| 115 | + idleFromTime = System.currentTimeMillis(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Checks if this object has been abandoned by comparing its last borrowed time |
| 120 | + * against the specified abandonment timeout. |
| 121 | + * |
| 122 | + * @param abandonmentTimeoutMillis The maximum allowed duration in milliseconds since the object was last borrowed. |
| 123 | + * @return {@code true} if the object is considered abandoned, {@code false} otherwise. |
| 124 | + */ |
| 125 | + boolean isAbandoned(long now, long abandonmentTimeoutMillis) { |
| 126 | + return borrowed && ((now - lastBorrowedTime) >= abandonmentTimeoutMillis); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the duration in milliseconds that this object has been idle. |
| 131 | + * |
| 132 | + * @return The duration in milliseconds that this object has been idle. |
| 133 | + */ |
| 134 | + public long idlingTime() { |
| 135 | + return System.currentTimeMillis() - idleFromTime; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public int hashCode() { |
| 140 | + return Long.hashCode(id()); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean equals(Object o) { |
| 145 | + if (this == o) return true; |
| 146 | + if (!(o instanceof PooledObject<?> that)) return false; |
| 147 | + return Objects.equals(id(), that.id()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns the number of times this object has been borrowed |
| 152 | + * |
| 153 | + * @return borrow count |
| 154 | + */ |
| 155 | + public long borrowCount() { |
| 156 | + return timesBorrowed; |
| 157 | + } |
| 158 | +} |
0 commit comments