aws / aws/aws-sdk-java-v2

Comparable Arns

Open
#2,955 1 comment 0 reactions 0 assignees View on GitHub
feature-request p3
Dominant language
Java
Stars
2.6k
Forks
1k
Avg merge
2d 9h
Merged PRs (30d)
51

Description

### Describe the feature

It'd be nice if [Arn](https://github.com/aws/aws-sdk-java-v2/blob/master/core/arns/src/main/java/software/amazon/awssdk/arns/Arn.java) implemented `Comparable`.

This would make Arns usable in sorted collection types, etc.

### Is your Feature Request related to a problem?

Not really.
- We can use `HashMap` instead of `TreeMap`, etc.
- We can sort Arns by converting to strings, sorting, then converting back to Arns.

So this would just make Arns more flexible.

### Proposed Solution

Here is a straightforward implementation:

public static final Comparator COMPARATOR =
Comparator.comparing(Arn::toString);

@Override
public int compareTo(Arn rhs) {
return COMPARATOR.compare(this, rhs);
}

Here is a more elaborate one, which may be more efficient by avoiding the String construction:

public static final Comparator COMPARATOR =
Comparator.comparing(Arn::partition)
.thenComparing(Arn::service)
.thenComparing(x -> x.region, Comparator.nullsLast(Comparator.naturalOrder()))
.thenComparing(x -> x.accountId, Comparator.nullsLast(Comparator.naturalOrder()))
.thenComparing(Arn::resourceAsString);

@Override
public int compareTo(Arn rhs) {
return COMPARATOR.compare(this, rhs);
}

### Describe alternatives you've considered

_No response_

### Acknowledge

- [X] I may be able to implement this feature request

### AWS Java SDK version used

aws-sdk-java-v2 c0eb030fceaa9ccb64ad8acd74445f4e5ee3a547

### JDK version used

openjdk version "1.8.0_312"

### Operating System and version

MacOS Big Sur Version 11.6.2 (20G314)

Contributor guide

Open the contributing guide

Research direction

Start with Arn.java, the entry point named in the issue, and review how ARN components and string representations are exposed. Implement the requested comparable behavior using one of the proposed ordering approaches, then verify that Arn instances can be used in sorted collections and that the ordering is consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.