/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.cassandra.db;

import java.nio.ByteBuffer;
import java.util.Comparator;

import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.dht.Token;
import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.utils.FBUtilities;

/**
 * Represents a decorated key, handy for certain operations
 * where just working with strings gets slow.
 *
 * We do a lot of sorting of DecoratedKeys, so for speed, we assume that tokens correspond one-to-one with keys.
 * This is not quite correct in the case of RandomPartitioner (which uses MD5 to hash keys to tokens);
 * if this matters, you can subclass RP to use a stronger hash, or use a non-lossy tokenization scheme (as in the
 * OrderPreservingPartitioner classes).
 */
public class DecoratedKey<T extends Token> implements Comparable<DecoratedKey>
{
    private static IPartitioner partitioner = StorageService.getPartitioner();

    public static final Comparator<DecoratedKey> comparator = new Comparator<DecoratedKey>()
    {
        public int compare(DecoratedKey o1, DecoratedKey o2)
        {
            return o1.compareTo(o2);
        }
    };

    public final T token;
    public final ByteBuffer key;

    public DecoratedKey(T token, ByteBuffer key)
    {
        super();
        assert token != null;
        this.token = token;
        this.key = key;
    }

    @Override
    public int hashCode()
    {
        return token.hashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;

        DecoratedKey other = (DecoratedKey) obj;
        return token.equals(other.token);
    }

    public int compareTo(DecoratedKey other)
    {
        return token.compareTo(other.token);
    }

    public boolean isEmpty()
    {
        return token.equals(partitioner.getMinimumToken());
    }

    @Override
    public String toString()
    {
        String keystring = key == null ? "null" : FBUtilities.bytesToHex(key);
        return "DecoratedKey(" + token + ", " + keystring + ")";
    }
}

@@ -2249,16 +2249,53 @@
 shCode()
+ + (key == null ? 0 : key.hashCode())
 ;%0A    %7D%0A
@@ -2572,36 +2572,249 @@
 y) obj;%0A
-        return token
+%0A        if (token.equals(other.token))%0A        %7B%0A            if (key == null && other.key == null)%0A                return true;%0A%0A            if (key == null %7C%7C other.key == null)%0A                return false;%0A%0A            return key
 .equals(
@@ -2815,32 +2815,63 @@
 quals(other.
-to
 ke
-n
+y
 );%0A
+        %7D%0A%0A        return false;%0A
     %7D%0A%0A    p
@@ -2928,43 +2928,351 @@
     
-return token.compareTo(other.token)
+int cmp = token.compareTo(other.token);%0A%0A        if (cmp == 0)%0A        %7B%0A            if (key == null && other.key == null)%0A                return 0;%0A%0A            if (key == null)%0A                return 1;%0A%0A            if (other.key == null)%0A                return -1;%0A%0A            return key.compareTo(other.key);%0A        %7D%0A%0A        return cmp
 ;%0A  

include key in DecoratedKey equals, compareTo, and hashcode methods patch by tjake; reviewed by Stu Hood for CASSANDRA-1720