Skip to content

Commit 797e0c7

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

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,56 @@
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+
*
24+
* <p>Provides utility methods to determine the format based on file name or path, and to access the
25+
* corresponding {@link ObjectMapper} for serialization and deserialization.
26+
*/
2127
public enum WorkflowFormat {
28+
/** JSON format for workflow definitions. */
2229
JSON(ObjectMapperFactory.jsonMapper()),
30+
31+
/** YAML format for workflow definitions. */
2332
YAML(ObjectMapperFactory.yamlMapper());
2433

2534
private final ObjectMapper mapper;
2635

36+
/**
37+
* Determines the {@link WorkflowFormat} from a file path by inspecting its file extension.
38+
*
39+
* @param path the file path to inspect
40+
* @return the corresponding {@link WorkflowFormat}
41+
*/
2742
public static WorkflowFormat fromPath(Path path) {
2843
return fromFileName(path.getFileName().toString());
2944
}
3045

46+
/**
47+
* Determines the {@link WorkflowFormat} from a file name by inspecting its extension. Returns
48+
* {@code JSON} if the file name ends with ".json", otherwise returns {@code YAML}.
49+
*
50+
* @param fileName the file name to inspect
51+
* @return the corresponding {@link WorkflowFormat}
52+
*/
3153
public static WorkflowFormat fromFileName(String fileName) {
3254
return fileName.endsWith(".json") ? JSON : YAML;
3355
}
3456

57+
/**
58+
* Constructs a {@link WorkflowFormat} with the specified {@link ObjectMapper}.
59+
*
60+
* @param mapper the object mapper for this format
61+
*/
3562
private WorkflowFormat(ObjectMapper mapper) {
3663
this.mapper = mapper;
3764
}
3865

66+
/**
67+
* Returns the {@link ObjectMapper} associated with this workflow format.
68+
*
69+
* @return the object mapper for this format
70+
*/
3971
public ObjectMapper mapper() {
4072
return mapper;
4173
}

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

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

26+
/** Utility class for reading and parsing Serverless Workflow definitions from various sources. */
2627
public class WorkflowReader {
2728

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

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

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

65+
/**
66+
* Reads a workflow from a file path, inferring the format from the file extension.
67+
*
68+
* @param path the path to the workflow file
69+
* @return the parsed {@link Workflow}
70+
* @throws IOException if an I/O error occurs
71+
*/
4072
public static Workflow readWorkflow(Path path) throws IOException {
4173
return readWorkflow(path, WorkflowFormat.fromPath(path), defaultReader());
4274
}
4375

76+
/**
77+
* Reads a workflow from a file path using the specified format.
78+
*
79+
* @param path the path to the workflow file
80+
* @param format the workflow format
81+
* @return the parsed {@link Workflow}
82+
* @throws IOException if an I/O error occurs
83+
*/
4484
public static Workflow readWorkflow(Path path, WorkflowFormat format) throws IOException {
4585
return readWorkflow(path, format, defaultReader());
4686
}
4787

88+
/**
89+
* Reads a workflow from a string using the specified format.
90+
*
91+
* @param input the string containing the workflow definition
92+
* @param format the workflow format
93+
* @return the parsed {@link Workflow}
94+
* @throws IOException if an I/O error occurs
95+
*/
4896
public static Workflow readWorkflowFromString(String input, WorkflowFormat format)
4997
throws IOException {
5098
return defaultReader().read(input, format);
5199
}
52100

101+
/**
102+
* Reads a workflow from the classpath, inferring the format from the file name.
103+
*
104+
* @param classpath the classpath location of the workflow file
105+
* @return the parsed {@link Workflow}
106+
* @throws IOException if an I/O error occurs
107+
*/
53108
public static Workflow readWorkflowFromClasspath(String classpath) throws IOException {
54109
return readWorkflowFromClasspath(classpath, defaultReader());
55110
}
56111

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

126+
/**
127+
* Reads a workflow from a file path using a custom reader.
128+
*
129+
* @param path the path to the workflow file
130+
* @param reader the custom {@link WorkflowReaderOperations}
131+
* @return the parsed {@link Workflow}
132+
* @throws IOException if an I/O error occurs
133+
*/
62134
public static Workflow readWorkflow(Path path, WorkflowReaderOperations reader)
63135
throws IOException {
64136
return readWorkflow(path, WorkflowFormat.fromPath(path), reader);
65137
}
66138

139+
/**
140+
* Reads a workflow from a file path using the specified format and custom reader.
141+
*
142+
* @param path the path to the workflow file
143+
* @param format the workflow format
144+
* @param reader the custom {@link WorkflowReaderOperations}
145+
* @return the parsed {@link Workflow}
146+
* @throws IOException if an I/O error occurs
147+
*/
67148
public static Workflow readWorkflow(
68149
Path path, WorkflowFormat format, WorkflowReaderOperations reader) throws IOException {
69150
return reader.read(Files.readAllBytes(path), format);
70151
}
71152

153+
/**
154+
* Reads a workflow from the classpath using a custom reader.
155+
*
156+
* @param classpath the classpath location of the workflow file
157+
* @param reader the custom {@link WorkflowReaderOperations}
158+
* @return the parsed {@link Workflow}
159+
* @throws IOException if an I/O error occurs
160+
*/
72161
public static Workflow readWorkflowFromClasspath(
73162
String classpath, WorkflowReaderOperations reader) throws IOException {
74163
return readWorkflowFromClasspath(
@@ -78,6 +167,17 @@ public static Workflow readWorkflowFromClasspath(
78167
reader);
79168
}
80169

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

192+
/**
193+
* Returns a {@link WorkflowReaderOperations} instance that performs no validation.
194+
*
195+
* @return a no-validation reader
196+
*/
92197
public static WorkflowReaderOperations noValidation() {
93198
return NoValidationHolder.instance;
94199
}
95200

201+
/**
202+
* Returns a {@link WorkflowReaderOperations} instance that performs validation.
203+
*
204+
* @return a validation reader
205+
*/
96206
public static WorkflowReaderOperations validation() {
97207
return ValidationHolder.instance;
98208
}
@@ -105,6 +215,11 @@ private static class ValidationHolder {
105215
private static final WorkflowReaderOperations instance = new ValidationReader();
106216
}
107217

218+
/**
219+
* Returns the default {@link WorkflowReaderOperations} instance (no validation).
220+
*
221+
* @return the default reader
222+
*/
108223
private static WorkflowReaderOperations defaultReader() {
109224
return NoValidationHolder.instance;
110225
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,89 @@
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, streams,
30+
* writers, byte arrays, or strings in either JSON or YAML format. The format is determined by the
31+
* {@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
63+
* extension.
64+
*
65+
* @param output the file path to write the workflow to
66+
* @param workflow the workflow object to serialize
67+
* @throws IOException if an I/O error occurs during writing
68+
*/
3869
public static void writeWorkflow(Path output, Workflow workflow) throws IOException {
3970
writeWorkflow(output, workflow, WorkflowFormat.fromPath(output));
4071
}
4172

73+
/**
74+
* Writes a {@link Workflow} to the specified file path in the given format.
75+
*
76+
* @param output the file path to write the workflow to
77+
* @param workflow the workflow object to serialize
78+
* @param format the format to use (JSON or YAML)
79+
* @throws IOException if an I/O error occurs during writing
80+
*/
4281
public static void writeWorkflow(Path output, Workflow workflow, WorkflowFormat format)
4382
throws IOException {
4483
try (OutputStream out = Files.newOutputStream(output)) {
4584
writeWorkflow(out, workflow, format);
4685
}
4786
}
4887

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

101+
/**
102+
* Serializes a {@link Workflow} to a byte array in the specified format.
103+
*
104+
* @param workflow the workflow object to serialize
105+
* @param format the format to use (JSON or YAML)
106+
* @return the serialized workflow as a byte array
107+
* @throws IOException if an error occurs during serialization
108+
*/
54109
public static byte[] workflowAsBytes(Workflow workflow, WorkflowFormat format)
55110
throws IOException {
56111
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
@@ -59,5 +114,6 @@ public static byte[] workflowAsBytes(Workflow workflow, WorkflowFormat format)
59114
}
60115
}
61116

117+
// Private constructor to prevent instantiation
62118
private WorkflowWriter() {}
63119
}

0 commit comments

Comments
 (0)