Skip to content
Closed
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import androidx.annotation.Nullable;

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

Expand All @@ -11,39 +12,85 @@
/**
* Helper class for creating {@link EventMetadata} instances.
* <p>
* This keeps the metadata keys in a single place to avoid typos and inconsistencies.
* Use these factory methods to create metadata for different event types.
*/
public class EventMetadataHelpers {

private static final String KEY_UPDATED_FLAGS = "updatedFlags";
private static final String KEY_LAST_UPDATE_TIMESTAMP = "lastUpdateTimestamp";
private static final String KEY_FRESH_INSTALL = "freshInstall";

private EventMetadataHelpers() {
// Utility class
}

public static EventMetadata createUpdatedFlagsMetadata(List<String> updatedSplitNames) {
return new EventMetadataBuilder()
.put(KEY_UPDATED_FLAGS, new ArrayList<>(new HashSet<>(updatedSplitNames)))
.build();
/**
* Creates metadata for a FLAG_UPDATE event.
* <p>
* Flag names are deduplicated automatically.
*
* @param flagNames the names of flags that were updated
* @param changeNumber the changeNumber associated with this update, or null if not available
* @return the event metadata
*/
public static EventMetadata createFlagUpdateMetadata(
List<String> flagNames,
@Nullable Long changeNumber) {
// Deduplicate flag names
List<String> uniqueFlags = new ArrayList<>(new HashSet<>(flagNames));
return new EventMetadataImpl(
EventMetadata.Type.FLAG_UPDATE,
uniqueFlags,
changeNumber
);
}

/**
* Creates metadata for the SDK_READY_FROM_CACHE event.
* Creates metadata for a SEGMENT_UPDATE event.
* <p>
* Segment names are deduplicated automatically.
*
* @param lastUpdateTimestamp the timestamp when the cache was last updated, or null if not available
* @param freshInstall true if this is a fresh install (no prior cache), false if loaded from cache
* @param segmentNames the names of segments that were updated
* @param changeNumber the changeNumber associated with this update, or null if not available
* @return the event metadata
*/
public static EventMetadata createCacheReadyMetadata(@Nullable Long lastUpdateTimestamp, boolean freshInstall) {
EventMetadataBuilder builder = new EventMetadataBuilder()
.put(KEY_FRESH_INSTALL, freshInstall);
public static EventMetadata createSegmentUpdateMetadata(
List<String> segmentNames,
@Nullable Long changeNumber) {
// Deduplicate segment names
List<String> uniqueSegments = new ArrayList<>(new HashSet<>(segmentNames));
return new EventMetadataImpl(
EventMetadata.Type.SEGMENT_UPDATE,
uniqueSegments,
changeNumber
);
}

if (lastUpdateTimestamp != null) {
builder.put(KEY_LAST_UPDATE_TIMESTAMP, lastUpdateTimestamp);
}
/**
* Creates metadata for a FRESH_INSTALL event.
* <p>
* This is used when SDK_READY_FROM_CACHE fires but there was no prior cache
* (fresh install scenario).
*
* @return the event metadata
*/
public static EventMetadata createFreshInstallMetadata() {
return new EventMetadataImpl(
EventMetadata.Type.FRESH_INSTALL,
Collections.emptyList(),
null
);
}

return builder.build();
/**
* Creates metadata for a FROM_CACHE event.
* <p>
* This is used when SDK_READY_FROM_CACHE fires after loading from existing cache.
*
* @param lastUpdateTimestamp the timestamp when the cache was last updated
* @return the event metadata
*/
public static EventMetadata createFromCacheMetadata(long lastUpdateTimestamp) {
return new EventMetadataImpl(
EventMetadata.Type.FROM_CACHE,
Collections.emptyList(),
lastUpdateTimestamp
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,53 @@
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.split.android.client.api.EventMetadata;

/**
* Implementation of {@link EventMetadata}.
* Use {@link EventMetadataBuilder} to create instances.
* Use {@link EventMetadataHelpers} factory methods to create instances.
*/
class EventMetadataImpl implements EventMetadata {
public class EventMetadataImpl implements EventMetadata {

private final Map<String, Object> mData;

EventMetadataImpl(@NonNull Map<String, Object> data) {
Map<String, Object> copy = new HashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
Object value = entry.getValue();
if (value instanceof List) {
copy.put(entry.getKey(), Collections.unmodifiableList(new ArrayList<>((List<?>) value)));
} else {
copy.put(entry.getKey(), value);
}
}
mData = Collections.unmodifiableMap(copy);
@NonNull
private final Type mType;
@NonNull
private final List<String> mValues;
@Nullable
private final Long mValue;

/**
* Creates a new EventMetadataImpl.
*
* @param type the type of metadata
* @param values the list of values (flag names, segment names, etc.)
* @param value the numeric value (changeNumber, timestamp, etc.)
*/
public EventMetadataImpl(@NonNull Type type, @NonNull List<String> values, @Nullable Long value) {
mType = type;
// Defensive copy to ensure immutability
mValues = Collections.unmodifiableList(new ArrayList<>(values));
mValue = value;
}

@NonNull
@Override
public Set<String> keys() {
return mData.keySet();
public Type getType() {
return mType;
}

@NonNull
@Override
public Collection<Object> values() {
return mData.values();
public List<String> getValues() {
return mValues;
}

@Nullable
@Override
public Object get(@NonNull String key) {
return mData.get(key);
}

@Override
public boolean containsKey(@NonNull String key) {
return mData.containsKey(key);
}

@NonNull
@Override
public Map<String, Object> toMap() {
Map<String, Object> copy = new HashMap<>();
for (Map.Entry<String, Object> entry : mData.entrySet()) {
Object value = entry.getValue();
if (value instanceof List) {
copy.put(entry.getKey(), new ArrayList<>((List<?>) value));
} else {
copy.put(entry.getKey(), value);
}
}
return copy;
public Long getValue() {
return mValue;
}
}

This file was deleted.

This file was deleted.

Loading
Loading