package org.apache.cassandra.db.commitlog;
import java.io.IOException;
import java.util.*;
import com.google.common.collect.ImmutableSortedMap;
import org.apache.cassandra.db.TypeSizes;
import org.apache.cassandra.io.ISerializer;
import org.apache.cassandra.io.util.DataInputPlus;
import org.apache.cassandra.io.util.DataOutputPlus;
public class IntervalSet<T extends Comparable<T>>
{
@SuppressWarnings({ "rawtypes", "unchecked" })
private static final IntervalSet EMPTY = new IntervalSet(ImmutableSortedMap.of());
final private NavigableMap<T, T> ranges;
private IntervalSet(ImmutableSortedMap<T, T> ranges)
{
this.ranges = ranges;
}
public IntervalSet(T start, T end)
{
this(ImmutableSortedMap.of(start, end));
}
@SuppressWarnings("unchecked")
public static <T extends Comparable<T>> IntervalSet<T> empty()
{
return EMPTY;
}
public boolean contains(T position)
{
Map.Entry<T, T> range = ranges.floorEntry(position);
return range != null && position.compareTo(range.getValue()) <= 0;
}
public boolean isEmpty()
{
return ranges.isEmpty();
}
public Optional<T> lowerBound()
{
return isEmpty() ? Optional.empty() : Optional.of(ranges.firstKey());
}
public Optional<T> upperBound()
{
return isEmpty() ? Optional.empty() : Optional.of(ranges.lastEntry().getValue());
}
public Collection<T> starts()
{
return ranges.keySet();
}
public Collection<T> ends()
{
return ranges.values();
}
public String toString()
{
return ranges.toString();
}
@Override
public int hashCode()
{
return ranges.hashCode();
}
@Override
public boolean equals(Object obj)
{
return obj instanceof IntervalSet && ranges.equals(((IntervalSet<?>) obj).ranges);
}
public static final <T extends Comparable<T>> ISerializer<IntervalSet<T>> serializer(ISerializer<T> pointSerializer)
{
return new ISerializer<IntervalSet<T>>()
{
public void serialize(IntervalSet<T> intervals, DataOutputPlus out) throws IOException
{
out.writeInt(intervals.ranges.size());
for (Map.Entry<T, T> en : intervals.ranges.entrySet())
{
pointSerializer.serialize(en.getKey(), out);
pointSerializer.serialize(en.getValue(), out);
}
}
public IntervalSet<T> deserialize(DataInputPlus in) throws IOException
{
int count = in.readInt();
NavigableMap<T, T> ranges = new TreeMap<>();
for (int i = 0; i < count; ++i)
ranges.put(pointSerializer.deserialize(in), pointSerializer.deserialize(in));
return new IntervalSet<T>(ImmutableSortedMap.copyOfSorted(ranges));
}
public long serializedSize(IntervalSet<T> intervals)
{
long size = TypeSizes.sizeof(intervals.ranges.size());
for (Map.Entry<T, T> en : intervals.ranges.entrySet())
{
size += pointSerializer.serializedSize(en.getKey());
size += pointSerializer.serializedSize(en.getValue());
}
return size;
}
};
};
static public class Builder<T extends Comparable<T>>
{
final NavigableMap<T, T> ranges;
public Builder()
{
this.ranges = new TreeMap<>();
}
public Builder(T start, T end)
{
this();
assert start.compareTo(end) <= 0;
ranges.put(start, end);
}
public void add(T start, T end)
{
assert start.compareTo(end) <= 0;
Map.Entry<T, T> extend = ranges.floorEntry(end);
if (extend != null && extend.getValue().compareTo(end) > 0)
end = extend.getValue();
extend = ranges.lowerEntry(start);
if (extend != null && extend.getValue().compareTo(start) >= 0)
start = extend.getKey();
ranges.subMap(start, end).clear();
ranges.put(start, end);
}
public void addAll(IntervalSet<T> otherSet)
{
for (Map.Entry<T, T> en : otherSet.ranges.entrySet())
{
add(en.getKey(), en.getValue());
}
}
public IntervalSet<T> build()
{
return new IntervalSet<T>(ImmutableSortedMap.copyOfSorted(ranges));
}
}
}