/*
 * Copyright (C) 2004-2005  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.Collections;
import java.util.List;


public abstract class Attribute extends Feature
{
	private final boolean readOnly;
	private final boolean mandatory;

	protected Attribute(final Option option)
	{
		this.readOnly = option.readOnly;
		this.mandatory = option.mandatory;
	}
	
	public final boolean isReadOnly()
	{
		return readOnly;
	}
	
	public final boolean isMandatory()
	{
		return mandatory;
	}
	
	// patterns ---------------------------------------------------------------------
	
	ArrayList patterns = null;
	
	public 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(patterns==null)
			patterns = new ArrayList();
		
		patterns.add(pattern);
	}
	
	public List getPatterns()
	{
		if(!isInitialized())
			throw new RuntimeException("getPatterns cannot be called before initialization of the attribute.");

		return patterns==null ? Collections.EMPTY_LIST : Collections.unmodifiableList(patterns); // make this permanent
	}
	
	// second initialization phase ---------------------------------------------------

	private Column column;
	
	void initialize(final Type type, final String name)
	{
		super.initialize(type, name);
		type.registerInitialization(this);
	}
	
	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;
	}
	
	public final String toString()
	{
		// should be precomputed
		final StringBuffer buf = new StringBuffer();
		buf.append(super.toString());
		buf.append('{');
		boolean first = true;
		if(readOnly)
		{
			if(first)
				first = false;
			else
				buf.append(',');
			buf.append("read-only");
		}
		if(mandatory)
		{
			if(first)
				first = false;
			else
				buf.append(',');
			buf.append("mandatory");
		}
		buf.append('}');

		return buf.toString();
	}
	
	protected abstract Column createColumn(Table table, String name, boolean notNull);
	
	public static class Option
	{
		public final boolean readOnly;
		public final boolean unique;
		public final boolean mandatory;

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



@@ -1357,21 +1357,83 @@
 ----%0A%09%0A%09
-Array
+private ArrayList patternsWhileTypeInitialization = null;%0A%09private 
 List pat
@@ -1708,27 +1708,73 @@
 erns
-==null)%0A%09%09%09patterns
+WhileTypeInitialization==null)%0A%09%09%09patternsWhileTypeInitialization
  = n
@@ -1802,16 +1802,39 @@
 patterns
+WhileTypeInitialization
 .add(pat
@@ -2001,26 +2001,21 @@
 ute.%22);%0A
-%0A
 %09%09
-return 
+if(
 patterns
@@ -2024,98 +2024,62 @@
 null
- ? Collections.EMPTY_LIST : Collections.unmodifiableList(patterns); // make this permanent
+)%0A%09%09%09throw new RuntimeException();%0A%0A%09%09return patterns;
 %0A%09%7D%0A
@@ -2316,16 +2316,232 @@
 (this);%0A
+%09%09%0A%09%09final ArrayList patterns = patternsWhileTypeInitialization;%0A%09%09patternsWhileTypeInitialization = null;%0A%09%09this.patterns =%0A%09%09%09patterns==null%0A%09%09%09? Collections.EMPTY_LIST%0A%09%09%09: Collections.unmodifiableList(patterns);%0A
 %09%7D%0A%09%0A%09fi

make patterns permanent