Skip to content

Commit ba64ee2

Browse files
committed
release v0.3.0 (docs & tests)
1 parent 4fbf6b2 commit ba64ee2

28 files changed

+1021
-281
lines changed

.github/workflows/deploy.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build and deploy
2+
on:
3+
push:
4+
branches:
5+
- main
6+
jobs:
7+
build-and-deploy:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout repository
11+
uses: actions/checkout@v2
12+
- name: Setup JDK
13+
uses: actions/setup-java@v3
14+
with:
15+
java-version: '20'
16+
distribution: 'temurin'
17+
architecture: x64
18+
- name: Build and deploy binaries
19+
run: |
20+
export NEXUS_USERNAME="${{ secrets.NEXUS_USERNAME }}"
21+
export NEXUS_PASSWORD="${{ secrets.NEXUS_PASSWORD }}"
22+
23+
mvn -s $GITHUB_WORKSPACE/settings.xml deploy

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.idea/
22
target/
33
build/
4-
*.iml
5-
src/test
4+
*.iml

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ The concepts of `Observable` and `AttributeHolder` are considered seperate, howe
1414

1515
Subscriptions can be cancelled at any time with `subscription.cancel()`, and you are given the subscription object after observing an object.
1616

17-
It is **worth noting** that observing an object **does not** prevent the object from being garbage collected, however it **does** prevent the callback context from being garbage collected for the lifespan of the observed object.
17+
It is **worth noting** that observing an object with a **does not** prevent the object from being garbage collected, however it **does** prevent the callback context from being garbage collected for the lifespan of the observed object, if subscribed with a **strong reference**. Read the JavaDoc in Observable for more information.
1818

1919
### Dependency
2020

21-
We are currently on version `0.2.0`.
21+
We are currently on version `0.3.0`.
2222

2323
```xml
2424
<repositories>
@@ -32,7 +32,7 @@ We are currently on version `0.2.0`.
3232
<dependency>
3333
<groupId>dev.tommyjs</groupId>
3434
<artifactId>JObserve</artifactId>
35-
<version>0.2.0</version>
35+
<version>0.3.0</version>
3636
<scope>compile</scope>
3737
</dependency>
3838
</dependencies>
@@ -52,7 +52,7 @@ person.observeAttribute(balanceAttribute, newBalance -> {
5252
});
5353

5454
person.setAttribute(balanceAttribute, 1500);
55-
person.getAndUpdateAttribute(balanceAttribute, balance -> balance == null ? 0 : balance + 50);
55+
person.getAttributeAndUpdate(balanceAttribute, balance -> balance == null ? 0 : balance + 50);
5656

5757
/* OUTPUT
5858
The balance of John Smith has changed to 1500

pom.xml

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,66 @@
66

77
<groupId>dev.tommyjs</groupId>
88
<artifactId>JObserve</artifactId>
9-
<version>0.2.0</version>
9+
<version>0.3.0</version>
1010

1111
<properties>
12-
<maven.compiler.source>20</maven.compiler.source>
13-
<maven.compiler.target>20</maven.compiler.target>
12+
<java.version>20</java.version>
13+
<maven.compiler.source>${java.version}</maven.compiler.source>
14+
<maven.compiler.target>${java.version}</maven.compiler.target>
1415
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1516
</properties>
1617

18+
<distributionManagement>
19+
<repository>
20+
<id>tommyjs</id>
21+
<url>https://repo.tommyjs.dev/repository/maven-releases</url>
22+
</repository>
23+
</distributionManagement>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.13.0</version>
31+
<configuration>
32+
<source>${java.version}</source>
33+
<target>${java.version}</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-source-plugin</artifactId>
39+
<version>3.3.1</version>
40+
<executions>
41+
<execution>
42+
<id>attach-sources</id>
43+
<goals>
44+
<goal>jar</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-surefire-plugin</artifactId>
52+
<version>3.2.5</version>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
1757
<dependencies>
1858
<dependency>
1959
<groupId>org.jetbrains</groupId>
2060
<artifactId>annotations</artifactId>
2161
<version>24.1.0</version>
62+
<scope>compile</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.junit.jupiter</groupId>
66+
<artifactId>junit-jupiter</artifactId>
67+
<version>5.10.2</version>
68+
<scope>test</scope>
2269
</dependency>
2370
</dependencies>
2471

settings.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<settings>
2+
<servers>
3+
<server>
4+
<id>tommyjs</id>
5+
<username>${env.NEXUS_USERNAME}</username>
6+
<password>${env.NEXUS_PASSWORD}</password>
7+
</server>
8+
</servers>
9+
</settings>

src/main/java/dev/tommyjs/jobserve/attribute/AttributeHolder.java

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,120 @@
99
import java.util.function.Function;
1010
import java.util.function.Supplier;
1111

12+
/**
13+
* Represents an object that stores attributes that can be retrieved and updated concurrently.
14+
* <p>
15+
* Attributes are indexed by {@link AttributeKey} objects, which can be created with {@link AttributeKey#register(Class)}.
16+
* These keys are used to get and set objects with the type declared in the attribute type parameter.
17+
*/
1218
public interface AttributeHolder extends Observable, AttributeObservable {
1319

14-
AttributeRegistry getAttributes();
20+
/**
21+
* Retrieves the {@link AttributeRegistry} instance powering this {@link AttributeHolder}.
22+
* This method should always return the same registry instance.
23+
* @return attribute registry powering this object
24+
*/
25+
@NotNull AttributeRegistry getAttributes();
1526

1627
@Override
1728
default @NotNull ObserverEmitter getObserver() {
1829
return getAttributes().getObserver();
1930
}
2031

32+
/**
33+
* Checks whether a given attribute is stored in this attribute holder. It is worth noting
34+
* that a null value is treated as the value not being stored.
35+
* @param key attribute key
36+
* @return whether a value is stored
37+
*/
2138
default boolean hasAttribute(@NotNull AttributeKey<?> key) {
2239
return getAttribute(key) != null;
2340
}
2441

42+
/**
43+
* Retrieves an attribute from a given attribute key.
44+
* @param key attribute key
45+
* @return current attribute value, or null if not stored
46+
*/
2547
default <T> @Nullable T getAttribute(@NotNull AttributeKey<T> key) {
2648
return getAttributes().getAttribute(key);
2749
}
2850

29-
default <T> @Nullable T getAttributeOrDefault(@NotNull AttributeKey<T> key, @Nullable T defaultValue) {
51+
/**
52+
* Retrieves an attribute from a given attribute key, but returns a default value if the
53+
* attribute is not stored. This method will not set the default value as the stored value
54+
* if the attribute is not stored.
55+
* @param key attribute key
56+
* @param defaultValue default value to return if not stored
57+
* @return current attribute value, or default value if not stored
58+
*/
59+
default <T> @NotNull T getAttributeOrDefault(@NotNull AttributeKey<T> key, @NotNull T defaultValue) {
3060
return getAttributes().getAttributeOrDefault(key, defaultValue);
3161
}
3262

63+
/**
64+
* Retrieves an attribute from a given attribute key, but calls default value if the
65+
* attribute is not stored. This method will set the default value as the stored value
66+
* if the attribute is not stored.
67+
* @param key attribute key
68+
* @param supplier default value supplier to call and return if not stored
69+
* @return final attribute value, or default value if not stored
70+
*/
3371
default <T> @NotNull T getAttributeOrCreateDefault(@NotNull AttributeKey<T> key, @NotNull Supplier<@NotNull T> supplier) {
3472
return getAttributes().getAttributeOrCreateDefault(key, supplier);
3573
}
3674

75+
/**
76+
* Sets an attribute with a given attribute key to a given value. If the given value is
77+
* null, the value will no longer be stored.
78+
* @param key attribute key
79+
* @param value new stored value
80+
*/
3781
default <T> void setAttribute(@NotNull AttributeKey<T> key, @Nullable T value) {
3882
getAttributes().setAttribute(key, value);
3983
}
4084

41-
default <T> @Nullable T getAttributeOrSetDefault(@NotNull AttributeKey<T> key, @Nullable T defaultValue) {
85+
/**
86+
* Retrieves an attribute from a given attribute key, but returns a default value if the
87+
* attribute is not stored. This method will set the default value as the stored value
88+
* if the attribute is not stored.
89+
* @param key attribute key
90+
* @param defaultValue default value to return if not stored
91+
* @return final attribute value, or default value if not stored
92+
*/
93+
default <T> @Nullable T getAttributeOrSetDefault(@NotNull AttributeKey<T> key, @NotNull T defaultValue) {
4294
return getAttributes().getAttributeOrSetDefault(key, defaultValue);
4395
}
4496

45-
default <T> T getAttributeAndUpdate(@NotNull AttributeKey<T> key, @NotNull Function<@Nullable T, @Nullable T> function) {
97+
/**
98+
* Gets an attribute from a given attribute key, and applies a function to it before storing
99+
* the value returned by the function.
100+
* @param key attribute key
101+
* @param function function to apply to the previous value to obtain the new stored value
102+
* @return previous stored value
103+
*/
104+
default <T> @Nullable T getAttributeAndUpdate(@NotNull AttributeKey<T> key, @NotNull Function<@Nullable T, @Nullable T> function) {
46105
return getAttributes().getAndUpdate(key, function);
47106
}
48107

49-
@Deprecated(forRemoval = true, since = "0.2.0")
50-
default <T> T getAndUpdateAttribute(@NotNull AttributeKey<T> key, @NotNull Function<@Nullable T, @Nullable T> function) {
51-
return getAttributes().getAndUpdate(key, function);
52-
}
53-
54-
default <T> Optional<T> getAttibuteAsOptional(@NotNull AttributeKey<T> key) {
55-
return getAttributes().getAsOptional(key);
108+
/**
109+
* Gets an attribute from a given attribute key, and applies a function to it before storing
110+
* the value returned by the function.
111+
* @param key attribute key
112+
* @param function function to apply to the previous value to obtain the new stored value
113+
* @return final stored value
114+
*/
115+
default <T> @Nullable T updateAttributeAndGet(@NotNull AttributeKey<T> key, @NotNull Function<@Nullable T, @Nullable T> function) {
116+
return getAttributes().updateAndGet(key, function);
56117
}
57118

58-
@Deprecated(forRemoval = true, since = "0.2.0")
59-
default <T> Optional<T> getAsOptional(@NotNull AttributeKey<T> key) {
119+
/**
120+
* Gets an attribute from a given attribute key as an optional. The optional will be empty
121+
* if the value is not stored.
122+
* @param key attribute key
123+
* @return optional with the stored value
124+
*/
125+
default <T> @NotNull Optional<T> getAttributeAsOptional(@NotNull AttributeKey<T> key) {
60126
return getAttributes().getAsOptional(key);
61127
}
62128

src/main/java/dev/tommyjs/jobserve/attribute/AttributeHolderObject.java

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

src/main/java/dev/tommyjs/jobserve/attribute/AttributeKey.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
public class AttributeKey<T> {
77

8-
private static final Object LOCK = new Object();
98
private static final Random RANDOM = new SecureRandom();
109

1110
private final int id;
@@ -33,9 +32,7 @@ public boolean equals(Object o) {
3332
}
3433

3534
public static <T> AttributeKey<T> register(Class<T> clazz) {
36-
synchronized (LOCK) {
37-
return new AttributeKey<>(RANDOM.nextInt(), clazz);
38-
}
35+
return new AttributeKey<>(RANDOM.nextInt(), clazz);
3936
}
4037

4138
}

src/main/java/dev/tommyjs/jobserve/attribute/AttributeObservable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66

77
import java.util.function.Consumer;
88

9+
/**
10+
* A marker interface signifying that this object has attributes which can be subscribed to, in
11+
* order to be notified of updates to attributes.
12+
*/
913
public interface AttributeObservable extends Observable {
1014

15+
/**
16+
* Subscribes to mutations of a given {@link AttributeKey} on an {@link Observable} with attributes.
17+
* @param key attribute key
18+
* @param consumer callback
19+
* @return cancellable subscription
20+
*/
1121
default @NotNull <T> ObserverSubscription observeAttribute(@NotNull AttributeKey<T> key, @NotNull Consumer<T> consumer) {
1222
return observe(AttributeRegistry.UPDATE_ATTRIBUTE_OBSERVER, (k, o) -> {
1323
if (k == key) {

0 commit comments

Comments
 (0)