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


public abstract class Condition
{
	abstract void append(Statement statment);
	
	abstract void check(TC tc);

	public final NotCondition not()
	{
		return new NotCondition(this);
	}
	
	public final CompositeCondition and(final Condition other)
	{
		return new CompositeCondition(CompositeCondition.Operator.AND, new Condition[]{this, other});
	}
	
	public final CompositeCondition or(final Condition other)
	{
		return new CompositeCondition(CompositeCondition.Operator.OR, new Condition[]{this, other});
	}
	
	@Override
	public abstract boolean equals(Object o);
	@Override
	public abstract int hashCode();

	static final boolean equals(final Object o1, final Object o2)
	{
		return o1==null ? o2==null : o1.equals(o2);
	}
	
	static final int hashCode(final Object o)
	{
		return o==null ? 0 : o.hashCode();
	}

	/**
	 * Does the same as {@link #toString()} with one exception:
	 * it calls {@link #toStringForQueryKey(Object)}
	 * instead of {@link Object#toString()} for
	 * literal values in conditions.
	 * This avoids errors, if {@link Object#toString()} of
	 * items is implemented in a way, that fails without
	 * an active transaction bound to the current thread.
	 * Such errors would occur in the query cache statistics tab
	 * of the COPE Console.
	 */
	abstract String toStringForQueryKey();
	
	static final String toStringForQueryKey(final Object o)
	{
		if(o==null)
			return "NULL";
		else if(o instanceof Item)
			return ((Item)o).getCopeID();
		else
			return o.toString();
	}
}

@@ -1075,37 +1075,33 @@
 %09%7B%0A%09%09return 
-new C
+c
 omposite
 Condition(Co
@@ -1084,33 +1084,24 @@
 rn composite
-Condition
 (CompositeCo
@@ -1125,37 +1125,14 @@
 AND,
- new Condition%5B%5D%7Bthis,
  other
-%7D
 );%0A%09
@@ -1206,19 +1206,85 @@
 %09return 
-new
+composite(CompositeCondition.Operator.OR, other);%0A%09%7D%0A%09%0A%09private final
  Composi
@@ -1294,17 +1294,33 @@
 ondition
-(
+ composite(final 
 Composit
@@ -1338,19 +1338,93 @@
 Operator
-.OR
+ operator, final Condition other)%0A%09%7B%0A%09%09return new CompositeCondition(operator
 , new Co

add helper method composite