Skip to content

Commit 660d785

Browse files
Add a dependency on AutoValue to simplify handling of value classes. (#471)
## What changes are proposed in this pull request? This PR adds a dependency on [Google's AutoValue library](https://chromium.googlesource.com/external/github.com/google/auto/+/refs/tags/auto-value-1.6.2/value/userguide/index.md) to simplify the development and maintenance of value classes. See [Why should I use AutoValue?](https://chromium.googlesource.com/external/github.com/google/auto/+/refs/tags/auto-value-1.6.2/value/userguide/why.md) for more details. The decision to use AutoValue is motivated by the fact that the Databricks SDK must support Java 8 for the foreseeable future and does not have access to new language features such as records — which would obsolete AutoValue. NO_CHANGELOG=true ## How is this tested? Simplified one small internal class to use AutoValue and validated that the impacted unit and integration tests are fine with it.
1 parent 497c415 commit 660d785

3 files changed

Lines changed: 30 additions & 34 deletions

File tree

databricks-sdk-java/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,18 @@
103103
<artifactId>jackson-datatype-jsr310</artifactId>
104104
<version>${jackson.version}</version>
105105
</dependency>
106+
<!-- Google Auto Value -->
107+
<dependency>
108+
<groupId>com.google.auto.value</groupId>
109+
<artifactId>auto-value</artifactId>
110+
<version>1.10.4</version>
111+
<scope>provided</scope>
112+
</dependency>
113+
<dependency>
114+
<groupId>com.google.auto.value</groupId>
115+
<artifactId>auto-value-annotations</artifactId>
116+
<version>1.10.4</version>
117+
<scope>provided</scope>
118+
</dependency>
106119
</dependencies>
107120
</project>

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/DataPlaneTokenSource.java

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.databricks.sdk.core.oauth;
22

33
import com.databricks.sdk.core.http.HttpClient;
4+
import com.google.auto.value.AutoValue;
45
import java.util.Objects;
56
import java.util.concurrent.ConcurrentHashMap;
67

@@ -17,44 +18,19 @@ public class DataPlaneTokenSource {
1718
private final TokenSource cpTokenSource;
1819
private final String host;
1920
private final ConcurrentHashMap<TokenSourceKey, EndpointTokenSource> sourcesCache;
21+
2022
/**
2123
* Caching key for {@link EndpointTokenSource}, based on endpoint and authorization details. This
2224
* is a value object that uniquely identifies a token source configuration.
2325
*/
24-
private static final class TokenSourceKey {
25-
/** The target service endpoint URL. */
26-
private final String endpoint;
27-
28-
/** Specific authorization details for the endpoint. */
29-
private final String authDetails;
26+
@AutoValue
27+
abstract static class TokenSourceKey {
28+
abstract String endpoint();
3029

31-
/**
32-
* Constructs a TokenSourceKey.
33-
*
34-
* @param endpoint The target service endpoint URL.
35-
* @param authDetails Specific authorization details.
36-
*/
37-
public TokenSourceKey(String endpoint, String authDetails) {
38-
this.endpoint = endpoint;
39-
this.authDetails = authDetails;
40-
}
41-
42-
@Override
43-
public boolean equals(Object o) {
44-
if (this == o) {
45-
return true;
46-
}
47-
if (o == null || getClass() != o.getClass()) {
48-
return false;
49-
}
50-
TokenSourceKey that = (TokenSourceKey) o;
51-
return Objects.equals(endpoint, that.endpoint)
52-
&& Objects.equals(authDetails, that.authDetails);
53-
}
30+
abstract String authDetails();
5431

55-
@Override
56-
public int hashCode() {
57-
return Objects.hash(endpoint, authDetails);
32+
static TokenSourceKey create(String endpoint, String authDetails) {
33+
return new AutoValue_DataPlaneTokenSource_TokenSourceKey(endpoint, authDetails);
5834
}
5935
}
6036

@@ -101,14 +77,14 @@ public Token getToken(String endpoint, String authDetails) {
10177
throw new IllegalArgumentException("Authorization details cannot be empty");
10278
}
10379

104-
TokenSourceKey key = new TokenSourceKey(endpoint, authDetails);
80+
TokenSourceKey key = TokenSourceKey.create(endpoint, authDetails);
10581

10682
EndpointTokenSource specificSource =
10783
sourcesCache.computeIfAbsent(
10884
key,
10985
k ->
11086
new EndpointTokenSource(
111-
this.cpTokenSource, k.authDetails, this.httpClient, this.host));
87+
this.cpTokenSource, k.authDetails(), this.httpClient, this.host));
11288

11389
return specificSource.getToken();
11490
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
<configuration>
6868
<source>8</source>
6969
<target>8</target>
70+
<annotationProcessorPaths>
71+
<path>
72+
<groupId>com.google.auto.value</groupId>
73+
<artifactId>auto-value</artifactId>
74+
<version>1.10.4</version>
75+
</path>
76+
</annotationProcessorPaths>
7077
</configuration>
7178
</plugin>
7279
<plugin>

0 commit comments

Comments
 (0)