Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c5d969714297ff1ff662722c4647ad7868491ea (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*******************************************************************************
 * Copyright (c) 2016 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.common.utility.internal.model.value;

import java.util.Arrays;
import java.util.Collection;
import org.eclipse.jpt.common.utility.closure.Closure;
import org.eclipse.jpt.common.utility.internal.closure.BooleanClosure;
import org.eclipse.jpt.common.utility.internal.closure.ClosureTools;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerTools;
import org.eclipse.jpt.common.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.predicate.Predicate;
import org.eclipse.jpt.common.utility.transformer.Transformer;

/**
 * {@link CollectionValueModel Collection value model} utility methods.
 */
public final class CollectionValueModelTools {

	// ********** composite PVMs **********

	/**
	 * Construct a composite property value model adapter for the specified
	 * transformer and property value models.
	 */
	@SafeVarargs
	public static <E, V> PropertyValueModel<V> compositePropertyValueModel(Transformer<? super Collection<E>, V> transformer, PropertyValueModel<? extends E>... propertyValueModels) {
		return compositePropertyValueModel(Arrays.asList(propertyValueModels), transformer);
	}

	/**
	 * Construct a composite property value model adapter for the specified
	 * property value models and transformer.
	 */
	public static <E, V> PropertyValueModel<V> compositePropertyValueModel(Collection<? extends PropertyValueModel<? extends E>> propertyValueModels, Transformer<? super Collection<E>, V> transformer) {
		return compositePropertyValueModel(new StaticCollectionValueModel<>(propertyValueModels), transformer);
	}

	/**
	 * Construct a composite property value model adapter for the specified
	 * collection value model and transformer.
	 * @see PluggablePropertyValueModel
	 */
	public static <E, V> PropertyValueModel<V> compositePropertyValueModel(CollectionValueModel<? extends PropertyValueModel<? extends E>> collectionModel, Transformer<? super Collection<E>, V> transformer) {
		return PropertyValueModelTools.propertyValueModel(new CompositePropertyValueModelAdapter.Factory<>(collectionModel, transformer));
	}


	// ********** collection meta data adapters **********

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model.
	 * If the collection is empty, the model's value is <code>null</code>;
	 * otherwise, it is the first element in the collection.
	 */
	public static <E> PropertyValueModel<E> firstElementPropertyValueModel(CollectionValueModel<? extends E> collectionModel) {
		return propertyValueModel(collectionModel, TransformerTools.collectionFirstElementTransformer());
	}

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model.
	 * If the collection is empty, the model's value is <code>null</code>;
	 * otherwise, it is the last element in the collection.
	 */
	public static <E> PropertyValueModel<E> lastElementPropertyValueModel(CollectionValueModel<? extends E> collectionModel) {
		return propertyValueModel(collectionModel, TransformerTools.collectionLastElementTransformer());
	}

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model.
	 * If the collection contains <em>only</em> a single element,
	 * the model's value is that element; otherwise,
	 * the model's value is <code>null</code>.
	 */
	public static <E> PropertyValueModel<E> singleElementPropertyValueModel(CollectionValueModel<? extends E> collectionModel) {
		return propertyValueModel(collectionModel, TransformerTools.collectionSingleElementTransformer());
	}

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model that returns whether the collection is <em>not</em> empty.
	 */
	public static PropertyValueModel<Boolean> isNotEmptyPropertyValueModel(CollectionValueModel<?> collectionModel) {
		return propertyValueModel(collectionModel, TransformerTools.collectionIsNotEmptyTransformer());
	}

	/**
	 * Construct a <em>modifiable</em> property value model adapter for the specified
	 * collection value model that returns whether the collection is <em>not</em> empty.
	 * This model can also be used to populate or clear the collection value model.
	 * The specified collection mutator is used to convert the collection value model:
	 * it should populate the collection value model when passed <code>true</code>
	 * and clear the collection value model when passed <code>false</code>.
	 */
	public static ModifiablePropertyValueModel<Boolean> isNotEmptyModifiablePropertyValueModel(CollectionValueModel<?> collectionModel, BooleanClosure.Adapter collectionMutator) {
		return booleanModifiablePropertyValueModel(collectionModel, TransformerTools.collectionIsNotEmptyTransformer(), collectionMutator);
	}

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model that returns whether the collection is empty.
	 */
	public static PropertyValueModel<Boolean> isEmptyPropertyValueModel(CollectionValueModel<?> collectionModel) {
		return propertyValueModel(collectionModel, TransformerTools.collectionIsEmptyTransformer());
	}

	/**
	 * Construct a <em>modifiable</em> property value model adapter for the specified
	 * collection value model that returns whether the collection is empty.
	 * This model can also be used to populate or clear the collection value model.
	 * The specified collection mutator is used to convert the collection value model:
	 * it should clear the collection value model when passed <code>true</code>
	 * and populate the collection value model when passed <code>false</code>.
	 */
	public static ModifiablePropertyValueModel<Boolean> isEmptyModifiablePropertyValueModel(CollectionValueModel<?> collectionModel, BooleanClosure.Adapter collectionMutator) {
		return booleanModifiablePropertyValueModel(collectionModel, TransformerTools.collectionIsEmptyTransformer(), collectionMutator);
	}

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model that returns whether the collection contains
	 * exactly one element.
	 */
	public static PropertyValueModel<Boolean> containsSingleElementPropertyValueModel(CollectionValueModel<?> collectionModel) {
		return propertyValueModel(collectionModel, TransformerTools.collectionContainsSingleElementTransformer());
	}

	/**
	 * Construct a property value model adapter for the specified
	 * collection value model that returns whether the collection's size
	 * equals the specified size.
	 */
	public static PropertyValueModel<Boolean> sizeEqualsPropertyValueModel(CollectionValueModel<?> collectionModel, int size) {
		return propertyValueModel(collectionModel, TransformerTools.collectionSizeEqualsTransformer(size));
	}


	// ********** boolean PVM adapters **********

	/**
	 * Construct a boolean property value model that is a composite AND of the
	 * specified boolean property value models; i.e. the model's value is {@link Boolean#TRUE}
	 * if all the contained models' values are {@link Boolean#TRUE},
	 * otherwise its value is {@link Boolean#FALSE}.
	 * The model's default value, when it contains no nested models, is {@link Boolean#TRUE}.
	 */
	public static PropertyValueModel<Boolean> and(CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) {
		return compositePropertyValueModel(collectionModel, PropertyValueModelTools.AND_TRANSFORMER);
	}

	/**
	 * Construct a boolean property value model that is a composite OR of the
	 * specified boolean property value models; i.e. the model's value is {@link Boolean#FALSE}
	 * if all the contained models' values are {@link Boolean#FALSE},
	 * otherwise its value is {@link Boolean#TRUE}.
	 * The model's default value, when it contains no nested models, is {@link Boolean#FALSE}.
	 */
	public static PropertyValueModel<Boolean> or(CollectionValueModel<? extends PropertyValueModel<Boolean>> collectionModel) {
		return compositePropertyValueModel(collectionModel, PropertyValueModelTools.OR_TRANSFORMER);
	}


	// ********** PVM adapters **********

	/**
	 * Construct a boolean property value model adapter for the specified
	 * collection value model and predicate. The returned model will indicate whether
	 * the collection belongs to the set defined by the predicate.
	 */
	public static PropertyValueModel<Boolean> booleanPropertyValueModel(CollectionValueModel<?> collectionModel, Predicate<? super Collection<?>> predicate) {
		return propertyValueModel(collectionModel, TransformerTools.adapt(predicate));
	}

	/**
	 * Construct a property value model adapted to the specified
	 * collection value model and transformer.
	 * @see PluggablePropertyValueModel
	 */
	public static <E, V> PropertyValueModel<V> propertyValueModel(CollectionValueModel<? extends E> collectionModel, Transformer<? super Collection<E>, V> transformer) {
		return PropertyValueModelTools.propertyValueModel(transformationPluggablePropertyValueModelAdapterFactory(collectionModel, transformer));
	}

	/**
	 * Construct a pluggable property value model adapter factory for the specified
	 * collection value model and transformer.
	 * @see PluggablePropertyValueModel
	 */
	public static <E, V> PluggablePropertyValueModel.Adapter.Factory<V> transformationPluggablePropertyValueModelAdapterFactory(CollectionValueModel<? extends E> collectionModel, Transformer<? super Collection<E>, V> transformer) {
		return new CollectionTransformationPluggablePropertyValueModelAdapter.Factory<>(collectionModel, transformer);
	}

	/**
	 * Construct a <em>modifiable</em> property value model adapted to the specified
	 * collection value model that returns whether the collection belongs to the set defined
	 * by the specified predicate.
	 * This model will use the specified collection mutator to modify the collection value model.
	 */
	public static ModifiablePropertyValueModel<Boolean> booleanModifiablePropertyValueModel(CollectionValueModel<?> collectionModel, Predicate<? super Collection<?>> predicate, BooleanClosure.Adapter collectionMutator) {
		return booleanModifiablePropertyValueModel(collectionModel, TransformerTools.adapt(predicate), collectionMutator);
	}

	/**
	 * Construct a <em>modifiable</em> property value model adapter for the specified
	 * collection value model that returns whether the collection belongs to the set defined
	 * by the specified transformer.
	 * This model will use the specified collection mutator to modify the collection value model.
	 */
	public static ModifiablePropertyValueModel<Boolean> booleanModifiablePropertyValueModel(CollectionValueModel<?> collectionModel, Transformer<? super Collection<?>, Boolean> transformer, BooleanClosure.Adapter collectionMutator) {
		PluggablePropertyValueModel.Adapter.Factory<Boolean> factory = transformationPluggablePropertyValueModelAdapterFactory(collectionModel, transformer);
		Closure<Boolean> closure = ClosureTools.booleanClosure(collectionMutator);
		return PropertyValueModelTools.pluggableModifiablePropertyValueModel(factory, closure);
	}


	// ********** filtering **********

	/**
	 * Construct a collection value model with the specified wrapped collection value model and filter.
	 */
	public static <E> CollectionValueModel<E> filter(CollectionValueModel<? extends E> collectionModel, Predicate<? super E> filter) {
		return new FilteringCollectionValueModel<>(collectionModel, filter);
	}


	// ********** suppressed constructor **********

	/**
	 * Suppress default constructor, ensuring non-instantiability.
	 */
	private CollectionValueModelTools() {
		super();
		throw new UnsupportedOperationException();
	}
}

Back to the top