/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package fi.tnie.db.model;

public abstract class BinaryRelationModel<V>
	extends ImmutableBooleanModel {
	
	private MutableBooleanModel result = new MutableBooleanModel();

	public BinaryRelationModel(final ValueModel<V> a, final ValueModel<V> b) {		
		ChangeListener<V> cl = new ChangeListener<V>() {
			@Override
			public void changed(V from, V to) {				
				Boolean newResult = apply(a.get(), b.get());
				result.set(newResult);
			}
		};
		
		if (!a.isConstant()) {
			a.addChangeHandler(cl);
		}
		
		if (!b.isConstant()) {
			b.addChangeHandler(cl);
		}
		
		this.result.set(apply(a.get(), b.get()));
	}

	protected abstract Boolean apply(V a, V b);

	
	@Override
	public Registration addChangeHandler(ChangeListener<Boolean> ch) {
		return this.result.addChangeHandler(ch);
	}
	
	@Override
	public Boolean get() {
		return result.get();
	}

	public static class Gt<V extends Comparable<V>>
		extends BinaryRelationModel<V>
	{
		public Gt(ValueModel<V> a, ValueModel<V> b) {
			super(a, b);
		}
		
		@Override
		protected Boolean apply(V a, V b) {
			if (a == null || b == null) {
				return null;
			}
			
			int result = a.compareTo(b);
			return Boolean.valueOf(result > 0);
		}
	}
	
	public class Eq<V>
		extends BinaryRelationModel<V>
	{
		public Eq(ValueModel<V> a, ValueModel<V> b) {
			super(a, b);
		}
		
		@Override
		protected Boolean apply(V a, V b) {		
			return Boolean.valueOf(a.equals(b));
		}
	}
	
	public static class Lt<V extends Comparable<V>>
		extends BinaryRelationModel<V>
	{
		public Lt(ValueModel<V> a, ValueModel<V> b) {
			super(a, b);
		}
		
		@Override
		protected Boolean apply(V a, V b) {
			if (a == null || b == null) {
				return null;
			}
			
			int result = a.compareTo(b);
			return Boolean.valueOf(result < 0);
		}
	}
	
}

@@ -1253,17 +1253,17 @@
 lass Eq%3C
-V
+T
 %3E%0A%09%09exte
@@ -1278,33 +1278,33 @@
 ryRelationModel%3C
-V
+T
 %3E%0A%09%7B%0A%09%09public Eq
@@ -1307,33 +1307,33 @@
 c Eq(ValueModel%3C
-V
+T
 %3E a, ValueModel%3C
@@ -1324,33 +1324,33 @@
 %3E a, ValueModel%3C
-V
+T
 %3E b) %7B%0A%09%09%09super(
@@ -1397,30 +1397,30 @@
 olean apply(
-V
+T
  a, 
-V
+T
  b) %7B%09%09%0A%09%09%09r

Eliminate warning