/**
 *  Copyright 2008 ThimbleWare 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 com.thimbleware.jmemcached;

import java.util.Arrays;

/**
 * Represents information about a cache entry.
 */
public final class LocalCacheElement implements CacheElement {
    private int expire ;
    private int flags;
    private byte[] data;
    private String keystring;
    private long casUnique = 0L;
    private boolean blocked = false;
    private long blockedUntil;

    public LocalCacheElement() {
    }

    public LocalCacheElement(String keystring) {
        this.keystring = keystring;
    }

    public LocalCacheElement(String keystring, int flags, int expire) {
        this.keystring = keystring;
        this.flags = flags;
        this.expire = expire;
    }

    /**
     * @return the current time in seconds
     */
    public static int Now() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    public int size() {
        return getData().length;
    }


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

        LocalCacheElement that = (LocalCacheElement) o;

        if (blocked != that.blocked) return false;
        if (blockedUntil != that.blockedUntil) return false;
        if (casUnique != that.casUnique) return false;
        if (expire != that.expire) return false;
        if (flags != that.flags) return false;
        if (!Arrays.equals(data, that.data)) return false;
        if (!keystring.equals(that.keystring)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = expire;
        result = 31 * result + flags;
        result = 31 * result + (data != null ? Arrays.hashCode(data) : 0);
        result = 31 * result + keystring.hashCode();
        result = 31 * result + (int) (casUnique ^ (casUnique >>> 32));
        result = 31 * result + (blocked ? 1 : 0);
        result = 31 * result + (int) (blockedUntil ^ (blockedUntil >>> 32));
        return result;
    }

    public static LocalCacheElement key(String key) {
        return new LocalCacheElement(key);
    }

    public int getExpire() {
        return expire;
    }

    public int getFlags() {
        return flags;
    }

    public byte[] getData() {
        return data;
    }

    public String getKeystring() {
        return keystring;
    }

    public long getCasUnique() {
        return casUnique;
    }

    public boolean isBlocked() {
        return blocked;
    }

    public long getBlockedUntil() {
        return blockedUntil;
    }

    public void setCasUnique(long casUnique) {
        this.casUnique = casUnique;
    }

    public void setBlockedUntil(long blockedUntil) {
        this.blockedUntil = blockedUntil;
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}
@@ -641,16 +641,132 @@
 ached;%0A%0A
+import java.io.Externalizable;%0Aimport java.io.IOException;%0Aimport java.io.ObjectInput;%0Aimport java.io.ObjectOutput;%0A
 import j
@@ -898,16 +898,32 @@
 eElement
+, Externalizable
  %7B%0A    p
@@ -3632,9 +3632,750 @@
 ;%0A    %7D%0A
+    %0A    public void readExternal(ObjectInput in) throws IOException%7B%0A%09%09  expire = in.readInt() ;%0A%09%09  flags = in.readInt();%0A%0A%09%09  final int length = in.readInt();%0A%09%09  int readSize = 0; %0A%09%09  data = new byte%5Blength%5D;%0A%09%09  while( readSize %3C length)%0A%09%09%09  readSize += in.read(data, readSize, length - readSize);%0A%09%09  %0A%09%09  keystring = in.readUTF();%0A%09%09  casUnique = in.readLong();%0A%09%09  blocked = in.readBoolean();%0A%09%09  blockedUntil = in.readLong();%0A%09%7D%0A%0A%09public void writeExternal(ObjectOutput out) throws IOException %7B%0A%09%09  out.writeInt(expire) ;%0A%09%09  out.writeInt(flags);%0A%09%09  out.writeInt(data.length);%0A%09%09  out.write(data);%09%09  %0A%09%09  out.writeUTF(keystring);%0A%09%09  out.writeLong(casUnique);%0A%09%09  out.writeBoolean(blocked);%0A%09%09  out.writeLong(blockedUntil);%0A%09%7D%0A
 %7D

Implement Externalizable