Skip to content

Commit 85b50e8

Browse files
V2.0.0 (#2)
* added factory getters * pool config changes * pool changes to suite new config * fixed activate object call and added log format * config tests * udpated retry logic, and handle and error case * added most used option and handled better eviction logic * fixed some issue with object pool eviction and return * tests passing state * updated version * updated version and fixed flaky tests * updated parent pom version
1 parent 8c6a0c8 commit 85b50e8

21 files changed

+2140
-524
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<parent>
88
<groupId>today.bonfire.oss</groupId>
99
<artifactId>bonfire-oss-parent</artifactId>
10-
<version>1.1.3</version>
10+
<version>1.1.4</version>
1111
</parent>
1212

1313
<artifactId>simple-object-pool</artifactId>
14-
<version>1.3.0</version>
14+
<version>2.0.0</version>
1515
<packaging>jar</packaging>
1616

1717
<properties>

src/main/java/today/bonfire/oss/sop/PoolEntity.java renamed to src/main/java/today/bonfire/oss/sop/PoolObject.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Represents an entity that can be managed by a {@link SimpleObjectPool}. Each implementing class must provide a unique identifier.
55
*/
6-
public interface PoolEntity {
6+
public interface PoolObject {
77

88
/**
99
* Retrieves the unique identifier for this entity.
@@ -17,7 +17,8 @@ public interface PoolEntity {
1717
/**
1818
* Sets the unique identifier for this entity.
1919
* This method should only be called by the {@link SimpleObjectPool} implementation.
20-
* Directly setting this value may lead to unexpected behavior.
20+
* Directly setting this value may lead to unexpected behavior.
21+
* Implementations should throw an {@link IllegalStateException} if the entity ID is already set as it is not allowed to change.
2122
* <p>
2223
* Example:
2324
* <pre>{@code
@@ -31,6 +32,9 @@ public interface PoolEntity {
3132
*
3233
* @Override
3334
* public void setEntityId(Long entityIdValue) {
35+
* if (this.entityId != null) {
36+
* throw new IllegalStateException("Entity ID already set");
37+
* }
3438
* this.entityId = entityIdValue;
3539
* }
3640
* }

src/main/java/today/bonfire/oss/sop/PoolResourceException.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/java/today/bonfire/oss/sop/PooledEntity.java

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

src/main/java/today/bonfire/oss/sop/PooledObjectFactory.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,32 @@
33
/**
44
* Provides a factory for creating pooled objects.
55
* Implementations should provide a concrete type of T that implements the
6-
* {@link PoolEntity} interface.
6+
* {@link PoolObject} interface.
77
*
8-
* @param <T> the type of object to be pooled, must implement {@link PoolEntity}
8+
* @param <T> the type of object to be pooled, must implement {@link PoolObject}
99
*/
10-
public interface PooledObjectFactory<T extends PoolEntity> {
10+
public interface PooledObjectFactory<T extends PoolObject> {
1111

1212
/**
1313
* Creates an instance of the pooled object.
1414
* This method is responsible for instantiating and initializing
15-
* a new object of type T that implements the {@link PoolEntity} interface.
15+
* a new object of type T that implements the {@link PoolObject} interface.
1616
* Implementations should handle any exceptions gracefully.
1717
*
1818
* @return a newly created object of type T, never null.
1919
*/
2020
T createObject();
2121

22+
23+
/**
24+
* Activates the pooled object.
25+
* This method is called when the object is taken from the pool,
26+
* and is responsible for any necessary activation or initialization setting before borrow.
27+
*
28+
* @param obj the object to activate, must not be null.
29+
*/
30+
void activateObject(T obj);
31+
2232
/**
2333
* Validates the pooled object to determine if it can be safely borrowed.
2434
* This method should return true if the object is in a valid state for

0 commit comments

Comments
 (0)