Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start Interning instances of SlsVersionMatcher #942

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sls-versions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
api 'com.palantir.safe-logging:preconditions'
api 'com.palantir.safe-logging:safe-logging'
implementation 'com.palantir.safe-logging:logger'
implementation 'com.google.guava:guava'

annotationProcessor 'org.immutables:value'
compileOnly 'org.immutables:value::annotations'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.logger.SafeLogger;
Expand All @@ -38,6 +40,8 @@
@ImmutablesStyle
public abstract class SlsVersionMatcher {

private static final Interner<SlsVersionMatcher> interner = Interners.newWeakInterner();

private static final SafeLogger log = SafeLoggerFactory.get(SlsVersionMatcher.class);

private static final Comparator<OptionalInt> EMPTY_IS_GREATER =
Expand Down Expand Up @@ -96,7 +100,7 @@ static Optional<SlsVersionMatcher> maybeCreate(
SafeArg.of("matcher", maybeMatcher));
return Optional.empty();
}
return Optional.of(maybeMatcher);
return Optional.of(interner.intern(maybeMatcher));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ public void testMatcherComparator_comparesNumbers() {
assertMatcherOrder(SlsVersionMatcher.valueOf("2.6.5"), SlsVersionMatcher.valueOf("2.6.6"));
}

@Test
void interning_works() {
SlsVersionMatcher instance1 = matcher("1.x.x");
SlsVersionMatcher instance2 = matcher("1.x.x");
assertThat(instance1).isSameAs(instance2);
}

@Test
void surprising_interning_consequences() {
SlsVersionMatcher instance1 = matcher("09876543.x.x");
SlsVersionMatcher instance2 = matcher("9876543.x.x");
assertThat(instance1).isEqualTo(instance2);
assertThat(instance1).isSameAs(instance2);
assertThat(instance2).hasToString("09876543.x.x");
}

private static void assertMatcherOrder(SlsVersionMatcher smaller, SlsVersionMatcher larger) {
assertThat(SlsVersionMatcher.MATCHER_COMPARATOR.compare(smaller, larger))
.isLessThan(0);
Expand Down
10 changes: 5 additions & 5 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
com.fasterxml.jackson.core:jackson-annotations:2.16.1 (2 constraints: c517c771)
com.google.code.findbugs:jsr305:3.0.2 (1 constraints: 170aecb4)
com.google.errorprone:error_prone_annotations:2.26.1 (5 constraints: 9b497093)
com.google.guava:failureaccess:1.0.2 (1 constraints: 150ae2b4)
com.google.guava:guava:33.1.0-jre (1 constraints: a7066453)
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava (1 constraints: bd17c918)
com.google.j2objc:j2objc-annotations:3.0.0 (1 constraints: 150aeab4)
com.palantir.safe-logging:logger:3.7.0 (1 constraints: 0c050f36)
com.palantir.safe-logging:logger-slf4j:3.7.0 (1 constraints: 050e6842)
com.palantir.safe-logging:logger-spi:3.7.0 (2 constraints: 191ea27b)
com.palantir.safe-logging:preconditions:3.7.0 (1 constraints: 0c050f36)
com.palantir.safe-logging:safe-logging:3.7.0 (4 constraints: 92339760)
org.checkerframework:checker-qual:3.42.0 (1 constraints: 4b0a47bf)
org.immutables:value:2.10.1 (1 constraints: 3605303b)
org.jetbrains:annotations:24.1.0 (1 constraints: 331166d1)
org.slf4j:slf4j-api:1.7.36 (2 constraints: 2321cfd3)

[Test dependencies]
com.fasterxml.jackson.core:jackson-core:2.16.1 (1 constraints: 8a123f21)
com.fasterxml.jackson.core:jackson-databind:2.16.1 (1 constraints: 3c05423b)
com.google.guava:failureaccess:1.0.2 (1 constraints: 150ae2b4)
com.google.guava:guava:33.1.0-jre (1 constraints: a7066453)
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava (1 constraints: bd17c918)
com.google.j2objc:j2objc-annotations:3.0.0 (1 constraints: 150aeab4)
net.bytebuddy:byte-buddy:1.14.11 (1 constraints: 7e0bc5ea)
net.jqwik:jqwik:1.3.7 (1 constraints: 0d050036)
net.jqwik:jqwik-api:1.3.7 (2 constraints: de117b11)
Expand All @@ -29,7 +30,6 @@ org.apache.logging.log4j:log4j-core:2.23.1 (1 constraints: d41002df)
org.apache.logging.log4j:log4j-slf4j-impl:2.23.1 (1 constraints: 3a053d3b)
org.apiguardian:apiguardian-api:1.1.2 (8 constraints: d66ee506)
org.assertj:assertj-core:3.25.3 (1 constraints: 3f054b3b)
org.checkerframework:checker-qual:3.42.0 (1 constraints: 4b0a47bf)
org.junit.jupiter:junit-jupiter:5.10.2 (1 constraints: 3a05433b)
org.junit.jupiter:junit-jupiter-api:5.10.2 (3 constraints: f12f0e51)
org.junit.jupiter:junit-jupiter-engine:5.10.2 (1 constraints: 350eff49)
Expand Down