Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18b6ce0bdc8b4b646254c2659db75178ec6bd7c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * 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.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.model.value;

import org.eclipse.jpt.utility.internal.BidiTransformer;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;

/**
 * A <code>CachingTransformationWritablePropertyValueModel<code> augments the
 * behavior of a <code>TransformationWritablePropertyValueModel<code> 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 <code>valueChanged(PropertyChangeEvent)</code> 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<T1, T2>
	extends TransformationWritablePropertyValueModel<T1, T2>
{

	/**
	 * 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 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
	 * <code>transform_(Object)</code> and <code>reverseTransform_(Object)</code>
	 * (or <code>transform(Object)</code> and <code>reverseTransform(Object)</code>)
	 * methods instead of building a <code>BidiTransformer</code>.
	 */
	public CachingTransformationWritablePropertyValueModel(WritablePropertyValueModel<T1> valueHolder) {
		super(valueHolder);
	}

	/**
	 * Construct a writable property value model with the specified nested
	 * writable property value model and bidi transformer.
	 */
	public CachingTransformationWritablePropertyValueModel(WritablePropertyValueModel<T1> valueHolder, BidiTransformer<T1, T2> 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() {
		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;
	}

}

Back to the top