Skip to content

Commit f546463

Browse files
committed
Add multi append request related class.
1 parent 447b0ff commit f546463

7 files changed

+523
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import javax.validation.constraints.NotNull;
4+
import java.util.Objects;
5+
6+
/**
7+
* Represents a failed append to a stream.
8+
*/
9+
public class AppendStreamFailure {
10+
private final String stream;
11+
private final Exception error;
12+
13+
/**
14+
* Initializes a new instance of the AppendStreamFailure class.
15+
*
16+
* @param stream The stream name.
17+
* @param error The error that occurred.
18+
*/
19+
public AppendStreamFailure(@NotNull String stream, Exception error) {
20+
this.stream = stream;
21+
this.error = error;
22+
}
23+
24+
/**
25+
* Gets the stream name.
26+
*
27+
* @return The stream name.
28+
*/
29+
public String getStream() {
30+
return stream;
31+
}
32+
33+
/**
34+
* Gets the error that occurred.
35+
*
36+
* @return The error that occurred.
37+
*/
38+
public Exception getError() {
39+
return error;
40+
}
41+
42+
@Override
43+
public boolean equals(Object o) {
44+
if (this == o) return true;
45+
if (o == null || getClass() != o.getClass()) return false;
46+
AppendStreamFailure that = (AppendStreamFailure) o;
47+
return Objects.equals(stream, that.stream) &&
48+
Objects.equals(error, that.error);
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(stream, error);
54+
}
55+
56+
@Override
57+
public String toString() {
58+
return "AppendStreamFailure{" +
59+
"stream='" + stream + '\'' +
60+
", error=" + error +
61+
'}';
62+
}
63+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
/**
7+
* Represents a collection of failed appends to streams.
8+
*/
9+
public class AppendStreamFailures extends ArrayList<AppendStreamFailure> {
10+
11+
/**
12+
* Initializes a new instance of the AppendStreamFailures class.
13+
*/
14+
public AppendStreamFailures() {
15+
super();
16+
}
17+
18+
/**
19+
* Initializes a new instance of the AppendStreamFailures class with the specified collection of AppendStreamFailure objects.
20+
*
21+
* @param input The collection of AppendStreamFailure objects.
22+
*/
23+
public AppendStreamFailures(Collection<AppendStreamFailure> input) {
24+
super(input);
25+
}
26+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import io.kurrent.dbclient.StreamState;
4+
5+
import javax.validation.constraints.NotNull;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Objects;
9+
10+
/**
11+
* Represents a request to append messages to a stream.
12+
*/
13+
public class AppendStreamRequest {
14+
private final String stream;
15+
private final List<Message> messages;
16+
private final StreamState expectedState;
17+
/**
18+
* Initializes a new instance of the AppendStreamRequest class.
19+
*
20+
* @param stream The stream name.
21+
* @param messages The messages to append.
22+
* @param expectedState The expected state of the stream.
23+
*/
24+
public AppendStreamRequest(@NotNull String stream, @NotNull List<Message> messages, @NotNull StreamState expectedState) {
25+
this.stream = stream;
26+
this.messages = messages;
27+
this.expectedState = expectedState;
28+
}
29+
30+
/**
31+
* Gets the stream name.
32+
*
33+
* @return The stream name.
34+
*/
35+
public String getStream() {
36+
return stream;
37+
}
38+
39+
/**
40+
* Gets the messages to append.
41+
*
42+
* @return The messages to append.
43+
*/
44+
public List<Message> getMessages() {
45+
return messages;
46+
}
47+
48+
/**
49+
* Gets the expected state of the stream.
50+
*
51+
* @return The expected state of the stream.
52+
*/
53+
public StreamState getExpectedState() {
54+
return expectedState;
55+
}
56+
57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o) return true;
60+
if (o == null || getClass() != o.getClass()) return false;
61+
AppendStreamRequest that = (AppendStreamRequest) o;
62+
return Objects.equals(stream, that.stream) &&
63+
Objects.equals(messages, that.messages) &&
64+
Objects.equals(expectedState, that.expectedState);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(stream, messages, expectedState);
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "AppendStreamRequest{" +
75+
"stream='" + stream + '\'' +
76+
", messages=" + messages +
77+
", expectedState=" + expectedState +
78+
'}';
79+
}
80+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
/**
7+
* Represents the result of appending to a stream, which can be either a success or a failure.
8+
*/
9+
public class AppendStreamResult {
10+
private final AppendStreamSuccess success;
11+
private final AppendStreamFailure failure;
12+
13+
private AppendStreamResult(AppendStreamSuccess success, AppendStreamFailure failure) {
14+
this.success = success;
15+
this.failure = failure;
16+
}
17+
18+
/**
19+
* Creates a new AppendStreamResult from a success.
20+
*
21+
* @param success The success result.
22+
* @return A new AppendStreamResult.
23+
*/
24+
public static AppendStreamResult fromSuccess(AppendStreamSuccess success) {
25+
if (success == null)
26+
throw new IllegalArgumentException("Success cannot be null");
27+
28+
return new AppendStreamResult(success, null);
29+
}
30+
31+
/**
32+
* Creates a new AppendStreamResult from a failure.
33+
*
34+
* @param failure The failure result.
35+
* @return A new AppendStreamResult.
36+
*/
37+
public static AppendStreamResult fromFailure(AppendStreamFailure failure) {
38+
if (failure == null)
39+
throw new IllegalArgumentException("Failure cannot be null");
40+
41+
return new AppendStreamResult(null, failure);
42+
}
43+
44+
/**
45+
* Checks if this result is a success.
46+
*
47+
* @return true if this result is a success, false otherwise.
48+
*/
49+
public boolean isSuccess() {
50+
return success != null;
51+
}
52+
53+
/**
54+
* Checks if this result is a failure.
55+
*
56+
* @return true if this result is a failure, false otherwise.
57+
*/
58+
public boolean isFailure() {
59+
return failure != null;
60+
}
61+
62+
/**
63+
* Gets the success result.
64+
*
65+
* @return The success result.
66+
* @throws IllegalStateException if this result is not a success.
67+
*/
68+
public AppendStreamSuccess getSuccess() {
69+
if (!isSuccess())
70+
throw new IllegalStateException("Result is not a success");
71+
72+
return success;
73+
}
74+
75+
/**
76+
* Gets the failure result.
77+
*
78+
* @return The failure result.
79+
* @throws IllegalStateException if this result is not a failure.
80+
*/
81+
public AppendStreamFailure getFailure() {
82+
if (!isFailure())
83+
throw new IllegalStateException("Result is not a failure");
84+
85+
return failure;
86+
}
87+
88+
/**
89+
* Folds the result to the appropriate action.
90+
*
91+
* @param successAction The action to perform if this result is a success.
92+
* @param failureAction The action to perform if this result is a failure.
93+
* @param <T> The type of the result.
94+
* @return The result of the action.
95+
*/
96+
public <T> T fold(Function<AppendStreamSuccess, T> successAction,
97+
Function<AppendStreamFailure, T> failureAction) {
98+
if (isSuccess())
99+
return successAction.apply(success);
100+
else
101+
return failureAction.apply(failure);
102+
}
103+
104+
@Override
105+
public boolean equals(Object o) {
106+
if (this == o) return true;
107+
if (o == null || getClass() != o.getClass()) return false;
108+
AppendStreamResult that = (AppendStreamResult) o;
109+
return Objects.equals(success, that.success) &&
110+
Objects.equals(failure, that.failure);
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
return Objects.hash(success, failure);
116+
}
117+
118+
@Override
119+
public String toString() {
120+
if (isSuccess())
121+
return "AppendStreamResult{success=" + success + '}';
122+
else
123+
return "AppendStreamResult{failure=" + failure + '}';
124+
}
125+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Represents a successful append to a stream.
7+
*/
8+
public class AppendStreamSuccess {
9+
private final String stream;
10+
private final long position;
11+
private final long streamRevision;
12+
13+
/**
14+
* Initializes a new instance of the AppendStreamSuccess class.
15+
*
16+
* @param stream The stream name.
17+
* @param position The position in the log.
18+
* @param streamRevision The stream revision.
19+
*/
20+
public AppendStreamSuccess(String stream, long position, long streamRevision) {
21+
this.stream = stream != null ? stream : "";
22+
this.position = position;
23+
this.streamRevision = streamRevision;
24+
}
25+
26+
/**
27+
* Gets the stream name.
28+
*
29+
* @return The stream name.
30+
*/
31+
public String getStream() {
32+
return stream;
33+
}
34+
35+
/**
36+
* Gets the position in the log.
37+
*
38+
* @return The position in the log.
39+
*/
40+
public long getPosition() {
41+
return position;
42+
}
43+
44+
/**
45+
* Gets the stream revision.
46+
*
47+
* @return The stream revision.
48+
*/
49+
public long getStreamRevision() {
50+
return streamRevision;
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) return true;
56+
if (o == null || getClass() != o.getClass()) return false;
57+
AppendStreamSuccess that = (AppendStreamSuccess) o;
58+
return position == that.position &&
59+
streamRevision == that.streamRevision &&
60+
Objects.equals(stream, that.stream);
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return Objects.hash(stream, position, streamRevision);
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "AppendStreamSuccess{" +
71+
"stream='" + stream + '\'' +
72+
", position=" + position +
73+
", streamRevision=" + streamRevision +
74+
'}';
75+
}
76+
}

0 commit comments

Comments
 (0)