/**
 * 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.activeblaze.wire;

import java.io.IOException;

/**
 * Holds information about the Value in the Map
 */
public class StateValue extends Packet {
    public enum StateType {
        NOT_SET, INSERT, DELETE, SYNC
    }

    ;
    private StateKey key;
    private Object value;
    private StateType type = StateType.NOT_SET;
    private Object oldValue;
    private boolean mapUpdate;
    private boolean mapWrite;
    private boolean expired;
    private boolean lockExpired;
    private boolean lockUpdate;
    private boolean lockWrite;
    private boolean error;

    /**
     * Constructor
     */
    public StateValue() {
    }

    /**
     * Constructor
     */
    public StateValue(StateKey key, Object value) {
        this.key = key;
        this.value = value;
    }

    public int getPacketType() {
        return PacketType.STATE.getNumber();
    }

    public StateValue clone() {
        StateValue result = new StateValue();
        copy(result);
        return result;
    }

    protected void copy(StateValue copy) {
        super.copy(copy);
        copy.key = this.key;
        copy.value = this.value;
        copy.oldValue = this.oldValue;
        copy.mapUpdate = this.mapUpdate;
        copy.mapWrite = this.mapWrite;
        copy.expired = this.expired;
        copy.lockExpired = this.lockExpired;
        copy.lockUpdate = this.lockUpdate;
        copy.lockWrite = this.lockWrite;
        copy.error = this.error;
        copy.type = this.type;
    }

    public int hashCode() {
        return this.value != null ? this.value.hashCode() : super.hashCode();
    }

    public boolean equals(Object obj) {
        boolean result = false;
        if (obj instanceof StateValue) {
            StateValue other = (StateValue) obj;
            result = (this.value == null && other.value == null)
                    || (this.value != null && other.value != null && this.value.equals(other.value));
        }
        return result;
    }

    /**
     * @return the owner
     */
    public StateKey getKey() {
        return this.key;
    }

    /**
     * Set the key
     *
     * @param key the key to set
     */
    public void setKey(StateKey key) {
        this.key = key;
    }

    /**
     * @return the key
     */
    public Object getValue() throws Exception {
        return this.value;
    }

    /**
     * Set the value
     *
     * @param value the value to set
     */
    public void setValue(Object value) {
        this.value = value;
    }

    /**
     * Get the oldValue
     *
     * @return the oldValue
     */
    public Object getOldValue() {
        return this.oldValue;
    }

    /**
     * Set the oldValue
     *
     * @param oldValue the oldValue to set
     */
    public void setOldValue(Object oldValue) {
        this.oldValue = oldValue;
    }

    /**
     * Get the mapUpdate
     *
     * @return the mapUpdate
     */
    public boolean isMapUpdate() {
        return this.mapUpdate;
    }

    /**
     * Set the mapUpdate
     *
     * @param mapUpdate the mapUpdate to set
     */
    public void setMapUpdate(boolean mapUpdate) {
        this.mapUpdate = mapUpdate;
    }

    /**
     * Get the mapWrite
     *
     * @return the mapWrite
     */
    public boolean isMapWrite() {
        return this.mapWrite;
    }

    /**
     * Set the mapWrite
     *
     * @param mapWrite the mapWrite to set
     */
    public void setMapWrite(boolean mapWrite) {
        this.mapWrite = mapWrite;
    }

    /**
     * Get the expired
     *
     * @return the expired
     */
    public boolean isExpired() {
        return this.expired;
    }

    /**
     * Set the expired
     *
     * @param expired the expired to set
     */
    public void setExpired(boolean expired) {
        this.expired = expired;
    }

    /**
     * Get the lockExpired
     *
     * @return the lockExpired
     */
    public boolean isLockExpired() {
        return this.lockExpired;
    }

    /**
     * Set the lockExpired
     *
     * @param lockExpired the lockExpired to set
     */
    public void setLockExpired(boolean lockExpired) {
        this.lockExpired = lockExpired;
    }

    /**
     * Get the lockUpdate
     *
     * @return the lockUpdate
     */
    public boolean isLockUpdate() {
        return this.lockUpdate;
    }

    /**
     * Set the lockUpdate
     *
     * @param lockUpdate the lockUpdate to set
     */
    public void setLockUpdate(boolean lockUpdate) {
        this.lockUpdate = lockUpdate;
    }

    /**
     * Get the lockWrite
     *
     * @return the lockWrite
     */
    public boolean isLockWrite() {
        return this.lockWrite;
    }

    /**
     * Set the lockWrite
     *
     * @param lockWrite the lockWrite to set
     */
    public void setLockWrite(boolean lockWrite) {
        this.lockWrite = lockWrite;
    }

    /**
     * Get the error
     *
     * @return the error
     */
    public boolean isError() {
        return this.error;
    }

    /**
     * Set the error
     *
     * @param error the error to set
     */
    public void setError(boolean error) {
        this.error = error;
    }

    public void read(BufferInputStream in) throws IOException {
        super.read(in);
        String typeStr = in.readUTF();
        this.type = StateType.valueOf(typeStr);
        ByteBool bb = new ByteBool();
        bb.setData(in.readByte());
        this.mapUpdate = bb.readBoolean();
        this.mapWrite = bb.readBoolean();
        this.expired = bb.readBoolean();
        this.lockExpired = bb.readBoolean();
        this.lockUpdate = bb.readBoolean();
        this.lockWrite = bb.readBoolean();
        this.error = bb.readBoolean();
        this.value = IOUtils.readObject(in);
        this.oldValue = IOUtils.readObject(in);
        this.key = new StateKey();
        this.key.read(in);
    }

    /**
     * Write state to a Buffer
     */
    public void write(BufferOutputStream out) throws IOException {
        super.write(out);
        out.writeUTF(this.type.toString());
        ByteBool bb = new ByteBool();
        bb.writeBoolean(this.mapUpdate);
        bb.writeBoolean(this.mapWrite);
        bb.writeBoolean(this.expired);
        bb.writeBoolean(this.lockExpired);
        bb.writeBoolean(this.lockUpdate);
        bb.writeBoolean(this.lockWrite);
        bb.writeBoolean(this.error);
        out.writeByte(bb.getData());
        IOUtils.writeObject(out, this.value);
        IOUtils.writeObject(out, this.oldValue);
        this.key.write(out);
    }

    /**
     * Get the type
     *
     * @return the type
     */
    public StateType getType() {
        return this.type;
    }

    /**
     * Set the type
     *
     * @param type the type to set
     */
    public void setType(StateType type) {
        this.type = type;
    }

    /**
     * @return a String
     * @see java.lang.Object#toString()
     */
    public String toString() {
        String result = "StateValue(" + this.type + ")key=" + this.key + ",value=" + this.value + ",oldValue="
                + this.oldValue;
        return result;
    }
}