/*
 * Copyright 2008-2009 LinkedIn, Inc
 * 
 * Licensed 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 voldemort.versioning;

import java.io.Serializable;
import java.util.Comparator;

import com.google.common.base.Objects;

/**
 * A wrapper for an object that adds a Version.
 * 
 * @author jay
 * 
 */
public final class Versioned<T> implements Serializable {

    private static final long serialVersionUID = 1;

    private volatile VectorClock version;
    private volatile T object;

    public Versioned(T object) {
        this(object, new VectorClock());
    }

    public Versioned(T object, Version version) {
        this.version = version == null ? new VectorClock() : (VectorClock) version;
        this.object = object;
    }

    public Version getVersion() {
        return version;
    }

    public T getValue() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean equals(Object object) {
        if(this == null)
            return true;
        else if(object == null)
            return false;
        else if(!object.getClass().equals(Versioned.class))
            return false;

        Versioned versioned = (Versioned) object;
        return Objects.equal(getVersion(), versioned.getVersion())
               && Objects.deepEquals(getValue(), versioned.getValue());
    }

    @Override
    public int hashCode() {
        return 31 + version.hashCode() + 31 * object.hashCode();
    }

    @Override
    public String toString() {
        return "[" + object + ", " + version + "]";
    }

    /**
     * Create a clone of this Versioned object such that the object pointed to
     * is the same, but the VectorClock and Versioned wrapper is a shallow copy.
     */
    public Versioned<T> cloneVersioned() {
        return new Versioned<T>(this.getValue(), this.version.clone());
    }

    public static final class HappenedBeforeComparator<S> implements Comparator<Versioned<S>> {

        public int compare(Versioned<S> v1, Versioned<S> v2) {
            Occured occured = v1.getVersion().compare(v2.getVersion());
            if(occured == Occured.BEFORE)
                return -1;
            else if(occured == Occured.AFTER)
                return 1;
            else
                return 0;
        }
    }

}

@@ -1542,63 +1542,8 @@
     
-if(this == null)%0A            return true;%0A        else 
 if(o
@@ -1551,20 +1551,20 @@
 ject == 
-null
+this
 )%0A      
@@ -1576,20 +1576,19 @@
  return 
-fals
+tru
 e;%0A     
@@ -1603,33 +1603,27 @@
 if(!
+(
 object
-.getClass().equals(
+ instanceof 
 Vers
@@ -1631,14 +1631,8 @@
 oned
-.class
 ))%0A 

Fix Versioned.equals()--remove useless null check of this. http://code.google.com/p/project-voldemort/issues/detail?id=39