/*
 * Copyright 2011 Jonathan Anderson
 *
 * 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 me.footlights.core.data;

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;

import me.footlights.core.Preconditions;


/**
 * Encrypted block data.
 * @author Jonathan Anderson (jon@footlights.me)
 */
public class EncryptedBlock
{
	public static class Builder
	{
		public EncryptedBlock build() throws GeneralSecurityException
		{
			return new EncryptedBlock(ciphertext, link.decrypt(ciphertext), link);
		}

		public Builder setCiphertext(ByteBuffer ciphertext)
		{
			this.ciphertext = ciphertext;
			return this;
		}
		public Builder setLink(Link link)
		{
			this.link = link;
			return this;
		}

		private Builder() {}

		private ByteBuffer ciphertext;
		private Link link;
	}

	public static Builder newBuilder() { return new Builder(); }

	/** The name of the block. */
	public String name() { return link.uri().toString(); }
	@Override public String toString() { return name(); }

	/** The ciphertext itself. */
	public ByteBuffer ciphertext() { return ciphertext.asReadOnlyBuffer(); }

	/** Decrypted plaintext. */
	public Block plaintext() { return plaintext; }

	/** A link to the ciphertext. */
	public Link link() { return link; }


	private EncryptedBlock(ByteBuffer ciphertext, Block plaintext, Link link)
	{
		Preconditions.notNull(ciphertext, plaintext, link);

		this.ciphertext = ciphertext.asReadOnlyBuffer();
		this.plaintext = plaintext;
		this.link = link;
	}

	private final ByteBuffer ciphertext;
	private final Block plaintext;
	private final Link link;
}

@@ -1776,16 +1776,355 @@
 nk; %7D%0A%0A%0A
+%09@Override public boolean equals(Object o)%0A%09%7B%0A%09%09if (o == null) return false;%0A%09%09if (!(o instanceof EncryptedBlock)) return false;%0A%09%09EncryptedBlock e = (EncryptedBlock) o;%0A%0A%09%09if (!link.equals(e.link)) return false;%0A%09%09if (!ciphertext.equals(e.ciphertext)) return false;%0A%09%09if (!plaintext.equals(e.plaintext)) return false;%0A%0A%09%09return true;%0A%09%7D%0A%0A
 %09private

Add EncryptedBlock.equals().