/*
 * 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.EnumMap;

import com.exedio.cope.FunctionField;
import com.exedio.cope.Item;
import com.exedio.cope.Pattern;

public final class FieldMapLimited<K extends Enum<K>,V> extends Pattern
{
	private final Class<K> keyClass;
	private final EnumMap<K, FunctionField<V>> attributes;

	private FieldMapLimited(final Class<K> keyClass, final FunctionField<V> valueTemplate)
	{
		this.keyClass = keyClass;
		this.attributes = new EnumMap<K, FunctionField<V>>(keyClass);

		for(K key : keyClass.getEnumConstants())
		{
			final FunctionField<V> value = valueTemplate.copyFunctionField();
			registerSource(value);
			attributes.put(key, value);
		}
	}
	
	public static final <K extends Enum<K>,V> FieldMapLimited<K,V> newMap(final Class<K> keyClass, final FunctionField<V> value)
	{
		return new FieldMapLimited<K,V>(keyClass, value);
	}
	
	@Override
	public void initialize()
	{
		final String name = getName();
		
		for(K key : keyClass.getEnumConstants())
		{
			final FunctionField<V> value = attributes.get(key);
			initialize(value, name+key.name());
		}
	}
	
	public Class<K> getKeyClass()
	{
		return keyClass;
	}

	public FunctionField<V> getAttribute(final K key)
	{
		return attributes.get(key);
	}

	public V get(final Item item, final K key)
	{
		return attributes.get(key).get(item);
	}
	
	public void set(final Item item, final K key, final V value)
	{
		attributes.get(key).set(item, value);
	}
}

@@ -1110,25 +1110,21 @@
 eld%3CV%3E%3E 
-attribute
+field
 s;%0A%0A%09pri
@@ -1245,25 +1245,21 @@
 %0A%09%09this.
-attribute
+field
 s = new 
@@ -1444,25 +1444,21 @@
 ue);%0A%09%09%09
-attribute
+field
 s.put(ke
@@ -1820,25 +1820,21 @@
 value = 
-attribute
+field
 s.get(ke
@@ -2001,33 +2001,29 @@
 %09%7B%0A%09%09return 
-attribute
+field
 s.get(key);%0A
@@ -2082,25 +2082,21 @@
 %09return 
-attribute
+field
 s.get(ke
@@ -2185,17 +2185,13 @@
 %7B%0A%09%09
-attribute
+field
 s.ge

rename attribute to field