Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a8c5217f44c5a7f4c858618a7fcf3a6f32ed0336 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * 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.eclipselink.ui.internal.caching;

import org.eclipse.jpt.eclipselink.core.internal.context.caching.CacheType;
import org.eclipse.jpt.eclipselink.core.internal.context.caching.Caching;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.model.AbstractModel;
import org.eclipse.jpt.utility.internal.model.value.PropertyAspectAdapter;
import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;

/**
 * EntityCacheProperties
 */
@SuppressWarnings("nls")
public class EntityCacheProperties extends AbstractModel {

	private Caching caching;
	private String entityName;
	
	private PropertyValueModel<CacheType> cacheTypeHolder;
	private PropertyChangeListener cacheTypeListener;
	private PropertyValueModel<Integer> cacheSizeHolder;
	private PropertyChangeListener cacheSizeListener;
	private PropertyValueModel<Boolean> sharedCacheHolder;
	private PropertyChangeListener sharedCacheListener;

	private static final long serialVersionUID = 1L;

	public static final String CACHE_TYPE_PROPERTY = Caching.CACHE_TYPE_PROPERTY;
	public static final String CACHE_SIZE_PROPERTY = Caching.CACHE_SIZE_PROPERTY;
	public static final String SHARED_CACHE_PROPERTY = Caching.SHARED_CACHE_PROPERTY;

	// ********** constructors **********
	public EntityCacheProperties(Caching caching, String entityName) {
		super();
		this.caching    = caching;
		this.entityName = entityName;
		
		PropertyValueModel<Caching> cachingHolder = new SimplePropertyValueModel<Caching>(this.caching);
		this.initialize(cachingHolder);
	}
	
	protected void initialize(PropertyValueModel<Caching> cachingHolder) {
		this.cacheTypeHolder = this.buildCacheTypeAA(cachingHolder);
		this.cacheTypeListener = this.buildCacheTypeChangeListener();
		this.cacheTypeHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.cacheTypeListener);

		this.cacheSizeHolder = this.buildCacheSizeAA(cachingHolder);
		this.cacheSizeListener = this.buildCacheSizeChangeListener();
		this.cacheSizeHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.cacheSizeListener);

		this.sharedCacheHolder = this.buildSharedCacheAA(cachingHolder);
		this.sharedCacheListener = this.buildSharedCacheChangeListener();
		this.sharedCacheHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.sharedCacheListener);
	}
	
	// ********** behavior **********
	public boolean entityNameIsValid() {
		return !StringTools.stringIsEmpty(this.entityName);
	}

	public Integer getCacheSize() {
		return this.caching.getCacheSize(this.entityName);
	}

	public CacheType getCacheType() {
		return this.caching.getCacheType(this.entityName);
	}

	public Caching getCaching() {
		return caching;
	}

	public Integer getDefaultCacheSize() {
		return this.caching.getDefaultCacheSize();
	}

	public CacheType getDefaultCacheType() {
		return this.caching.getDefaultCacheType();
	}

	public String getEntityName() {
		return this.entityName;
	}

	public Boolean getSharedCache() {
		return this.caching.getSharedCache(this.entityName);
	}

	public Boolean getDefaultSharedCache() {
		return this.caching.getDefaultSharedCache();
	}

	public void setCacheSize(Integer cacheSize) {
		Integer oldCacheSize = this.getCacheSize();
		if (this.attributeValueHasChanged(oldCacheSize, cacheSize)) {
			this.caching.setCacheSize(cacheSize, this.entityName);
			this.firePropertyChanged(CACHE_SIZE_PROPERTY, oldCacheSize, cacheSize);
		}
	}

	public void setCacheType(CacheType cacheType) {
		CacheType oldCacheType = this.getCacheType();
		if (this.attributeValueHasChanged(oldCacheType, cacheType)) {
			this.caching.setCacheType(cacheType, this.entityName);
			this.firePropertyChanged(CACHE_TYPE_PROPERTY, oldCacheType, cacheType);
		}
	}

	public void setSharedCache(Boolean sharedCache) {
		Boolean oldSharedCache = this.getSharedCache();
		if (this.attributeValueHasChanged(oldSharedCache, sharedCache)) {
			this.caching.setSharedCache(sharedCache, this.entityName);
			this.firePropertyChanged(SHARED_CACHE_PROPERTY, oldSharedCache, sharedCache);
		}
	}
	
	// ********** PropertyChangeListener **********
	
	private PropertyValueModel<CacheType> buildCacheTypeAA(PropertyValueModel<Caching> subjectHolder) {
		return new PropertyAspectAdapter<Caching, CacheType>(
								subjectHolder, CACHE_TYPE_PROPERTY) {
			@Override
			protected CacheType buildValue_() {
				return this.subject.getCacheType(EntityCacheProperties.this.entityName);
			}
		};
	}
	
	private PropertyChangeListener buildCacheTypeChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				EntityCacheProperties.this.firePropertyChanged(CACHE_TYPE_PROPERTY, e.getOldValue(), e.getNewValue());
			}
		};
	}
	
	private PropertyValueModel<Integer> buildCacheSizeAA(PropertyValueModel<Caching> subjectHolder) {
		return new PropertyAspectAdapter<Caching, Integer>(
								subjectHolder, CACHE_SIZE_PROPERTY) {
			@Override
			protected Integer buildValue_() {
				return this.subject.getCacheSize(EntityCacheProperties.this.entityName);
			}
		};
	}
	
	private PropertyChangeListener buildCacheSizeChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				EntityCacheProperties.this.firePropertyChanged(CACHE_SIZE_PROPERTY, e.getOldValue(), e.getNewValue());
			}
		};
	}
	
	private PropertyValueModel<Boolean> buildSharedCacheAA(PropertyValueModel<Caching> subjectHolder) {
		return new PropertyAspectAdapter<Caching, Boolean>(
								subjectHolder, SHARED_CACHE_PROPERTY) {
			@Override
			protected Boolean buildValue_() {
				return this.subject.getSharedCache(EntityCacheProperties.this.entityName);
			}
		};
	}
	
	private PropertyChangeListener buildSharedCacheChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				EntityCacheProperties.this.firePropertyChanged(SHARED_CACHE_PROPERTY, e.getOldValue(), e.getNewValue());
			}
		};
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		StringTools.buildSimpleToStringOn(this, sb);
		sb.append(" (");
		this.toString(sb);
		sb.append(')');
		return sb.toString();
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append("name: ");
		sb.append(this.entityName);
		sb.append(", type: ");
		sb.append(this.getCacheType());
		sb.append(", size: ");
		sb.append(this.getCacheSize());
		sb.append(", isShared: ");
		sb.append(this.getSharedCache());
	}
}

Back to the top