Skip to content

Commit 0b61c58

Browse files
committed
refactoring code
1 parent 6752358 commit 0b61c58

File tree

2 files changed

+48
-61
lines changed

2 files changed

+48
-61
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Adding a new method of constructing ARNs without exceptions as control flow"
6+
}

core/arns/src/main/java/software/amazon/awssdk/arns/Arn.java

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -149,112 +149,93 @@ public static Builder builder() {
149149
*
150150
* @param arn A string containing an ARN to parse.
151151
* @return An {@link Optional} containing the parsed {@link Arn} if valid, or empty if invalid.
152+
* @throws IllegalArgumentException if the ARN contains empty partition or service fields
152153
*/
153154
public static Optional<Arn> tryFromString(String arn) {
155+
return parseArn(arn, false);
156+
}
157+
158+
/**
159+
* Parses a given string into an {@link Arn}. The resource is accessible entirely as a
160+
* string through {@link #resourceAsString()}. Where correctly formatted, a parsed
161+
* resource containing resource type, resource and qualifier is available through
162+
* {@link #resource()}.
163+
*
164+
* @param arn - A string containing an Arn.
165+
* @return {@link Arn} - A modeled Arn.
166+
*/
167+
public static Arn fromString(String arn) {
168+
return parseArn(arn, true).orElseThrow(() -> new IllegalArgumentException("ARN parsing failed"));
169+
}
170+
171+
private static Optional<Arn> parseArn(String arn, boolean throwOnError) {
154172
if (arn == null) {
155173
return Optional.empty();
156174
}
157175

158176
int arnColonIndex = arn.indexOf(':');
159177
if (arnColonIndex < 0 || !"arn".equals(arn.substring(0, arnColonIndex))) {
178+
if (throwOnError) {
179+
throw new IllegalArgumentException("Malformed ARN - doesn't start with 'arn:'");
180+
}
160181
return Optional.empty();
161182
}
162183

163184
int partitionColonIndex = arn.indexOf(':', arnColonIndex + 1);
164185
if (partitionColonIndex < 0) {
186+
if (throwOnError) {
187+
throw new IllegalArgumentException("Malformed ARN - no AWS partition specified");
188+
}
165189
return Optional.empty();
166190
}
167-
168191
String partition = arn.substring(arnColonIndex + 1, partitionColonIndex);
169192

170193
int serviceColonIndex = arn.indexOf(':', partitionColonIndex + 1);
171194
if (serviceColonIndex < 0) {
195+
if (throwOnError) {
196+
throw new IllegalArgumentException("Malformed ARN - no service specified");
197+
}
172198
return Optional.empty();
173199
}
174200
String service = arn.substring(partitionColonIndex + 1, serviceColonIndex);
175201

176202
int regionColonIndex = arn.indexOf(':', serviceColonIndex + 1);
177203
if (regionColonIndex < 0) {
204+
if (throwOnError) {
205+
throw new IllegalArgumentException("Malformed ARN - no AWS region partition specified");
206+
}
178207
return Optional.empty();
179208
}
180209
String region = arn.substring(serviceColonIndex + 1, regionColonIndex);
181210

182211
int accountColonIndex = arn.indexOf(':', regionColonIndex + 1);
183212
if (accountColonIndex < 0) {
213+
if (throwOnError) {
214+
throw new IllegalArgumentException("Malformed ARN - no AWS account specified");
215+
}
184216
return Optional.empty();
185217
}
186218
String accountId = arn.substring(regionColonIndex + 1, accountColonIndex);
187219

188220
String resource = arn.substring(accountColonIndex + 1);
189221
if (resource.isEmpty()) {
222+
if (throwOnError) {
223+
throw new IllegalArgumentException("Malformed ARN - no resource specified");
224+
}
190225
return Optional.empty();
191226
}
192227

193-
194228
Arn resultArn = builder()
195-
.partition(partition)
196-
.service(service)
197-
.region(region)
198-
.accountId(accountId)
199-
.resource(resource)
200-
.build();
229+
.partition(partition)
230+
.service(service)
231+
.region(region)
232+
.accountId(accountId)
233+
.resource(resource)
234+
.build();
201235

202236
return Optional.of(resultArn);
203237
}
204238

205-
/**
206-
* Parses a given string into an {@link Arn}. The resource is accessible entirely as a
207-
* string through {@link #resourceAsString()}. Where correctly formatted, a parsed
208-
* resource containing resource type, resource and qualifier is available through
209-
* {@link #resource()}.
210-
*
211-
* @param arn - A string containing an Arn.
212-
* @return {@link Arn} - A modeled Arn.
213-
*/
214-
public static Arn fromString(String arn) {
215-
int arnColonIndex = arn.indexOf(':');
216-
if (arnColonIndex < 0 || !"arn".equals(arn.substring(0, arnColonIndex))) {
217-
throw new IllegalArgumentException("Malformed ARN - doesn't start with 'arn:'");
218-
}
219-
220-
int partitionColonIndex = arn.indexOf(':', arnColonIndex + 1);
221-
if (partitionColonIndex < 0) {
222-
throw new IllegalArgumentException("Malformed ARN - no AWS partition specified");
223-
}
224-
String partition = arn.substring(arnColonIndex + 1, partitionColonIndex);
225-
226-
int serviceColonIndex = arn.indexOf(':', partitionColonIndex + 1);
227-
if (serviceColonIndex < 0) {
228-
throw new IllegalArgumentException("Malformed ARN - no service specified");
229-
}
230-
String service = arn.substring(partitionColonIndex + 1, serviceColonIndex);
231-
232-
int regionColonIndex = arn.indexOf(':', serviceColonIndex + 1);
233-
if (regionColonIndex < 0) {
234-
throw new IllegalArgumentException("Malformed ARN - no AWS region partition specified");
235-
}
236-
String region = arn.substring(serviceColonIndex + 1, regionColonIndex);
237-
238-
int accountColonIndex = arn.indexOf(':', regionColonIndex + 1);
239-
if (accountColonIndex < 0) {
240-
throw new IllegalArgumentException("Malformed ARN - no AWS account specified");
241-
}
242-
String accountId = arn.substring(regionColonIndex + 1, accountColonIndex);
243-
244-
String resource = arn.substring(accountColonIndex + 1);
245-
if (resource.isEmpty()) {
246-
throw new IllegalArgumentException("Malformed ARN - no resource specified");
247-
}
248-
249-
return Arn.builder()
250-
.partition(partition)
251-
.service(service)
252-
.region(region)
253-
.accountId(accountId)
254-
.resource(resource)
255-
.build();
256-
}
257-
258239
@Override
259240
public String toString() {
260241
return "arn:"

0 commit comments

Comments
 (0)