Skip to content
Merged
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
45 changes: 11 additions & 34 deletions client/src/main/java/io/split/client/SplitManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

import com.google.common.base.Preconditions;
import io.split.client.api.SplitView;
import io.split.client.dtos.Partition;
import io.split.engine.SDKReadinessGates;
import io.split.cache.SplitCache;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedSplit;
import io.split.inputValidation.SplitNameValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import java.util.Optional;
import java.util.concurrent.TimeoutException;

/**
Expand Down Expand Up @@ -44,21 +41,20 @@ public List<SplitView> splits() {
List<SplitView> result = new ArrayList<>();
Collection<ParsedSplit> parsedSplits = _splitCache.getAll();
for (ParsedSplit split : parsedSplits) {
result.add(toSplitView(split));
result.add(SplitView.fromParsedSplit(split));
}

return result;
}

@Override
public SplitView split(String featureName) {
if (featureName == null) {
_log.error("split: you passed a null split name, split name must be a non-empty string");
return null;
}
if (featureName.isEmpty()) {
_log.error("split: you passed an empty split name, split name must be a non-empty string");
Optional<String> result = SplitNameValidator.isValid(featureName, "split");
if (!result.isPresent()) {
return null;
}
featureName = result.get();

ParsedSplit parsedSplit = _splitCache.get(featureName);
if (parsedSplit == null) {
if (_gates.isSDKReadyNow()) {
Expand All @@ -67,7 +63,8 @@ public SplitView split(String featureName) {
}
return null;
}
return toSplitView(parsedSplit);

return SplitView.fromParsedSplit(parsedSplit);
}

@Override
Expand All @@ -77,6 +74,7 @@ public List<String> splitNames() {
for (ParsedSplit split : parsedSplits) {
result.add(split.feature());
}

return result;
}

Expand All @@ -89,25 +87,4 @@ public void blockUntilReady() throws TimeoutException, InterruptedException {
throw new TimeoutException("SDK was not ready in " + _config.blockUntilReady()+ " milliseconds");
}
}

private SplitView toSplitView(ParsedSplit parsedSplit) {
SplitView splitView = new SplitView();
splitView.name = parsedSplit.feature();
splitView.trafficType = parsedSplit.trafficTypeName();
splitView.killed = parsedSplit.killed();
splitView.changeNumber = parsedSplit.changeNumber();

Set<String> treatments = new HashSet<String>();
for (ParsedCondition condition : parsedSplit.parsedConditions()) {
for (Partition partition : condition.partitions()) {
treatments.add(partition.treatment);
}
}
treatments.add(parsedSplit.defaultTreatment());

splitView.treatments = new ArrayList<String>(treatments);
splitView.configs = parsedSplit.configurations() == null? Collections.<String, String>emptyMap() : parsedSplit.configurations() ;

return splitView;
}
}
30 changes: 30 additions & 0 deletions client/src/main/java/io/split/client/api/SplitView.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package io.split.client.api;

import io.split.client.dtos.Partition;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedSplit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
* A view of a Split meant for consumption through SplitManager interface.
Expand All @@ -15,4 +24,25 @@ public class SplitView {
public List<String> treatments;
public long changeNumber;
public Map<String, String> configs;

public static SplitView fromParsedSplit(ParsedSplit parsedSplit) {
SplitView splitView = new SplitView();
splitView.name = parsedSplit.feature();
splitView.trafficType = parsedSplit.trafficTypeName();
splitView.killed = parsedSplit.killed();
splitView.changeNumber = parsedSplit.changeNumber();

Set<String> treatments = new HashSet<String>();
for (ParsedCondition condition : parsedSplit.parsedConditions()) {
for (Partition partition : condition.partitions()) {
treatments.add(partition.treatment);
}
}
treatments.add(parsedSplit.defaultTreatment());

splitView.treatments = new ArrayList<String>(treatments);
splitView.configs = parsedSplit.configurations() == null? Collections.<String, String>emptyMap() : parsedSplit.configurations() ;

return splitView;
}
}