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

/**
 * A common super class for all patterns.
 * <p>
 * Patterns should be constructable in three different ways:
 * <dl>
 * <dt>1) by an explicit external source</dt>
 * <dd>
 * This is the most verbose kind of defining a pattern.
 * First the source for the pattern is created, such as:
 * <pre>static final StringAttribute source = new StringAttribute(OPTIONAL)</pre>
 * Then the pattern ist created using the previously defined source:
 * <pre>static final Hash hash = new MD5Hash(source)</pre>
 * </dd>
 * <dt>2) by an implicit external source</dt>
 * <dd>
 * More concisely the pattern can be constructed by defining the source
 * implicitely when the defining the pattern itself:
 * <pre>static final Hash hash = new MD5Hash(new StringAttribute(OPTIONAL))</pre>
 * </dd>
 * <dt>3) by an internal source</dt>
 * <dd>
 * Finally, the construction of the source can be done the the pattern itself:
 * <pre>static final Hash hash = new MD5Hash(OPTIONAL)</pre>
 * </dd>
 * </dl>
 * 
 * @author Ralf Wiebicke
 */
public abstract class Pattern extends Feature
{
	@Override
	final void initialize(final Type<? extends Item> type, final String name)
	{
		super.initialize(type, name);
		initialize();
		// TODO SOON check type of sources equals own type, introduce getSources
	}
	
	protected final void registerSource(final Attribute attribute)
	{
		attribute.registerPattern(this);
	}

	/**
	 * Here you can do additional initialization not yet done in the constructor.
	 * In this method you can call methods {@link #getType()} and {@link #getName()}
	 * for the first time.
	 */
	public void initialize()
	{
		// empty default implementation
	}
	
	protected final void initialize(final Attribute<? extends Object> attribute, final String name)
	{
		attribute.initialize(getType(), name);
	}
	
	protected final void initialize(final UniqueConstraint uniqueConstraint, final String name)
	{
		uniqueConstraint.initialize(getType(), name);
	}
	
	// Make non-final method from super class final
	public final Type<? extends Item> getType()
	{
		return super.getType();
	}
	
}

@@ -823,16 +823,45 @@
 .cope;%0A%0A
+import java.util.ArrayList;%0A%0A
 /**%0A * A
@@ -1914,16 +1914,127 @@
 ature%0A%7B%0A
+%09// TODO SOON introduce getSources%0A%09private final ArrayList%3CAttribute%3E sources = new ArrayList%3CAttribute%3E();%0A%09%0A
 %09@Overri
@@ -2036,16 +2036,16 @@
 verride%0A
-
 %09final v
@@ -2166,82 +2166,505 @@
 ();%0A
+%0A
 %09%09
-// TODO SOON check type of sources equals own type, introduce getSources
+for(final Attribute source : sources)%0A%09%09%7B%0A%09%09%09if(!source.isInitialized())%0A%09%09%09%09throw new RuntimeException(%22Source %22 + source + %22 of pattern %22 + this + %22 has not been initialized.%22);%0A%09%09%09final Type%3C? extends Item%3E sourceType = source.getType();%0A%09%09%09//System.out.println(%22----------check%22+source);%0A%09%09%09if(!sourceType.equals(type))%0A%09%09%09%09throw new RuntimeException(%22Source %22 + source + %22 of pattern %22 + this + %22 must be declared on the same type, expected %22 + type + %22, but was %22 + sourceType + '.');%0A%09%09%7D
 %0A%09%7D%0A
@@ -2728,24 +2728,24 @@
 tribute)%0A%09%7B%0A
-
 %09%09attribute.
@@ -2767,16 +2767,42 @@
 (this);%0A
+%09%09sources.add(attribute);%0A
 %09%7D%0A%0A%09/**

check type of sources equals own type