/**
 *Copyright [2009-2010] [dennis zhuang(killme2008@gmail.com)]
 *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
 */
/**
 *Copyright [2009-2010] [dennis zhuang(killme2008@gmail.com)]
 *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 net.rubyeye.xmemcached.command.text;

import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;

import net.rubyeye.xmemcached.command.Command;
import net.rubyeye.xmemcached.command.CommandType;
import net.rubyeye.xmemcached.impl.MemcachedTCPSession;
import net.rubyeye.xmemcached.monitor.Constants;
import net.rubyeye.xmemcached.utils.ByteUtils;

import com.google.code.yanf4j.buffer.IoBuffer;

/**
 * Delete command for text protocol
 * 
 * @author dennis
 * 
 */
public class TextDeleteCommand extends Command {

	protected int time;

	public TextDeleteCommand(String key, byte[] keyBytes, int time,
			final CountDownLatch latch, boolean noreply) {
		super(key, keyBytes, latch);
		this.commandType = CommandType.DELETE;
		this.time = time;
		this.noreply = noreply;
	}

	public final int getTime() {
		return this.time;
	}

	public final void setTime(int time) {
		this.time = time;
	}

	@Override
	public boolean decode(MemcachedTCPSession session, ByteBuffer buffer) {
		if (buffer == null || !buffer.hasRemaining()) {
			return false;
		}
		if (this.result == null) {
			byte first = buffer.get(buffer.position());
			if (first == 'D') {
				setResult(Boolean.TRUE);
				countDownLatch();
				// DELETED\r\n
				return ByteUtils.stepBuffer(buffer, 9);
			} else if (first == 'N') {
				setResult(Boolean.FALSE);
				countDownLatch();
				// NOT_FOUND\r\n
				return ByteUtils.stepBuffer(buffer, 11);
			} else {
				return decodeError(session, buffer);
			}
		} else {
			Boolean result = (Boolean) this.result;
			if (result) {
				return ByteUtils.stepBuffer(buffer, 9);
			} else {
				return ByteUtils.stepBuffer(buffer, 11);
			}
		}
	}

	@Override
	public final void encode() {

		int size = Constants.DELETE.length + 1 + this.keyBytes.length
				+ Constants.CRLF.length;
		if (this.time > 0) {
			size += 1 + ByteUtils.stringSize(this.time);
		}
		if (isNoreply()) {
			size += 8;
		}
		byte[] buf = new byte[size];
		if (isNoreply()) {
			if (this.time > 0) {
				ByteUtils.setArguments(buf, 0, Constants.DELETE, this.keyBytes,
						this.time, Constants.NO_REPLY);
			} else {
				ByteUtils.setArguments(buf, 0, Constants.DELETE, this.keyBytes,
						Constants.NO_REPLY);
			}
		} else {
			if (this.time > 0) {
				ByteUtils.setArguments(buf, 0, Constants.DELETE, this.keyBytes,
						this.time);
			} else {
				ByteUtils.setArguments(buf, 0, Constants.DELETE, this.keyBytes);
			}
		}
		this.ioBuffer = IoBuffer.wrap(buf);
	}

}

@@ -3038,18 +3038,16 @@
 de() %7B%0D%0A
-%0D%0A
 %09%09int si
@@ -3137,86 +3137,8 @@
 h;%0D%0A
-%09%09if (this.time %3E 0) %7B%0D%0A%09%09%09size += 1 + ByteUtils.stringSize(this.time);%0D%0A%09%09%7D%0D%0A
 %09%09if
@@ -3233,34 +3233,8 @@
  %7B%0D%0A
-%09%09%09if (this.time %3E 0) %7B%0D%0A%09
 %09%09%09B
@@ -3306,20 +3306,8 @@
 %09%09%09%09
-%09this.time, 
 Cons
@@ -3322,25 +3322,24 @@
 _REPLY);%0D%0A%09%09
-%09
 %7D else %7B%0D%0A%09%09
@@ -3328,33 +3328,32 @@
 );%0D%0A%09%09%7D else %7B%0D%0A
-%09
 %09%09%09ByteUtils.set
@@ -3405,257 +3405,10 @@
 ytes
-,%0D%0A%09%09%09%09%09%09Constants.NO_REPLY);%0D%0A%09%09%09%7D%0D%0A%09%09%7D else %7B%0D%0A%09%09%09if (this.time %3E 0) %7B%0D%0A%09%09%09%09ByteUtils.setArguments(buf, 0, Constants.DELETE, this.keyBytes,%0D%0A%09%09%09%09%09%09this.time);%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09ByteUtils.setArguments(buf, 0, Constants.DELETE, this.keyBytes);%0D%0A%09%09%09%7D
+);
 %0D%0A%09%09

Remove time in TextDeleteCommand