/*
 * Copyright (C) 2004-2006  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.pattern;

import java.util.Collections;
import java.util.Map;

import com.exedio.cope.SetValue;
import com.exedio.cope.EqualCondition;
import com.exedio.cope.FinalViolationException;
import com.exedio.cope.FunctionAttribute;
import com.exedio.cope.Item;
import com.exedio.cope.Join;
import com.exedio.cope.JoinedFunction;
import com.exedio.cope.LengthViolationException;
import com.exedio.cope.MandatoryViolationException;
import com.exedio.cope.NotEqualCondition;
import com.exedio.cope.Pattern;
import com.exedio.cope.Settable;
import com.exedio.cope.StringAttribute;
import com.exedio.cope.UniqueViolationException;
import com.exedio.cope.Attribute.Option;

public abstract class Hash extends Pattern implements Settable<String>
{
	private final StringAttribute storage;

	public Hash(final StringAttribute storage)
	{
		if(storage==null)
			throw new NullPointerException("hash storage must not be null");

		registerSource(this.storage = storage);
	}
	
	public Hash(final Option storageOption)
	{
		this(new StringAttribute(storageOption));
	}

	public void initialize()
	{
		if(!storage.isInitialized())
			initialize(storage, getName()+"Hash");
	}
	
	public final StringAttribute getStorage()
	{
		return storage;
	}
	
	public abstract String hash(String plainText);
	
	public final void set(final Item item, final String plainText)
		throws
			UniqueViolationException,
			MandatoryViolationException,
			LengthViolationException,
			FinalViolationException
	{
		storage.set(item, hash(plainText));
	}
	
	public final boolean check(final Item item, final String actualPlainText)
	{
		final String expectedHash = storage.get(item);
		final String actualHash = hash(actualPlainText);
		if(expectedHash==null)
			return actualHash==null;
		else
			return expectedHash.equals(actualHash);
	}
	
	public final SetValue map(final String value)
	{
		return new SetValue(this, value);
	}
	
	public final Map<? extends FunctionAttribute, ? extends Object> execute(final String value, final Item exceptionItem)
	{
		return Collections.singletonMap(storage, hash(value));
	}
	
	public final EqualCondition equal(final String value)
	{
		return new EqualCondition(storage, hash(value));
	}
	
	public final EqualCondition equal(final Join join, final String value)
	{
		return new EqualCondition(new JoinedFunction<String>(storage, join), hash(value));
	}

	public final NotEqualCondition notEqual(final String value)
	{
		return new NotEqualCondition(storage, hash(value));
	}
	
}

@@ -2914,32 +2914,40 @@
 l EqualCondition
+%3CString%3E
  equal(final Str
@@ -2979,32 +2979,40 @@
 w EqualCondition
+%3CString%3E
 (storage, hash(v
@@ -3052,16 +3052,24 @@
 ondition
+%3CString%3E
  equal(f
@@ -3134,16 +3134,24 @@
 ondition
+%3CString%3E
 (new Joi
@@ -3235,16 +3235,24 @@
 ondition
+%3CString%3E
  notEqua
@@ -3306,16 +3306,24 @@
 ondition
+%3CString%3E
 (storage

use generics for conditions