Skip to content
Open
Prev Previous commit
Next Next commit
extract common code
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
  • Loading branch information
chrfwow committed Nov 24, 2025
commit ba8f6bca9526a8951f91aeeb1fa0d40b36791c8d
24 changes: 24 additions & 0 deletions src/main/java/dev/openfeature/sdk/HookSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,10 +28,10 @@
*/
public void setHooks(
HookSupportData hookSupportData,
List<Hook> providerHooks,

Check warning on line 31 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIb&open=AZqiIk_Eaie-DOkKWNIb&pullRequest=1734
List<Hook> flagOptionsHooks,

Check warning on line 32 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIc&open=AZqiIk_Eaie-DOkKWNIc&pullRequest=1734
ConcurrentLinkedQueue<Hook> clientHooks,

Check warning on line 33 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The type of "clientHooks" should be an interface such as "Queue" rather than the implementation "ConcurrentLinkedQueue".

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIZ&open=AZqiIk_Eaie-DOkKWNIZ&pullRequest=1734

Check warning on line 33 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNId&open=AZqiIk_Eaie-DOkKWNId&pullRequest=1734
ConcurrentLinkedQueue<Hook> apiHooks) {

Check warning on line 34 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIe&open=AZqiIk_Eaie-DOkKWNIe&pullRequest=1734

Check warning on line 34 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The type of "apiHooks" should be an interface such as "Queue" rather than the implementation "ConcurrentLinkedQueue".

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIa&open=AZqiIk_Eaie-DOkKWNIa&pullRequest=1734
var lengthEstimate = 0;

if (providerHooks != null) {
Expand All @@ -45,7 +47,7 @@
lengthEstimate += apiHooks.size();
}

ArrayList<Pair<Hook, HookContext>> hookContextPairs = new ArrayList<>(lengthEstimate);

Check warning on line 50 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIg&open=AZqiIk_Eaie-DOkKWNIg&pullRequest=1734

Check warning on line 50 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIf&open=AZqiIk_Eaie-DOkKWNIf&pullRequest=1734

addAll(hookContextPairs, providerHooks);
addAll(hookContextPairs, flagOptionsHooks);
Expand All @@ -55,12 +57,12 @@
hookSupportData.hooks = hookContextPairs;
}

private void addAll(List<Pair<Hook, HookContext>> accumulator, Collection<Hook> toAdd) {

Check warning on line 60 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIh&open=AZqiIk_Eaie-DOkKWNIh&pullRequest=1734

Check warning on line 60 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Faie-DOkKWNIj&open=AZqiIk_Faie-DOkKWNIj&pullRequest=1734

Check warning on line 60 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Eaie-DOkKWNIi&open=AZqiIk_Eaie-DOkKWNIi&pullRequest=1734
if (toAdd == null || toAdd.isEmpty()) {
return;
}

for (Hook hook : toAdd) {

Check warning on line 65 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIk_Faie-DOkKWNIk&open=AZqiIk_Faie-DOkKWNIk&pullRequest=1734
accumulator.add(Pair.of(hook, null));
}
}
Expand Down Expand Up @@ -156,4 +158,26 @@
}
}
}

static void addHooks(Map<FlagValueType, ConcurrentLinkedQueue<Hook>> hookMap, Hook... hooksToAdd) {

Check warning on line 162 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZq1D1F8EzQ6q8saNcpy&open=AZq1D1F8EzQ6q8saNcpy&pullRequest=1734
var types = FlagValueType.values();
for (int i = 0; i < hooksToAdd.length; i++) {
var current = hooksToAdd[i];
for (int j = 0; j < types.length; j++) {
var type = types[j];
if (current.supportsFlagValueType(type)) {
hookMap.get(type).add(current);
}
}
}
}

static ArrayList<Hook> getAllUniqueHooks(Map<FlagValueType, ConcurrentLinkedQueue<Hook>> hookMap) {

Check warning on line 175 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZq1D1F8EzQ6q8saNcp0&open=AZq1D1F8EzQ6q8saNcp0&pullRequest=1734

Check warning on line 175 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZq1D1F8EzQ6q8saNcpz&open=AZq1D1F8EzQ6q8saNcpz&pullRequest=1734
// Hooks can be duplicated if they support multiple FlagValueTypes
var allHooks = new HashSet<Hook>();

Check warning on line 177 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZq1D1F8EzQ6q8saNcp1&open=AZq1D1F8EzQ6q8saNcp1&pullRequest=1734
for (var queue : hookMap.values()) {
allHooks.addAll(queue);
}
return new ArrayList<>(allHooks);
}
}
20 changes: 2 additions & 18 deletions src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import dev.openfeature.sdk.exceptions.OpenFeatureError;
import dev.openfeature.sdk.internal.AutoCloseableLock;
import dev.openfeature.sdk.internal.AutoCloseableReentrantReadWriteLock;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -24,7 +22,7 @@
public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
// package-private multi-read/single-write lock
static AutoCloseableReentrantReadWriteLock lock = new AutoCloseableReentrantReadWriteLock();
private final ConcurrentHashMap<FlagValueType, ConcurrentLinkedQueue<Hook>> apiHooks;

Check warning on line 25 in src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZq1D1JpEzQ6q8saNcp2&open=AZq1D1JpEzQ6q8saNcp2&pullRequest=1734
private ProviderRepository providerRepository;
private EventSupport eventSupport;
private final AtomicReference<EvaluationContext> evaluationContext = new AtomicReference<>();
Expand Down Expand Up @@ -308,16 +306,7 @@
* @param hooks The hook to add.
*/
public void addHooks(Hook... hooks) {
var types = FlagValueType.values();
for (int i = 0; i < hooks.length; i++) {
var current = hooks[i];
for (int j = 0; j < types.length; j++) {
var type = types[j];
if (current.supportsFlagValueType(type)) {
this.apiHooks.get(type).add(current);
}
}
}
HookSupport.addHooks(apiHooks, hooks);
}

/**
Expand All @@ -326,12 +315,7 @@
* @return A list of {@link Hook}s.
*/
public List<Hook> getHooks() {
// Hooks can be duplicated if they support multiple FlagValueTypes
var allHooks = new HashSet<Hook>();
for (var queue : this.apiHooks.values()) {
allHooks.addAll(queue);
}
return new ArrayList<>(allHooks);
return HookSupport.getAllUniqueHooks(apiHooks);
}

/**
Expand All @@ -339,7 +323,7 @@
*
* @return A list of {@link Hook}s.
*/
ConcurrentLinkedQueue<Hook> getHooks(FlagValueType type) {

Check warning on line 326 in src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZqiIlADaie-DOkKWNIm&open=AZqiIlADaie-DOkKWNIm&pullRequest=1734
return apiHooks.get(type);
}

Expand Down
29 changes: 7 additions & 22 deletions src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
import dev.openfeature.sdk.internal.ObjectUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -31,11 +29,11 @@
*/
@Slf4j
@SuppressWarnings({
"PMD.DataflowAnomalyAnalysis",
"PMD.BeanMembersShouldSerialize",
"PMD.UnusedLocalVariable",
"unchecked",
"rawtypes"
"PMD.DataflowAnomalyAnalysis",
"PMD.BeanMembersShouldSerialize",
"PMD.UnusedLocalVariable",
"unchecked",
"rawtypes"
})
@Deprecated() // TODO: eventually we will make this non-public. See issue #872
public class OpenFeatureClient implements Client {
Expand Down Expand Up @@ -130,16 +128,7 @@ public void track(String trackingEventName, EvaluationContext context, TrackingE
*/
@Override
public OpenFeatureClient addHooks(Hook... hooks) {
var types = FlagValueType.values();
for (int i = 0; i < hooks.length; i++) {
var current = hooks[i];
for (int j = 0; j < types.length; j++) {
var type = types[j];
if (current.supportsFlagValueType(type)) {
this.clientHooks.get(type).add(current);
}
}
}
HookSupport.addHooks(clientHooks, hooks);
return this;
}

Expand All @@ -148,11 +137,7 @@ public OpenFeatureClient addHooks(Hook... hooks) {
*/
@Override
public List<Hook> getHooks() {
var allHooks = new HashSet<Hook>();
for (var queue : this.clientHooks.values()) {
allHooks.addAll(queue);
}
return new ArrayList<>(allHooks);
return HookSupport.getAllUniqueHooks(clientHooks);
}

/**
Expand Down
Loading