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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * An <code>attribute</code> represents a persistently
 * stored attribute of a {@link Type}.
 * Subclasses specify the type of information to be stored
 * in an attribute.
 * <p>
 * For an overview here is a
 * <a href="attributes.png">UML diagram</a>.
 * 
 * @author Ralf Wiebicke
 */
public abstract class Attribute extends Feature
{
	final boolean isfinal;
	final boolean mandatory;

	Attribute(final boolean isfinal, final boolean mandatory)
	{
		this.isfinal = isfinal;
		this.mandatory = mandatory;
	}
	
	public final boolean isFinal()
	{
		return isfinal;
	}
	
	public final boolean isMandatory()
	{
		return mandatory;
	}
	
	// patterns ---------------------------------------------------------------------
	
	private ArrayList<Pattern> patternsWhileTypeInitialization = null;
	private List<Pattern> patterns = null;
	
	void registerPattern(final Pattern pattern)
	{
		if(isInitialized())
			throw new RuntimeException("registerPattern cannot be called after initialization of the attribute.");
		if(pattern==null)
			throw new NullPointerException();
		
		if(patternsWhileTypeInitialization==null)
			patternsWhileTypeInitialization = new ArrayList<Pattern>();
		
		patternsWhileTypeInitialization.add(pattern);
	}
	
	public List<Pattern> getPatterns()
	{
		if(!isInitialized())
			throw new RuntimeException("getPatterns cannot be called before initialization of the attribute.");
		if(patterns==null)
			throw new RuntimeException();

		return patterns;
	}
	
	// second initialization phase ---------------------------------------------------

	private Column column;
	
	void initialize(final Type type, final String name)
	{
		super.initialize(type, name);
		
		final ArrayList patterns = patternsWhileTypeInitialization;
		patternsWhileTypeInitialization = null;
		this.patterns =
			patterns==null
			? Collections.<Pattern>emptyList()
			: Collections.<Pattern>unmodifiableList(Arrays.<Pattern>asList((Pattern[])patterns.toArray(new Pattern[patterns.size()])));
	}
	
	final void materialize(final Table table)
	{
		if(table==null)
			throw new NullPointerException();
		if(this.column!=null)
			throw new RuntimeException();

		this.column = createColumn(table, getName(), mandatory);
	}
	
	final Column getColumn()
	{
		if(this.column==null)
			throw new RuntimeException();

		return column;
	}
	
	/**
	 * Returns the name of database column for this attribute - use with care!
	 * <p>
	 * This information is needed only, if you want to access
	 * the database without cope.
	 * In this case you should really know, what you are doing.
	 * Please note, that this string may vary,
	 * if a cope model is configured for different databases.
	 * 
	 * @see Type#getTableName()
	 */
	public final String getColumnName()
	{
		return column.id;
	}
	
	abstract Column createColumn(Table table, String name, boolean notNull);
	
	public static class Option
	{
		public final boolean isFinal;
		public final boolean unique;
		public final boolean mandatory;

		Option(final boolean isFinal, final boolean unique, final boolean mandatory)
		{
			this.isFinal = isFinal;
			this.unique = unique;
			this.mandatory = mandatory;
		}
	}
	
}



@@ -2624,16 +2624,25 @@
 rrayList
+%3CPattern%3E
  pattern
@@ -2811,25 +2811,16 @@
 ections.
-%3CPattern%3E
 unmodifi
@@ -2839,35 +2839,15 @@
 ays.
-%3CPattern%3EasList((Pattern%5B%5D)
+asList(
 patt

fix warning