io.getunleash.repository.SegmentCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unleash-client-java Show documentation
Show all versions of unleash-client-java Show documentation
A client library for Unleash
package io.getunleash.repository;
import io.getunleash.Segment;
import io.getunleash.lang.Nullable;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public final class SegmentCollection implements Serializable {
static final long serialVersionUID = 1214L;
private final Collection segments;
private final transient Map cache;
public SegmentCollection(final Collection segments) {
this.segments = ensureNotNull(segments);
if (this.segments.size() > 0) {
this.cache =
segments.stream()
.collect(
Collectors.toConcurrentMap(
Segment::getId, Function.identity()));
} else {
this.cache = new ConcurrentHashMap<>();
}
}
private Collection ensureNotNull(@Nullable Collection segments) {
return Optional.ofNullable(segments).orElseGet(Collections::emptyList);
}
public Collection getSegments() {
return Collections.unmodifiableCollection(segments);
}
public Segment getSegment(final Integer id) {
return cache.get(id);
}
}