Skip to content

Commit 81cf27a

Browse files
committed
Add convenience methods for fetching optional values
1 parent 9bb661d commit 81cf27a

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

asdf-core/src/main/java/org/asdfformat/asdf/node/AsdfNode.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.Instant;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.Optional;
1011

1112

1213
/**
@@ -138,6 +139,34 @@ public interface AsdfNode extends Iterable<AsdfNode> {
138139
*/
139140
AsdfNode get(AsdfNode key);
140141

142+
/**
143+
* Get an optional mapping value as AsdfNode, indexed by String key.
144+
* @param key mapping key
145+
* @return value
146+
*/
147+
Optional<AsdfNode> getOptional(String key);
148+
149+
/**
150+
* Get an optional sequence value or mapping value as AsdfNode, indexed by long key.
151+
* @param key sequence index or mapping key
152+
* @return value
153+
*/
154+
Optional<AsdfNode> getOptional(long key);
155+
156+
/**
157+
* Get an optional mapping value as AsdfNode, indexed by boolean key.
158+
* @param key mapping key
159+
* @return value
160+
*/
161+
Optional<AsdfNode> getOptional(boolean key);
162+
163+
/**
164+
* Get an optional mapping value as AsdfNode, indexed by AsdfNode key.
165+
* @param key mapping key
166+
* @return value
167+
*/
168+
Optional<AsdfNode> getOptional(AsdfNode key);
169+
141170
/**
142171
* Get a NUMBER mapping value as BigDecimal, indexed by String key.
143172
* @param key mapping key
@@ -166,6 +195,34 @@ public interface AsdfNode extends Iterable<AsdfNode> {
166195
*/
167196
BigDecimal getBigDecimal(AsdfNode key);
168197

198+
/**
199+
* Get a NUMBER mapping value as BigDecimal, indexed by String key.
200+
* @param key mapping key
201+
* @return value
202+
*/
203+
Optional<BigDecimal> getBigDecimalOptional(String key);
204+
205+
/**
206+
* Get a NUMBER sequence value or mapping value as BigDecimal, indexed by long key.
207+
* @param key sequence index or mapping key
208+
* @return value
209+
*/
210+
Optional<BigDecimal> getBigDecimalOptional(long key);
211+
212+
/**
213+
* Get a NUMBER mapping value as BigDecimal, indexed by boolean key.
214+
* @param key mapping key
215+
* @return value
216+
*/
217+
Optional<BigDecimal> getBigDecimalOptional(boolean key);
218+
219+
/**
220+
* Get a NUMBER sequence value or mapping value as BigDecimal, indexed by AsdfNode key.
221+
* @param key mapping key
222+
* @return value
223+
*/
224+
Optional<BigDecimal> getBigDecimalOptional(AsdfNode key);
225+
169226
/**
170227
* Get a NUMBER mapping value as BigInteger, indexed by String key.
171228
* @param key mapping key

asdf-core/src/main/java/org/asdfformat/asdf/node/impl/AsdfNodeBase.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Iterator;
1212
import java.util.List;
1313
import java.util.Map;
14+
import java.util.Optional;
1415

1516
public abstract class AsdfNodeBase implements AsdfNode {
1617

@@ -99,6 +100,42 @@ public AsdfNode get(final AsdfNode key) {
99100
throw new IllegalStateException(makeGetErrorMessage("AsdfNode"));
100101
}
101102

103+
@Override
104+
public Optional<AsdfNode> getOptional(final String key) {
105+
if (containsKey(key)) {
106+
return Optional.empty();
107+
} else {
108+
return Optional.of(get(key));
109+
}
110+
}
111+
112+
@Override
113+
public Optional<AsdfNode> getOptional(final long key) {
114+
if (containsKey(key)) {
115+
return Optional.empty();
116+
} else {
117+
return Optional.of(get(key));
118+
}
119+
}
120+
121+
@Override
122+
public Optional<AsdfNode> getOptional(final boolean key) {
123+
if (containsKey(key)) {
124+
return Optional.empty();
125+
} else {
126+
return Optional.of(get(key));
127+
}
128+
}
129+
130+
@Override
131+
public Optional<AsdfNode> getOptional(final AsdfNode key) {
132+
if (containsKey(key)) {
133+
return Optional.empty();
134+
} else {
135+
return Optional.of(get(key));
136+
}
137+
}
138+
102139
@Override
103140
public BigDecimal getBigDecimal(final String key) {
104141
return get(key).asBigDecimal();

0 commit comments

Comments
 (0)