Skip to content

Commit d25b719

Browse files
author
ftirados
committed
Add javadoc to serverlessworkflow api
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 8364d2b commit d25b719

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

api/src/main/java/io/serverlessworkflow/api/WorkflowFormat.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,60 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import java.nio.file.Path;
2020

21+
/**
22+
* Enum representing the supported formats for Serverless Workflow definitions.
23+
* <p>
24+
* Provides utility methods to determine the format based on file name or path,
25+
* and to access the corresponding {@link ObjectMapper} for serialization and deserialization.
26+
*/
2127
public enum WorkflowFormat {
28+
/**
29+
* JSON format for workflow definitions.
30+
*/
2231
JSON(ObjectMapperFactory.jsonMapper()),
32+
33+
/**
34+
* YAML format for workflow definitions.
35+
*/
2336
YAML(ObjectMapperFactory.yamlMapper());
2437

2538
private final ObjectMapper mapper;
2639

40+
/**
41+
* Determines the {@link WorkflowFormat} from a file path by inspecting its file extension.
42+
*
43+
* @param path the file path to inspect
44+
* @return the corresponding {@link WorkflowFormat}
45+
*/
2746
public static WorkflowFormat fromPath(Path path) {
2847
return fromFileName(path.getFileName().toString());
2948
}
3049

50+
/**
51+
* Determines the {@link WorkflowFormat} from a file name by inspecting its extension.
52+
* Returns {@code JSON} if the file name ends with ".json", otherwise returns {@code YAML}.
53+
*
54+
* @param fileName the file name to inspect
55+
* @return the corresponding {@link WorkflowFormat}
56+
*/
3157
public static WorkflowFormat fromFileName(String fileName) {
3258
return fileName.endsWith(".json") ? JSON : YAML;
3359
}
3460

61+
/**
62+
* Constructs a {@link WorkflowFormat} with the specified {@link ObjectMapper}.
63+
*
64+
* @param mapper the object mapper for this format
65+
*/
3566
private WorkflowFormat(ObjectMapper mapper) {
3667
this.mapper = mapper;
3768
}
3869

70+
/**
71+
* Returns the {@link ObjectMapper} associated with this workflow format.
72+
*
73+
* @return the object mapper for this format
74+
*/
3975
public ObjectMapper mapper() {
4076
return mapper;
4177
}

api/src/main/java/io/serverlessworkflow/api/WorkflowReader.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,143 @@
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
2525

26+
/**
27+
* Utility class for reading and parsing Serverless Workflow definitions from various sources.
28+
*/
2629
public class WorkflowReader {
2730

31+
/**
32+
* Reads a workflow from an {@link InputStream} using the specified format.
33+
*
34+
* @param input the input stream containing the workflow definition
35+
* @param format the workflow format
36+
* @return the parsed {@link Workflow}
37+
* @throws IOException if an I/O error occurs
38+
*/
2839
public static Workflow readWorkflow(InputStream input, WorkflowFormat format) throws IOException {
2940
return defaultReader().read(input, format);
3041
}
3142

43+
/**
44+
* Reads a workflow from a {@link Reader} using the specified format.
45+
*
46+
* @param input the reader containing the workflow definition
47+
* @param format the workflow format
48+
* @return the parsed {@link Workflow}
49+
* @throws IOException if an I/O error occurs
50+
*/
3251
public static Workflow readWorkflow(Reader input, WorkflowFormat format) throws IOException {
3352
return defaultReader().read(input, format);
3453
}
3554

55+
/**
56+
* Reads a workflow from a byte array using the specified format.
57+
*
58+
* @param input the byte array containing the workflow definition
59+
* @param format the workflow format
60+
* @return the parsed {@link Workflow}
61+
* @throws IOException if an I/O error occurs
62+
*/
3663
public static Workflow readWorkflow(byte[] input, WorkflowFormat format) throws IOException {
3764
return defaultReader().read(input, format);
3865
}
3966

67+
/**
68+
* Reads a workflow from a file path, inferring the format from the file extension.
69+
*
70+
* @param path the path to the workflow file
71+
* @return the parsed {@link Workflow}
72+
* @throws IOException if an I/O error occurs
73+
*/
4074
public static Workflow readWorkflow(Path path) throws IOException {
4175
return readWorkflow(path, WorkflowFormat.fromPath(path), defaultReader());
4276
}
4377

78+
/**
79+
* Reads a workflow from a file path using the specified format.
80+
*
81+
* @param path the path to the workflow file
82+
* @param format the workflow format
83+
* @return the parsed {@link Workflow}
84+
* @throws IOException if an I/O error occurs
85+
*/
4486
public static Workflow readWorkflow(Path path, WorkflowFormat format) throws IOException {
4587
return readWorkflow(path, format, defaultReader());
4688
}
4789

90+
/**
91+
* Reads a workflow from a string using the specified format.
92+
*
93+
* @param input the string containing the workflow definition
94+
* @param format the workflow format
95+
* @return the parsed {@link Workflow}
96+
* @throws IOException if an I/O error occurs
97+
*/
4898
public static Workflow readWorkflowFromString(String input, WorkflowFormat format)
4999
throws IOException {
50100
return defaultReader().read(input, format);
51101
}
52102

103+
/**
104+
* Reads a workflow from the classpath, inferring the format from the file name.
105+
*
106+
* @param classpath the classpath location of the workflow file
107+
* @return the parsed {@link Workflow}
108+
* @throws IOException if an I/O error occurs
109+
*/
53110
public static Workflow readWorkflowFromClasspath(String classpath) throws IOException {
54111
return readWorkflowFromClasspath(classpath, defaultReader());
55112
}
56113

114+
/**
115+
* Reads a workflow from the classpath using the specified class loader and format.
116+
*
117+
* @param classpath the classpath location of the workflow file
118+
* @param cl the class loader to use
119+
* @param format the workflow format
120+
* @return the parsed {@link Workflow}
121+
* @throws IOException if an I/O error occurs
122+
*/
57123
public static Workflow readWorkflowFromClasspath(
58124
String classpath, ClassLoader cl, WorkflowFormat format) throws IOException {
59125
return readWorkflowFromClasspath(classpath, defaultReader());
60126
}
61127

128+
/**
129+
* Reads a workflow from a file path using a custom reader.
130+
*
131+
* @param path the path to the workflow file
132+
* @param reader the custom {@link WorkflowReaderOperations}
133+
* @return the parsed {@link Workflow}
134+
* @throws IOException if an I/O error occurs
135+
*/
62136
public static Workflow readWorkflow(Path path, WorkflowReaderOperations reader)
63137
throws IOException {
64138
return readWorkflow(path, WorkflowFormat.fromPath(path), reader);
65139
}
66140

141+
/**
142+
* Reads a workflow from a file path using the specified format and custom reader.
143+
*
144+
* @param path the path to the workflow file
145+
* @param format the workflow format
146+
* @param reader the custom {@link WorkflowReaderOperations}
147+
* @return the parsed {@link Workflow}
148+
* @throws IOException if an I/O error occurs
149+
*/
67150
public static Workflow readWorkflow(
68151
Path path, WorkflowFormat format, WorkflowReaderOperations reader) throws IOException {
69152
return reader.read(Files.readAllBytes(path), format);
70153
}
71154

155+
/**
156+
* Reads a workflow from the classpath using a custom reader.
157+
*
158+
* @param classpath the classpath location of the workflow file
159+
* @param reader the custom {@link WorkflowReaderOperations}
160+
* @return the parsed {@link Workflow}
161+
* @throws IOException if an I/O error occurs
162+
*/
72163
public static Workflow readWorkflowFromClasspath(
73164
String classpath, WorkflowReaderOperations reader) throws IOException {
74165
return readWorkflowFromClasspath(
@@ -78,6 +169,16 @@ public static Workflow readWorkflowFromClasspath(
78169
reader);
79170
}
80171

172+
/**
173+
* Reads a workflow from the classpath using the specified class loader, format, and custom reader.
174+
*
175+
* @param classpath the classpath location of the workflow file
176+
* @param cl the class loader to use
177+
* @param format the workflow format
178+
* @param reader the custom {@link WorkflowReaderOperations}
179+
* @return the parsed {@link Workflow}
180+
* @throws IOException if an I/O error occurs or the resource is not found
181+
*/
81182
public static Workflow readWorkflowFromClasspath(
82183
String classpath, ClassLoader cl, WorkflowFormat format, WorkflowReaderOperations reader)
83184
throws IOException {
@@ -89,10 +190,20 @@ public static Workflow readWorkflowFromClasspath(
89190
}
90191
}
91192

193+
/**
194+
* Returns a {@link WorkflowReaderOperations} instance that performs no validation.
195+
*
196+
* @return a no-validation reader
197+
*/
92198
public static WorkflowReaderOperations noValidation() {
93199
return NoValidationHolder.instance;
94200
}
95201

202+
/**
203+
* Returns a {@link WorkflowReaderOperations} instance that performs validation.
204+
*
205+
* @return a validation reader
206+
*/
96207
public static WorkflowReaderOperations validation() {
97208
return ValidationHolder.instance;
98209
}
@@ -105,6 +216,11 @@ private static class ValidationHolder {
105216
private static final WorkflowReaderOperations instance = new ValidationReader();
106217
}
107218

219+
/**
220+
* Returns the default {@link WorkflowReaderOperations} instance (no validation).
221+
*
222+
* @return the default reader
223+
*/
108224
private static WorkflowReaderOperations defaultReader() {
109225
return NoValidationHolder.instance;
110226
}

api/src/main/java/io/serverlessworkflow/api/WorkflowWriter.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,88 @@
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
2525

26+
/**
27+
* Utility class for writing Serverless Workflow definitions to various outputs and formats.
28+
*
29+
* <p>This class provides static methods to serialize {@link Workflow} objects to files,
30+
* streams, writers, byte arrays, or strings in either JSON or YAML format.
31+
* The format is determined by the {@link WorkflowFormat} parameter or inferred from file extensions.
32+
*/
2633
public class WorkflowWriter {
2734

35+
/**
36+
* Writes a {@link Workflow} to the given {@link OutputStream} in the specified format.
37+
*
38+
* @param output the output stream to write the workflow to
39+
* @param workflow the workflow object to serialize
40+
* @param format the format to use (JSON or YAML)
41+
* @throws IOException if an I/O error occurs during writing
42+
*/
2843
public static void writeWorkflow(OutputStream output, Workflow workflow, WorkflowFormat format)
2944
throws IOException {
3045
format.mapper().writeValue(output, workflow);
3146
}
3247

48+
/**
49+
* Writes a {@link Workflow} to the given {@link Writer} in the specified format.
50+
*
51+
* @param output the writer to write the workflow to
52+
* @param workflow the workflow object to serialize
53+
* @param format the format to use (JSON or YAML)
54+
* @throws IOException if an I/O error occurs during writing
55+
*/
3356
public static void writeWorkflow(Writer output, Workflow workflow, WorkflowFormat format)
3457
throws IOException {
3558
format.mapper().writeValue(output, workflow);
3659
}
3760

61+
/**
62+
* Writes a {@link Workflow} to the specified file path, inferring the format from the file extension.
63+
*
64+
* @param output the file path to write the workflow to
65+
* @param workflow the workflow object to serialize
66+
* @throws IOException if an I/O error occurs during writing
67+
*/
3868
public static void writeWorkflow(Path output, Workflow workflow) throws IOException {
3969
writeWorkflow(output, workflow, WorkflowFormat.fromPath(output));
4070
}
4171

72+
/**
73+
* Writes a {@link Workflow} to the specified file path in the given format.
74+
*
75+
* @param output the file path to write the workflow to
76+
* @param workflow the workflow object to serialize
77+
* @param format the format to use (JSON or YAML)
78+
* @throws IOException if an I/O error occurs during writing
79+
*/
4280
public static void writeWorkflow(Path output, Workflow workflow, WorkflowFormat format)
4381
throws IOException {
4482
try (OutputStream out = Files.newOutputStream(output)) {
4583
writeWorkflow(out, workflow, format);
4684
}
4785
}
4886

87+
/**
88+
* Serializes a {@link Workflow} to a string in the specified format.
89+
*
90+
* @param workflow the workflow object to serialize
91+
* @param format the format to use (JSON or YAML)
92+
* @return the serialized workflow as a string
93+
* @throws IOException if an error occurs during serialization
94+
*/
4995
public static String workflowAsString(Workflow workflow, WorkflowFormat format)
5096
throws IOException {
5197
return format.mapper().writeValueAsString(workflow);
5298
}
5399

100+
/**
101+
* Serializes a {@link Workflow} to a byte array in the specified format.
102+
*
103+
* @param workflow the workflow object to serialize
104+
* @param format the format to use (JSON or YAML)
105+
* @return the serialized workflow as a byte array
106+
* @throws IOException if an error occurs during serialization
107+
*/
54108
public static byte[] workflowAsBytes(Workflow workflow, WorkflowFormat format)
55109
throws IOException {
56110
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
@@ -59,5 +113,6 @@ public static byte[] workflowAsBytes(Workflow workflow, WorkflowFormat format)
59113
}
60114
}
61115

116+
// Private constructor to prevent instantiation
62117
private WorkflowWriter() {}
63118
}

0 commit comments

Comments
 (0)