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

import fi.tnie.db.model.AbstractValueModel;
import fi.tnie.db.model.ChangeListener;
import fi.tnie.db.model.ValueModel;

/** 
 *
 * @param <V> Type of the value (computation result) of this model. 
 * @param <S> Type of the input of the computation this model represents.
 */
public abstract class AbstractTransformationModel<V, S>
	extends AbstractValueModel<V> {
	
	public AbstractTransformationModel(final ValueModel<S> source) {
		source.addChangeHandler(new ChangeListener<S>() {
			@Override
			public void changed(S from, S to) {
				V result = transform(to);
				fireIfChanged(get(), result);
			}
		});
	}
	
	/**
	 * @param source
	 * @return
	 */
	public abstract V transform(S source);
}

@@ -438,16 +438,49 @@
 %3CV%3E %7B%0A%09%0A
+%09private ValueModel%3CS%3E source;%0A%09%0A
 %09public 
@@ -649,16 +649,57 @@
 %09%09%09%09
-V result
+// change has already taken place:%0A%09%09%09%09V previous
  = t
@@ -711,12 +711,18 @@
 orm(
-to);
+from);%09%09%09%09
 %0A%09%09%09
@@ -740,33 +740,62 @@
 ged(
-get(), result);%0A%09%09%09%7D%0A%09%09%7D)
+previous, get());%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09%0A%09%09this.source = source
 ;%0A%09%7D
@@ -876,11 +876,143 @@
 source);
+%0A%0A%09public ValueModel%3CS%3E getSource() %7B%0A%09%09return source;%0A%09%7D%0A%09%0A%09@Override%0A%09public V get() %7B%09%09%0A%09%09return transform(getSource().get());%0A%09%7D
 %0A%7D%0A

Fix bug in AbstractTransformationModel