package org.apache.cassandra.db;
import java.nio.ByteBuffer;
import java.util.List;
import org.apache.cassandra.utils.memory.AbstractAllocator;
public class ClusteringBound extends ClusteringBoundOrBoundary
{
public static final ClusteringBound BOTTOM = new ClusteringBound(Kind.INCL_START_BOUND, EMPTY_VALUES_ARRAY);
public static final ClusteringBound TOP = new ClusteringBound(Kind.INCL_END_BOUND, EMPTY_VALUES_ARRAY);
protected ClusteringBound(Kind kind, ByteBuffer[] values)
{
super(kind, values);
}
public static ClusteringBound create(Kind kind, ByteBuffer[] values)
{
assert !kind.isBoundary();
return new ClusteringBound(kind, values);
}
public static Kind boundKind(boolean isStart, boolean isInclusive)
{
return isStart
? (isInclusive ? Kind.INCL_START_BOUND : Kind.EXCL_START_BOUND)
: (isInclusive ? Kind.INCL_END_BOUND : Kind.EXCL_END_BOUND);
}
public static ClusteringBound inclusiveStartOf(ByteBuffer... values)
{
return create(Kind.INCL_START_BOUND, values);
}
public static ClusteringBound inclusiveEndOf(ByteBuffer... values)
{
return create(Kind.INCL_END_BOUND, values);
}
public static ClusteringBound exclusiveStartOf(ByteBuffer... values)
{
return create(Kind.EXCL_START_BOUND, values);
}
public static ClusteringBound exclusiveEndOf(ByteBuffer... values)
{
return create(Kind.EXCL_END_BOUND, values);
}
public static ClusteringBound inclusiveStartOf(ClusteringPrefix prefix)
{
ByteBuffer[] values = new ByteBuffer[prefix.size()];
for (int i = 0; i < prefix.size(); i++)
values[i] = prefix.get(i);
return inclusiveStartOf(values);
}
public static ClusteringBound exclusiveStartOf(ClusteringPrefix prefix)
{
ByteBuffer[] values = new ByteBuffer[prefix.size()];
for (int i = 0; i < prefix.size(); i++)
values[i] = prefix.get(i);
return exclusiveStartOf(values);
}
public static ClusteringBound inclusiveEndOf(ClusteringPrefix prefix)
{
ByteBuffer[] values = new ByteBuffer[prefix.size()];
for (int i = 0; i < prefix.size(); i++)
values[i] = prefix.get(i);
return inclusiveEndOf(values);
}
public static ClusteringBound create(ClusteringComparator comparator, boolean isStart, boolean isInclusive, Object... values)
{
CBuilder builder = CBuilder.create(comparator);
for (Object val : values)
{
if (val instanceof ByteBuffer)
builder.add((ByteBuffer) val);
else
builder.add(val);
}
return builder.buildBound(isStart, isInclusive);
}
@Override
public ClusteringBound invert()
{
return create(kind().invert(), values);
}
public ClusteringBound copy(AbstractAllocator allocator)
{
return (ClusteringBound) super.copy(allocator);
}
public boolean isStart()
{
return kind().isStart();
}
public boolean isEnd()
{
return !isStart();
}
public boolean isInclusive()
{
return kind == Kind.INCL_START_BOUND || kind == Kind.INCL_END_BOUND;
}
public boolean isExclusive()
{
return kind == Kind.EXCL_START_BOUND || kind == Kind.EXCL_END_BOUND;
}
int compareTo(ClusteringComparator comparator, List<ByteBuffer> sstableBound)
{
for (int i = 0; i < sstableBound.size(); i++)
{
if (i >= size())
return isStart() ? -1 : 1;
int cmp = comparator.compareComponent(i, get(i), sstableBound.get(i));
if (cmp != 0)
return cmp;
}
if (size() > sstableBound.size())
return isStart() ? -1 : 1;
return isInclusive() ? 0 : (isStart() ? 1 : -1);
}
}