44import java .util .Optional ;
55import java .util .concurrent .atomic .AtomicReference ;
66
7- public sealed interface VipsOption permits VipsOption .Int , VipsOption .Double , VipsOption .Long , VipsOption .Boolean , VipsOption .String , VipsOption .Image , VipsOption .Source , VipsOption .Target , VipsOption .Blob , VipsOption .ArrayDouble , VipsOption .ArrayInt , VipsOption .ArrayImage , VipsOption .Interpolate , VipsOption .Enum {
7+ public sealed interface VipsOption < T > permits VipsOption .Int , VipsOption .Double , VipsOption .Long , VipsOption .Boolean , VipsOption .String , VipsOption .Image , VipsOption .Source , VipsOption .Target , VipsOption .Blob , VipsOption .ArrayDouble , VipsOption .ArrayInt , VipsOption .ArrayImage , VipsOption .Interpolate , VipsOption .Enum {
88
99 java .lang .String key ();
10+
1011 boolean hasValue ();
1112
12- record Int ( java . lang . String key , AtomicReference < Optional <Integer >> box ) implements VipsOption {
13+ Optional <T > value ();
1314
14- public int valueOrThrow () throws VipsError {
15- var optionalValue = box .get ();
16- return optionalValue .orElseThrow (
17- () -> new VipsError ("unexpected empty value" )
18- );
19- }
15+ /**
16+ * Returns the value contained within the Optional, or throws a VipsError
17+ * if the Optional is empty.
18+ *
19+ * @return The value of type T.
20+ * @throws VipsError if the Optional is empty.
21+ */
22+ default T valueOrThrow () throws VipsError {
23+ Optional <T > optionalValue = value ();
24+ return optionalValue .orElseThrow (
25+ () -> new VipsError ("Unexpected empty value from " + this .getClass ().getSimpleName ())
26+ );
27+ }
28+
29+ record Int (java .lang .String key , AtomicReference <Optional <Integer >> box ) implements VipsOption <Integer > {
2030
2131 public boolean hasValue () {
2232 return box .get ().isPresent ();
2333 }
2434
35+ @ Override
36+ public Optional <Integer > value () {
37+ return box .get ();
38+ }
39+
2540 void setValue (int value ) {
2641 this .box .set (Optional .of (value ));
2742 }
2843 }
2944
30- record Double (java .lang .String key , AtomicReference <Optional <java .lang .Double >> box ) implements VipsOption {
45+ record Double (java .lang .String key ,
46+ AtomicReference <Optional <java .lang .Double >> box ) implements VipsOption <java .lang .Double > {
3147
32- public double valueOrThrow () throws VipsError {
33- var optionalValue = box .get ();
34- return optionalValue .orElseThrow (
35- () -> new VipsError ("unexpected empty value" )
36- );
48+ @ Override
49+ public Optional <java .lang .Double > value () {
50+ return box .get ();
3751 }
3852
3953 public boolean hasValue () {
@@ -45,13 +59,12 @@ void setValue(double value) {
4559 }
4660 }
4761
48- record Long (java .lang .String key , AtomicReference <Optional <java .lang .Long >> box ) implements VipsOption {
62+ record Long (java .lang .String key ,
63+ AtomicReference <Optional <java .lang .Long >> box ) implements VipsOption <java .lang .Long > {
4964
50- public java .lang .Long valueOrThrow () throws VipsError {
51- var optionalValue = box .get ();
52- return optionalValue .orElseThrow (
53- () -> new VipsError ("unexpected empty value" )
54- );
65+ @ Override
66+ public Optional <java .lang .Long > value () {
67+ return box .get ();
5568 }
5669
5770 public boolean hasValue () {
@@ -63,13 +76,12 @@ void setValue(java.lang.Long value) {
6376 }
6477 }
6578
66- record Boolean (java .lang .String key , AtomicReference <Optional <java .lang .Boolean >> box ) implements VipsOption {
79+ record Boolean (java .lang .String key ,
80+ AtomicReference <Optional <java .lang .Boolean >> box ) implements VipsOption <java .lang .Boolean > {
6781
68- public boolean valueOrThrow () throws VipsError {
69- var optionalValue = box .get ();
70- return optionalValue .orElseThrow (
71- () -> new VipsError ("unexpected empty value" )
72- );
82+ @ Override
83+ public Optional <java .lang .Boolean > value () {
84+ return box .get ();
7385 }
7486
7587 public boolean hasValue () {
@@ -81,13 +93,12 @@ void setValue(boolean value) {
8193 }
8294 }
8395
84- record String (java .lang .String key , AtomicReference <Optional <java .lang .String >> box ) implements VipsOption {
96+ record String (java .lang .String key ,
97+ AtomicReference <Optional <java .lang .String >> box ) implements VipsOption <java .lang .String > {
8598
86- public java .lang .String valueOrThrow () throws VipsError {
87- var optionalValue = box .get ();
88- return optionalValue .orElseThrow (
89- () -> new VipsError ("unexpected empty value" )
90- );
99+ @ Override
100+ public Optional <java .lang .String > value () {
101+ return box .get ();
91102 }
92103
93104 public boolean hasValue () {
@@ -99,13 +110,11 @@ void setValue(java.lang.String value) {
99110 }
100111 }
101112
102- record Image (java .lang .String key , AtomicReference <Optional <VImage >> box ) implements VipsOption {
113+ record Image (java .lang .String key , AtomicReference <Optional <VImage >> box ) implements VipsOption < VImage > {
103114
104- public VImage valueOrThrow () throws VipsError {
105- var optionalValue = box .get ();
106- return optionalValue .orElseThrow (
107- () -> new VipsError ("unexpected empty value" )
108- );
115+ @ Override
116+ public Optional <VImage > value () {
117+ return box .get ();
109118 }
110119
111120 public boolean hasValue () {
@@ -117,13 +126,11 @@ void setValue(VImage value) {
117126 }
118127 }
119128
120- record Source (java .lang .String key , AtomicReference <Optional <VSource >> box ) implements VipsOption {
129+ record Source (java .lang .String key , AtomicReference <Optional <VSource >> box ) implements VipsOption < VSource > {
121130
122- public VSource valueOrThrow () throws VipsError {
123- var optionalValue = box .get ();
124- return optionalValue .orElseThrow (
125- () -> new VipsError ("unexpected empty value" )
126- );
131+ @ Override
132+ public Optional <VSource > value () {
133+ return box .get ();
127134 }
128135
129136 public boolean hasValue () {
@@ -135,13 +142,11 @@ void setValue(VSource value) {
135142 }
136143 }
137144
138- record Target (java .lang .String key , AtomicReference <Optional <VTarget >> box ) implements VipsOption {
145+ record Target (java .lang .String key , AtomicReference <Optional <VTarget >> box ) implements VipsOption < VTarget > {
139146
140- public VTarget valueOrThrow () throws VipsError {
141- var optionalValue = box .get ();
142- return optionalValue .orElseThrow (
143- () -> new VipsError ("unexpected empty value" )
144- );
147+ @ Override
148+ public Optional <VTarget > value () {
149+ return box .get ();
145150 }
146151
147152 public boolean hasValue () {
@@ -153,13 +158,11 @@ void setValue(VTarget value) {
153158 }
154159 }
155160
156- record Blob (java .lang .String key , AtomicReference <Optional <VBlob >> box ) implements VipsOption {
161+ record Blob (java .lang .String key , AtomicReference <Optional <VBlob >> box ) implements VipsOption < VBlob > {
157162
158- public VBlob valueOrThrow () throws VipsError {
159- var optionalValue = box .get ();
160- return optionalValue .orElseThrow (
161- () -> new VipsError ("unexpected empty value" )
162- );
163+ @ Override
164+ public Optional <VBlob > value () {
165+ return box .get ();
163166 }
164167
165168 public boolean hasValue () {
@@ -171,13 +174,12 @@ void setValue(VBlob value) {
171174 }
172175 }
173176
174- record ArrayDouble (java .lang .String key , AtomicReference <Optional <List <java .lang .Double >>> box ) implements VipsOption {
177+ record ArrayDouble (java .lang .String key ,
178+ AtomicReference <Optional <List <java .lang .Double >>> box ) implements VipsOption <List <java .lang .Double >> {
175179
176- public List <java .lang .Double > valueOrThrow () throws VipsError {
177- var optionalValue = box .get ();
178- return optionalValue .orElseThrow (
179- () -> new VipsError ("unexpected empty value" )
180- );
180+ @ Override
181+ public Optional <List <java .lang .Double >> value () {
182+ return box .get ();
181183 }
182184
183185 public boolean hasValue () {
@@ -189,13 +191,12 @@ void setValue(List<java.lang.Double> value) {
189191 }
190192 }
191193
192- record ArrayInt (java .lang .String key , AtomicReference <Optional <List <Integer >>> box ) implements VipsOption {
194+ record ArrayInt (java .lang .String key ,
195+ AtomicReference <Optional <List <Integer >>> box ) implements VipsOption <List <Integer >> {
193196
194- public List <Integer > valueOrThrow () throws VipsError {
195- var optionalValue = box .get ();
196- return optionalValue .orElseThrow (
197- () -> new VipsError ("unexpected empty value" )
198- );
197+ @ Override
198+ public Optional <List <Integer >> value () {
199+ return box .get ();
199200 }
200201
201202 public boolean hasValue () {
@@ -207,13 +208,12 @@ void setValue(List<Integer> value) {
207208 }
208209 }
209210
210- record ArrayImage (java .lang .String key , AtomicReference <Optional <List <VImage >>> box ) implements VipsOption {
211+ record ArrayImage (java .lang .String key ,
212+ AtomicReference <Optional <List <VImage >>> box ) implements VipsOption <List <VImage >> {
211213
212- public List <VImage > valueOrThrow () throws VipsError {
213- var optionalValue = box .get ();
214- return optionalValue .orElseThrow (
215- () -> new VipsError ("unexpected empty value" )
216- );
214+ @ Override
215+ public Optional <List <VImage >> value () {
216+ return box .get ();
217217 }
218218
219219 public boolean hasValue () {
@@ -225,13 +225,12 @@ void setValue(List<VImage> value) {
225225 }
226226 }
227227
228- record Interpolate (java .lang .String key , AtomicReference <Optional <VInterpolate >> box ) implements VipsOption {
228+ record Interpolate (java .lang .String key ,
229+ AtomicReference <Optional <VInterpolate >> box ) implements VipsOption <VInterpolate > {
229230
230- public VInterpolate valueOrThrow () throws VipsError {
231- var optionalValue = box .get ();
232- return optionalValue .orElseThrow (
233- () -> new VipsError ("unexpected empty value" )
234- );
231+ @ Override
232+ public Optional <VInterpolate > value () {
233+ return box .get ();
235234 }
236235
237236 public boolean hasValue () {
@@ -243,13 +242,11 @@ void setValue(VInterpolate value) {
243242 }
244243 }
245244
246- record Enum (java .lang .String key , AtomicReference <Optional <VEnum >> box ) implements VipsOption {
245+ record Enum (java .lang .String key , AtomicReference <Optional <VEnum >> box ) implements VipsOption < VEnum > {
247246
248- public VEnum valueOrThrow () throws VipsError {
249- var optionalValue = box .get ();
250- return optionalValue .orElseThrow (
251- () -> new VipsError ("unexpected empty value" )
252- );
247+ @ Override
248+ public Optional <VEnum > value () {
249+ return box .get ();
253250 }
254251
255252 public boolean hasValue () {
0 commit comments