Skip to content

Commit 7564256

Browse files
authored
comment,doc: added javadoc comments (#35)
1 parent 664cd3f commit 7564256

File tree

8 files changed

+328
-3
lines changed

8 files changed

+328
-3
lines changed

src/main/java/com/github/sttk/sabi/AsyncGroup.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,35 @@
44
*/
55
package com.github.sttk.sabi;
66

7+
/**
8+
* An interface for asynchronously executing multiple {@link Runner} instances and waiting for their
9+
* completion.
10+
*
11+
* <p>Implementations of this interface allow adding multiple {@link Runner} objects, which are then
12+
* executed concurrently. The group waits until all added runners have finished their execution. Any
13+
* errors occurring during the execution of a {@link Runner} are stored and can be retrieved by
14+
* their names in a map.
15+
*/
716
public interface AsyncGroup {
17+
18+
/**
19+
* Represents the reason for a new {@link com.github.sttk.errs.Exc} exception object when an
20+
* exception occurred during the execution of a {@link Runner} and the exception class was not the
21+
* {@link com.github.sttk.errs.Exc}.
22+
*/
823
record RunnerFailed() {}
924

25+
/**
26+
* Represents the reason for an {@link com.github.sttk.errs.Exc} exception object when the
27+
* creation of a thread for asynchronous execution of a {@link Runner} fails.
28+
*/
1029
record RunnerInterrupted() {}
1130

31+
/**
32+
* Adds a {@link Runner} to this group for asynchronous execution. The added runner will be
33+
* executed in a separate thread.
34+
*
35+
* @param runner The {@link Runner} to be added and executed asynchronously.
36+
*/
1237
void add(final Runner runner);
1338
}

src/main/java/com/github/sttk/sabi/DataAcc.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66

77
import com.github.sttk.errs.Exc;
88

9+
/**
10+
* An interface designed for implementing data access operations through default methods in its
11+
* sub-interfaces.
12+
*
13+
* <p>Sub-interfaces of {@code DataAcc} are expected to define and implement data access methods as
14+
* default methods. Within these default methods, the connection to the underlying data store should
15+
* be obtained using the {@link #getDataConn(String, Class)} method provided by this interface. This
16+
* design promotes a clear separation of concerns, allowing data access logic to be encapsulated
17+
* within the interface itself.
18+
*/
919
public interface DataAcc {
20+
/**
21+
* Retrieves a connection to a data store. This method is intended to be used by default methods
22+
* in sub-interfaces to obtain the necessary connection for performing data access operations.
23+
*
24+
* @param <C> The type of the data connection, which must extend {@link DataConn}.
25+
* @param name The name identifying the specific data connection to retrieve.
26+
* @param cls The {@link Class} object representing the type of the desired data connection.
27+
* @return A data connection object of the specified type.
28+
* @throws Exc if an error occurs while obtaining the data connection.
29+
*/
1030
<C extends DataConn> C getDataConn(String name, Class<C> cls) throws Exc;
1131
}

src/main/java/com/github/sttk/sabi/DataConn.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,77 @@
66

77
import com.github.sttk.errs.Exc;
88

9+
/**
10+
* The interface that abstracts a connection per session to an external data service, such as a
11+
* database, file system, or messaging service.
12+
*
13+
* <p>Its primary purpose is to enable cohesive transaction operations across multiple external data
14+
* services within a single transaction context. Implementations of this interface provide the
15+
* concrete input/output operations for their respective data services.
16+
*
17+
* <p>Methods declared within this interface are designed to handle transactional logic. The
18+
* AsyncGroup parameter in various methods allows for asynchronous processing when commit or
19+
* rollback operations are time-consuming.
20+
*/
921
public interface DataConn {
22+
/**
23+
* Commits the changes made within the current session to the external data service. This method
24+
* is responsible for finalizing all operations performed since the last commit or rollback.
25+
*
26+
* @param ag An {@link AsyncGroup} that can be used to perform asynchronous operations if the
27+
* commit process is time-consuming.
28+
* @throws Exc if an error occurs during the commit operation.
29+
*/
1030
void commit(AsyncGroup ag) throws Exc;
1131

32+
/**
33+
* Performs any necessary pre-commit operations. This method is called before the {@link
34+
* #commit(AsyncGroup)} method.
35+
*
36+
* @param ag An {@link AsyncGroup} that can be used for asynchronous pre-commit tasks.
37+
* @throws Exc if an error occurs during the pre-commit operation.
38+
*/
1239
default void preCommit(AsyncGroup ag) throws Exc {}
1340

41+
/**
42+
* Performs any necessary post-commit operations. This method is called after the {@link
43+
* #commit(AsyncGroup)} method has successfully completed.
44+
*
45+
* @param ag An {@link AsyncGroup} that can be used for asynchronous post-commit tasks.
46+
*/
1447
default void postCommit(AsyncGroup ag) {}
1548

49+
/**
50+
* Indicates whether a force-back operation should be performed. A force-back is a mechanism to
51+
* revert the committed changes when this connection had been already committed but the other
52+
* connection had failed.
53+
*
54+
* @return {@code true} if a force-back is required, {@code false} otherwise.
55+
*/
1656
default boolean shouldForceBack() {
1757
return false;
1858
}
1959

60+
/**
61+
* Rolls back the changes made within the current session, discarding all operations performed
62+
* since the last commit or rollback.
63+
*
64+
* @param ag An {@link AsyncGroup} that can be used to perform asynchronous operations if the
65+
* rollback process is time-consuming.
66+
*/
2067
void rollback(AsyncGroup ag);
2168

69+
/**
70+
* Performs a force-back operation to revert the committed changes when this connection had been
71+
* already committed but the other connection had failed.
72+
*
73+
* @param ag An {@link AsyncGroup} that can be used for asynchronous force-back tasks.
74+
*/
2275
default void forceBack(AsyncGroup ag) {}
2376

77+
/**
78+
* Closes the connection to the external data service, releasing any associated resources. This
79+
* method should be called to ensure proper resource management.
80+
*/
2481
void close();
2582
}

src/main/java/com/github/sttk/sabi/DataHub.java

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,137 @@
88
import com.github.sttk.sabi.internal.DataHubInner;
99
import java.util.Map;
1010

11+
/**
12+
* {@code DataHub} is a central component in the Sabi framework that manages {@link DataSrc} and
13+
* {@link DataConn} instances, facilitating data access and transaction management. It implements
14+
* both {@link DataAcc} for data access operations and {@link AutoCloseable} for resource
15+
* management.
16+
*
17+
* <p>This class allows for the registration and unregistration of local {@link DataSrc} objects,
18+
* and provides methods to execute application logic with or without transactional boundaries.
19+
*/
1120
public class DataHub implements DataAcc, AutoCloseable {
21+
/**
22+
* Represents an error reason that occurred when failing to set up global {@link DataSrc}
23+
* instances.
24+
*
25+
* @param errors A map containing the names of the data sources and the corresponding exceptions
26+
* that occurred during their setup.
27+
*/
1228
public record FailToSetupGlobalDataSrcs(Map<String, Exc> errors) {}
1329

30+
/**
31+
* Represents an error reason that occurred when failing to set up local {@link DataSrc}
32+
* instances.
33+
*
34+
* @param errors A map containing the names of the data sources and the corresponding exceptions
35+
* that occurred during their setup.
36+
*/
1437
public record FailToSetupLocalDataSrcs(Map<String, Exc> errors) {}
1538

39+
/**
40+
* Represents an error reason that occurred when failing to commit one or more {@link DataConn}
41+
* instances.
42+
*
43+
* @param errors A map containing the names of the data connections and the corresponding
44+
* exceptions that occurred during their commit.
45+
*/
1646
public record FailToCommitDataConn(Map<String, Exc> errors) {}
1747

48+
/**
49+
* Represents an error reason that occurred when failing to pre-commit one or more {@link
50+
* DataConn} instances.
51+
*
52+
* @param errors A map containing the names of the data connections and the corresponding
53+
* exceptions that occurred during their pre-commit.
54+
*/
1855
public record FailToPreCommitDataConn(Map<String, Exc> errors) {}
1956

57+
/**
58+
* Represents an error reason where no {@link DataSrc} was found to create a {@link DataConn} with
59+
* the specified name and type.
60+
*
61+
* @param name The name of the data source requested.
62+
* @param dataConnType The type of the data connection requested.
63+
*/
2064
public record NoDataSrcToCreateDataConn(String name, String dataConnType) {}
2165

66+
/**
67+
* Represents an error reason that occurred when failing to create a {@link DataConn} instance.
68+
*
69+
* @param name The name of the data source from which the connection was attempted.
70+
* @param dataConnType The type of the data connection that failed to be created.
71+
*/
2272
public record FailToCreateDataConn(String name, String dataConnType) {}
2373

74+
/**
75+
* Represents an error reason where the created {@link DataConn} instance was null.
76+
*
77+
* @param name The name of the data source.
78+
* @param dataConnType The type of the data connection expected.
79+
*/
2480
public record CreatedDataConnIsNull(String name, String dataConnType) {}
2581

82+
/**
83+
* Represents an error reason that occurred when failing to cast a {@link DataConn} to the
84+
* requested type.
85+
*
86+
* @param name The name of the data connection.
87+
* @param castToType The type to which the data connection was attempted to be cast.
88+
*/
2689
public record FailToCastDataConn(String name, String castToType) {}
2790

91+
/**
92+
* Represents an error reason that occurred when failing to cast the {@code DataHub} instance
93+
* itself to the expected data access interface type for a {@link Logic}.
94+
*
95+
* @param castFromType The actual type of the {@code DataHub} instance that failed to cast.
96+
*/
2897
public record FailToCastDataHub(String castFromType) {}
2998

30-
public record RuntimeExceptionOccured() {}
99+
/**
100+
* Represents an unexpected {@link RuntimeException} that occurred during pre-commit or commit
101+
* operations.
102+
*/
103+
public record RuntimeExceptionOccurred() {}
31104

32105
private final DataHubInner inner = new DataHubInner();
33106

107+
/** Constructs a new {@code DataHub} instance. */
34108
public DataHub() {}
35109

110+
/**
111+
* Registers a local {@link DataSrc} with the specified name for use within this {@code DataHub}
112+
* instance. This allows specific data sources to be managed independently from globally
113+
* registered ones.
114+
*
115+
* @param name The unique name for the {@link DataSrc}.
116+
* @param ds The {@link DataSrc} instance to register.
117+
*/
36118
public void uses(String name, DataSrc ds) {
37119
inner.uses(name, ds);
38120
}
39121

122+
/**
123+
* Unregisters a local {@link DataSrc} with the given name from this {@code DataHub} instance.
124+
*
125+
* @param name The name of the {@link DataSrc} to unregister.
126+
*/
40127
public void disuses(String name) {
41128
inner.disuses(name);
42129
}
43130

131+
/**
132+
* Executes the provided application {@link Logic} without transactional boundaries. The {@code
133+
* DataHub} instance itself is passed as the data access object {@code D} to the {@link Logic}'s
134+
* {@code run} method.
135+
*
136+
* @param <D> The type of the data access object, which typically is {@code DataHub} or an
137+
* interface implemented by {@code DataHub} that {@link Logic} expects.
138+
* @param logic The application logic to execute.
139+
* @throws Exc if an {@link Exc} or {@link RuntimeException} occurs during logic execution or if
140+
* the {@code DataHub} cannot be cast to the expected data access type.
141+
*/
44142
public <D> void run(Logic<D> logic) throws Exc {
45143
D data;
46144
try {
@@ -60,6 +158,19 @@ public <D> void run(Logic<D> logic) throws Exc {
60158
}
61159
}
62160

161+
/**
162+
* Executes the provided application {@link Logic} within a transactional context. The {@code
163+
* DataHub} instance is passed as the data access object {@code D} to the {@link Logic}'s {@code
164+
* run} method. If the logic completes successfully, a commit operation is attempted. If any
165+
* {@link Exc}, {@link RuntimeException}, or {@link Error} occurs, a rollback operation is
166+
* performed.
167+
*
168+
* @param <D> The type of the data access object, which typically is {@code DataHub} or an
169+
* interface implemented by {@code DataHub} that {@link Logic} expects.
170+
* @param logic The application logic to execute transactionally.
171+
* @throws Exc if an {@link Exc}, {@link RuntimeException}, or {@link Error} occurs during logic
172+
* execution, pre-commit, or commit. The original exception is re-thrown after rollback.
173+
*/
63174
public <D> void txn(Logic<D> logic) throws Exc {
64175
D data;
65176
try {
@@ -82,11 +193,27 @@ public <D> void txn(Logic<D> logic) throws Exc {
82193
}
83194
}
84195

196+
/**
197+
* Retrieves a {@link DataConn} instance from the managed data sources. This method is part of the
198+
* {@link DataAcc} interface implementation.
199+
*
200+
* @param <C> The type of the {@link DataConn} to retrieve, which must extend {@link DataConn}.
201+
* @param name The name of the data source from which to get the connection.
202+
* @param cls The {@link Class} object representing the desired type of the data connection.
203+
* @return A {@link DataConn} instance of the specified type.
204+
* @throws Exc if no data source is found, if the connection cannot be created, if the created
205+
* connection is null, or if the connection cannot be cast to the specified class.
206+
*/
85207
@Override
86208
public <C extends DataConn> C getDataConn(String name, Class<C> cls) throws Exc {
87209
return inner.getDataConn(name, cls);
88210
}
89211

212+
/**
213+
* Closes all {@link DataConn} instances managed by this {@code DataHub}, releasing their
214+
* resources. This method is part of the {@link AutoCloseable} interface and should be called to
215+
* ensure proper resource cleanup, ideally in a try-with-resources statement.
216+
*/
90217
@Override
91218
public void close() {
92219
inner.close();

src/main/java/com/github/sttk/sabi/DataSrc.java

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

77
import com.github.sttk.errs.Exc;
88

9+
/**
10+
* The interface that abstracts a data source responsible for managing connections to external data
11+
* services, such as databases, file systems, or messaging services.
12+
*
13+
* <p>It receives configuration for connecting to an external data service and then creates and
14+
* supplies a {@link DataConn} instance, representing a single session connection.
15+
*/
916
public interface DataSrc {
17+
/**
18+
* Sets up the data source, performing any necessary initialization or configuration to establish
19+
* connectivity to the external data service. This method is typically called once at the
20+
* application startup.
21+
*
22+
* @param ag An {@link AsyncGroup} that can be used for asynchronous setup operations, especially
23+
* if initialization is time-consuming.
24+
* @throws Exc if an error occurs during the setup process.
25+
*/
1026
void setup(AsyncGroup ag) throws Exc;
1127

28+
/**
29+
* Closes the data source, releasing all resources and shutting down connections managed by this
30+
* source. This method should be called when the application is shutting down to ensure proper
31+
* resource cleanup.
32+
*/
1233
void close();
1334

35+
/**
36+
* Creates and returns a new {@link DataConn} instance, representing a single session connection
37+
* to the external data service. Each call to this method should yield a new, independent
38+
* connection.
39+
*
40+
* @return A new {@link DataConn} instance for a session.
41+
* @throws Exc if an error occurs while creating the data connection.
42+
*/
1443
DataConn createDataConn() throws Exc;
1544
}

src/main/java/com/github/sttk/sabi/Logic.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,28 @@
66

77
import com.github.sttk.errs.Exc;
88

9+
/**
10+
* Represents the application's business logic, designed to separate data access concerns from the
11+
* core logic.
12+
*
13+
* <p>Implementations of this functional interface should focus solely on the business logic,
14+
* utilizing the provided {@code data} object of type {@code D} for all data access operations. The
15+
* {@link #run(Object)} method should not contain any direct data access code; instead, it should
16+
* delegate such operations to methods of the {@code D} object.
17+
*
18+
* <p>If an exceptional condition occurs during the execution of the logic, an {@link Exc} object
19+
* should be thrown.
20+
*
21+
* @param <D> The type of the data access object through which data operations are performed.
22+
*/
923
@FunctionalInterface
1024
public interface Logic<D> {
25+
/**
26+
* Executes the application's business logic. This method should implement the core logic, relying
27+
* on the {@code data} object for all data access needs.
28+
*
29+
* @param data The data access object, providing methods for interacting with data.
30+
* @throws Exc if an error or exceptional condition occurs during the logic execution.
31+
*/
1132
void run(D data) throws Exc;
1233
}

0 commit comments

Comments
 (0)