forked from VapiAI/server-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.java
More file actions
97 lines (91 loc) · 3.52 KB
/
Copy pathStream.java
File metadata and controls
97 lines (91 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.vapi.api.core;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* The {@code Stream} class implmenets {@link Iterable} to provide a simple mechanism for reading and parsing
* objects of a given type from data streamed via a {@link Reader} using a specified delimiter.
* <p>
* {@code Stream} assumes that data is being pushed to the provided {@link Reader} asynchronously and utilizes a
* {@code Scanner} to block during iteration if the next object is not available.
*
* @param <T> The type of objects in the stream.
*/
public final class Stream<T> implements Iterable<T> {
/**
* The {@link Class} of the objects in the stream.
*/
private final Class<T> valueType;
/**
* The {@link Scanner} used for reading from the input stream and blocking when neede during iteration.
*/
private final Scanner scanner;
/**
* Constructs a new {@code Stream} with the specified value type, reader, and delimiter.
*
* @param valueType The class of the objects in the stream.
* @param reader The reader that provides the streamed data.
* @param delimiter The delimiter used to separate elements in the stream.
*/
public Stream(Class<T> valueType, Reader reader, String delimiter) {
this.scanner = new Scanner(reader).useDelimiter(delimiter);
this.valueType = valueType;
}
/**
* Returns an iterator over the elements in this stream that blocks during iteration when the next object is
* not yet available.
*
* @return An iterator that can be used to traverse the elements in the stream.
*/
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
/**
* Returns {@code true} if there are more elements in the stream.
* <p>
* Will block and wait for input if the stream has not ended and the next object is not yet available.
*
* @return {@code true} if there are more elements, {@code false} otherwise.
*/
@Override
public boolean hasNext() {
return scanner.hasNext();
}
/**
* Returns the next element in the stream.
* <p>
* Will block and wait for input if the stream has not ended and the next object is not yet available.
*
* @return The next element in the stream.
* @throws NoSuchElementException If there are no more elements in the stream.
*/
@Override
public T next() {
if (!scanner.hasNext()) {
throw new NoSuchElementException();
} else {
try {
T parsedResponse = ObjectMappers.JSON_MAPPER.readValue(
scanner.next().trim(), valueType);
return parsedResponse;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
/**
* Removing elements from {@code Stream} is not supported.
*
* @throws UnsupportedOperationException Always, as removal is not supported.
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}