/**
 * 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 a StateKey
 */
public class StateKey implements Cloneable {
    private MemberImpl owner;
    private String key;
    private boolean locked;
    private boolean removeOnExit;
    private boolean releaseLockOnExit;
    private long expiration;
    private long lockExpiration;

    /**
     * Constructor
     */
    public StateKey(MemberImpl owner, String key) {
        this.owner = owner;
        this.key = key;
    }

    public StateKey() {
    }

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

    protected void copy(StateKey copy) {
        copy.owner = this.owner.clone();
        copy.key = this.key;
        copy.locked = this.locked;
        copy.removeOnExit = this.removeOnExit;
        copy.releaseLockOnExit = this.releaseLockOnExit;
        copy.expiration = this.expiration;
        copy.lockExpiration = this.lockExpiration;
    }

    /**
     * @return the owner
     */
    public MemberImpl getOwner() {
        return this.owner;
    }

    /**
     * @param member
     */
    public void setOwner(MemberImpl member) {
        this.owner = member;
    }

    /**
     * Get the key
     *
     * @return the key
     */
    public String getKey() {
        return this.key;
    }

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

    /**
     * Get the locked
     *
     * @return the locked
     */
    public boolean isLocked() {
        return this.locked;
    }

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

    /**
     * Get the removeOnExit
     *
     * @return the removeOnExit
     */
    public boolean isRemoveOnExit() {
        return this.removeOnExit;
    }

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

    /**
     * Get the releaseLockOnExit
     *
     * @return the releaseLockOnExit
     */
    public boolean isReleaseLockOnExit() {
        return this.releaseLockOnExit;
    }

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

    /**
     * Get the expiration
     *
     * @return the expiration
     */
    public long getExpiration() {
        return this.expiration;
    }

    /**
     * Set the expiration
     *
     * @param expiration the expiration to set
     */
    public void setExpiration(long expiration) {
        this.expiration = expiration;
    }

    /**
     * Get the lockExpiration
     *
     * @return the lockExpiration
     */
    public long getLockExpiration() {
        return this.lockExpiration;
    }

    /**
     * Set the lockExpiration
     *
     * @param lockExpiration the lockExpiration to set
     */
    public void setLockExpiration(long lockExpiration) {
        this.lockExpiration = lockExpiration;
    }

    /**
     * set time to live
     */
    public void setTimeToLive(long ttl) {
        if (ttl > 0) {
            setExpiration(ttl + System.currentTimeMillis());
        } else {
            setExpiration(0);
        }
    }

    /**
     * set lease time to live
     */
    public void setLockLeaseTime(long ttl) {
        if (ttl > 0) {
            setLockExpiration(ttl + System.currentTimeMillis());
        } else {
            setLockExpiration(0);
        }
    }

    /**
     * @return true if expired
     */
    public boolean isExpired() {
        return isExpired(System.currentTimeMillis());
    }

    /**
     * @return true if expired
     */
    public boolean isExpired(long currentTime) {
        boolean result = getExpiration() > 0 && getExpiration() < currentTime;
        return result;
    }

    /**
     * @return true if lock expired
     */
    public boolean isLockExpired() {
        return isLockExpired(System.currentTimeMillis());
    }

    /**
     * @return true if lock expired
     */
    public boolean isLockExpired(long currentTime) {
        return getLockExpiration() > 0 && getLockExpiration() < currentTime;
    }

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

    public boolean equals(Object obj) {
        boolean result = false;
        if (obj instanceof StateKey) {
            StateKey other = (StateKey) obj;
            result = other.getKey().equals(getKey());
        }
        return result;
    }

    public String toString() {
        return "key:" + getKey();
    }

    public void write(BufferOutputStream out) throws IOException {
        this.owner.write(out);
        out.writeUTF(this.key);
        ByteBool byteBool = new ByteBool();
        byteBool.writeBoolean(this.locked);
        byteBool.writeBoolean(this.removeOnExit);
        byteBool.writeBoolean(this.releaseLockOnExit);
        out.writeByte(byteBool.getData());
        out.writeLong(this.expiration);
        out.writeLong(this.lockExpiration);
    }

    public void read(BufferInputStream in) throws IOException {
        this.owner = new MemberImpl();
        this.owner.read(in);
        this.key = in.readUTF();
        ByteBool byteBool = new ByteBool();
        byteBool.setData(in.readByte());
        this.locked = byteBool.readBoolean();
        this.removeOnExit = byteBool.readBoolean();
        this.releaseLockOnExit = byteBool.readBoolean();
        this.expiration = in.readLong();
        this.lockExpiration = in.readLong();
    }
}
