From 3cf61d45b2081fb58975c506e598cf2445ec8d10 Mon Sep 17 00:00:00 2001
From: bvosburgh
Date: Tue, 9 Sep 2008 16:52:41 +0000
Subject: reviewed new CachingTransformationPVMs - minor tweaks, comments
---
.../CachingTransformationPropertyValueModel.java | 48 +++++++++-----
...ngTransformationWritablePropertyValueModel.java | 73 ++++++++++++++--------
...chingTransformationPropertyValueModelTests.java | 10 +--
...nsformationWritablePropertyValueModelTests.java | 3 +-
4 files changed, 88 insertions(+), 46 deletions(-)
diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationPropertyValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationPropertyValueModel.java
index 3f584aa496..a2d1e6157d 100644
--- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationPropertyValueModel.java
+++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationPropertyValueModel.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2008 Oracle. All rights reserved.
+ * Copyright (c) 2008 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -15,9 +15,12 @@ import org.eclipse.jpt.utility.model.value.PropertyValueModel;
/**
* A CachingTransformationPropertyValueModel
wraps another
* PropertyValueModel
and uses a Transformer
- * to transform the wrapped value before it is returned by value()
.
- * It also caches the value so that a transform is not done on the oldValue when
- * firing a property change event.
+ * to transform the wrapped value before it is returned by getValue()
.
+ * The transformed value is calculated and cached during initialization and every
+ * time the wrapped value changes. This can be useful when the old value
+ * passed in to valueChanged(PropertyChangeEvent)
can no longer
+ * be "transformed" because its state is no longer valid.
+ * This caching can also improve time performance in some situations.
*
* As an alternative to building a Transformer
,
* a subclass of CachingTransformationPropertyValueModel
can
@@ -27,16 +30,16 @@ import org.eclipse.jpt.utility.model.value.PropertyValueModel;
*/
public class CachingTransformationPropertyValueModel
extends TransformationPropertyValueModel
- implements PropertyValueModel
{
/**
* Cache the transformed value so that during property change event notification
- * we do not have to transform the value. The oldValue could no longer be valid in
- * the model, thus transforming it would not be valid.
+ * we do not have to transform the old value. The old value could no longer be valid in
+ * the model; as a result, transforming it would not be valid.
*/
protected T2 cachedValue;
-
+
+
// ********** constructors/initialization **********
/**
@@ -57,40 +60,53 @@ public class CachingTransformationPropertyValueModel
public CachingTransformationPropertyValueModel(PropertyValueModel extends T1> valueHolder, Transformer transformer) {
super(valueHolder, transformer);
}
-
+
+
// ********** behavior **********
+ /**
+ * We have listeners, transform the nested value and cache the result.
+ */
@Override
protected void engageValueHolder() {
super.engageValueHolder();
this.cachedValue = this.transform(this.valueHolder.getValue());
}
-
+
+ /**
+ * We have no more listeners, clear the cached value.
+ */
@Override
protected void disengageValueHolder() {
- super.disengageValueHolder();
this.cachedValue = null;
+ super.disengageValueHolder();
}
+ /**
+ * No need to transform the nested value, simply return the cached value,
+ * which is already transformed.
+ */
@Override
public T2 getValue() {
return this.cachedValue;
}
-
+
/**
- * Transform the new value, caching it before returning it
+ * Transform the specified new value, caching it before returning it.
*/
@Override
protected T2 transformNew(T1 value) {
this.cachedValue = super.transformNew(value);
return this.cachedValue;
- };
-
+ }
+
/**
- * no transformation, just return the cachedValue which is already transformed
+ * No need to transform the old value, simply return the cached value,
+ * which is already transformed.
*/
@Override
protected T2 transformOld(T1 value) {
return this.cachedValue;
}
+
}
diff --git a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationWritablePropertyValueModel.java b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationWritablePropertyValueModel.java
index 1d97e46fca..18b6ce0bdc 100644
--- a/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationWritablePropertyValueModel.java
+++ b/jpa/plugins/org.eclipse.jpt.utility/src/org/eclipse/jpt/utility/internal/model/value/CachingTransformationWritablePropertyValueModel.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2008 Oracle. All rights reserved.
+ * Copyright (c) 2008 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -10,75 +10,98 @@
package org.eclipse.jpt.utility.internal.model.value;
import org.eclipse.jpt.utility.internal.BidiTransformer;
-import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
/**
- * A TransformationPropertyValueModel
wraps another
- * PropertyValueModel
and uses a Transformer
- * to transform the wrapped value before it is returned by value()
.
- *
- * As an alternative to building a Transformer
,
- * a subclass of TransformationPropertyValueModel
can
- * either override the transform_(Object)
method or,
- * if something other than null should be returned when the wrapped value
- * is null, override the transform(Object)
method.
+ * A CachingTransformationWritablePropertyValueModel augments the
+ * behavior of a TransformationWritablePropertyValueModel by caching
+ * the transformed value.
+ * The transformed value is calculated and cached during initialization and every
+ * time the wrapped value changes. This can be useful when the old value
+ * passed in to valueChanged(PropertyChangeEvent)
can no longer
+ * be "transformed" because its state is no longer valid.
+ * This caching can also improve time performance in some situations.
*/
public class CachingTransformationWritablePropertyValueModel
extends TransformationWritablePropertyValueModel
- implements PropertyValueModel
{
+ /**
+ * Cache the transformed value so that during property change event notification
+ * we do not have to transform the old value. The old value could no longer be valid in
+ * the model; as a result, transforming it would not be valid.
+ */
protected T2 cachedValue;
-
+
+
// ********** constructors/initialization **********
/**
- * Construct a property value model with the specified nested
- * property value model and the default transformer.
+ * Construct a writable property value model with the specified nested
+ * writable property value model and the default bidi transformer.
* Use this constructor if you want to override the
- * transform_(Object)
or transform(Object)
- * method instead of building a Transformer
.
+ * transform_(Object)
and reverseTransform_(Object)
+ * (or transform(Object)
and reverseTransform(Object)
)
+ * methods instead of building a BidiTransformer
.
*/
public CachingTransformationWritablePropertyValueModel(WritablePropertyValueModel valueHolder) {
super(valueHolder);
}
/**
- * Construct an property value model with the specified nested
- * property value model and transformer.
+ * Construct a writable property value model with the specified nested
+ * writable property value model and bidi transformer.
*/
public CachingTransformationWritablePropertyValueModel(WritablePropertyValueModel valueHolder, BidiTransformer transformer) {
super(valueHolder, transformer);
}
-
+
+
// ********** behavior **********
+ /**
+ * We have listeners, transform the nested value and cache the result.
+ */
@Override
protected void engageValueHolder() {
super.engageValueHolder();
this.cachedValue = this.transform(this.valueHolder.getValue());
}
-
+
+ /**
+ * We have no more listeners, clear the cached value.
+ */
@Override
protected void disengageValueHolder() {
- super.disengageValueHolder();
this.cachedValue = null;
+ super.disengageValueHolder();
}
+ /**
+ * No need to transform the nested value, simply return the cached value,
+ * which is already transformed.
+ */
@Override
public T2 getValue() {
return this.cachedValue;
}
-
+
+ /**
+ * Transform the specified new value, caching it before returning it.
+ */
@Override
protected T2 transformNew(T1 value) {
this.cachedValue = super.transformNew(value);
return this.cachedValue;
- };
-
+ }
+
+ /**
+ * No need to transform the old value, simply return the cached value,
+ * which is already transformed.
+ */
@Override
protected T2 transformOld(T1 value) {
return this.cachedValue;
}
+
}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationPropertyValueModelTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationPropertyValueModelTests.java
index fd6ff2ae34..62c56b993f 100644
--- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationPropertyValueModelTests.java
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationPropertyValueModelTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2008 Oracle. All rights reserved.
+ * Copyright (c) 2008 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -20,6 +20,7 @@ import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
import org.eclipse.jpt.utility.tests.internal.TestTools;
+@SuppressWarnings("nls")
public class CachingTransformationPropertyValueModelTests extends TestCase {
private WritablePropertyValueModel objectHolder;
PropertyChangeEvent event;
@@ -58,7 +59,6 @@ public class CachingTransformationPropertyValueModelTests extends TestCase {
public void testValue() {
PropertyChangeListener listener = this.buildTransformationListener();
this.transformationObjectHolder.addPropertyChangeListener(listener);
-
Person person = this.objectHolder.getValue();
assertEquals("Karen", person.getName());
@@ -174,10 +174,10 @@ public class CachingTransformationPropertyValueModelTests extends TestCase {
private class Person extends AbstractModel {
private String name;
- public static final String NAME_PROPERTY = "nameProperty";
+ public static final String NAME_PROPERTY = "name";
private String parentName;
- public static final String PARENT_NAME_PROPERTY = "parentNameProperty";
+ public static final String PARENT_NAME_PROPERTY = "parentName";
private Person child;
@@ -214,5 +214,7 @@ public class CachingTransformationPropertyValueModelTests extends TestCase {
public Person getChild() {
return this.child;
}
+
}
+
}
diff --git a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationWritablePropertyValueModelTests.java b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationWritablePropertyValueModelTests.java
index 023c3134dd..72fd1288c3 100644
--- a/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationWritablePropertyValueModelTests.java
+++ b/jpa/tests/org.eclipse.jpt.utility.tests/src/org/eclipse/jpt/utility/tests/internal/model/value/CachingTransformationWritablePropertyValueModelTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2008 Oracle. All rights reserved.
+ * Copyright (c) 2008 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -21,6 +21,7 @@ import org.eclipse.jpt.utility.tests.internal.TestTools;
import junit.framework.TestCase;
+@SuppressWarnings("nls")
public class CachingTransformationWritablePropertyValueModelTests extends TestCase {
private WritablePropertyValueModel objectHolder;
PropertyChangeEvent event;
--
cgit v1.2.3