aws / aws-sdk-java-v2

The official AWS SDK for Java - Version 2
Apache License 2.0
2.16k stars 833 forks source link

Comparable Arns #2955

Open jaredcdavis opened 2 years ago

jaredcdavis commented 2 years ago

Describe the feature

It'd be nice if Arn implemented Comparable<Arn>.

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

Is your Feature Request related to a problem?

Not really.

So this would just make Arns more flexible.

Proposed Solution

Here is a straightforward implementation:

public static final Comparator<Arn> 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<Arn> 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

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)

debora-ito commented 2 years ago

HI @jaredcdavis that's interesting feature request, marking as such.

Can you tell us more about your use case?