Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93bb9618754976acbbd2425c0ec9ade9d8c73565 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*******************************************************************************
 * Copyright (c) 2008, 2010 Borland Software Corporation and others.
 * 
 * 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:
 *     Borland Software Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2m.tests.qvt.oml.bbox;

import generics.GenericCls;
import generics.GenericsFactory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.m2m.internal.qvt.oml.expressions.DirectionKind;
import org.eclipse.m2m.internal.qvt.oml.stdlib.MutableListImpl;
import org.eclipse.m2m.qvt.oml.blackbox.java.Operation;
import org.eclipse.m2m.qvt.oml.blackbox.java.Parameter;
import org.eclipse.m2m.qvt.oml.blackbox.java.Operation.Kind;
import org.eclipse.m2m.qvt.oml.util.Dictionary;
import org.eclipse.m2m.qvt.oml.util.IContext;
import org.eclipse.m2m.qvt.oml.util.MutableList;
import org.eclipse.m2m.qvt.oml.util.Utils;
import org.eclipse.ocl.util.Bag;
import org.eclipse.ocl.util.CollectionUtil;

import simpleuml.Model;


public class AnnotatedJavaLibrary {
	
	private String myState;
	
	public AnnotatedJavaLibrary() {
		 super();
	}

	@Operation (kind=Kind.HELPER, contextual = true)
	public <T> T genericListElementAt(MutableList<T> self, int index) {
		return self.get(index - 1);
	}
	
	@Operation (kind=Kind.HELPER, contextual = false)
	public MutableList<Set<Boolean>> newListOfBooleanSetsFromBooleanCollection(Collection<Boolean> elements) {
		final MutableListImpl<Set<Boolean>> result = new MutableListImpl<Set<Boolean>>();
		result.add(new HashSet<Boolean>(elements));
		return result;
	}
		
	@Operation (kind=Kind.HELPER, contextual = false)
	public MutableList<String> newList(Collection<String> elements) {
		return new MutableListImpl<String>(elements);
	}	
	
	@Operation (kind=Kind.HELPER, contextual = false)
	public MutableList<String> echoList(MutableList<String> listType) {
		return new MutableListImpl<String>(listType);
	}
	
	@Operation (kind=Kind.HELPER, contextual = false)
	public ArrayList<Object> primitiveTypeArgumentsHelper(String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}
	
	@Operation (kind=Kind.QUERY, contextual = false)
	public ArrayList<Object> primitiveTypeArgumentsQuery(String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}
	
	@Operation(contextual = false)
	public ArrayList<Object> primitiveTypeArgumentsOperation(String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}

	public ArrayList<Object> primitiveTypeArgumentsDefault(String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}
	
	
	// contextual operations
	private Map<String, Integer> fCounters = new HashMap<String, Integer>();
	
	@Operation (contextual = true)
	public int getStrCounterX(String self) {
		Integer val = fCounters.get(self);
		if(val == null) {
			return 0;
		}
		return fCounters.get(self);
	}

	@Operation (contextual = true)
	public int incStrCounterX(String self) {
		Integer val = fCounters.get(self);
		if(val == null) {
			fCounters.put(self, 1);
		} else {
			fCounters.put(self, ++val);
		}
		return fCounters.get(self);
	}	
	
	@Operation (kind=Kind.HELPER, contextual = true)
	public static ArrayList<Object> primitiveTypeArgumentsHelperContextual(EAnnotation self, String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}
	
	@Operation (kind=Kind.QUERY, contextual = true)
	public static ArrayList<Object> primitiveTypeArgumentsQueryContextual(EAnnotation self, String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}
	
	@Operation (contextual = true)
	public static ArrayList<Object> primitiveTypeArgumentsOperationContextual(EAnnotation self, String strArg, Integer intArg, Double realArg, Boolean boolArg) {
		ArrayList<Object> result = new ArrayList<Object>();
		result.add(strArg);
		result.add(intArg);
		result.add(realArg);
		result.add(boolArg);		
		return result;
	}

	
	@Operation (kind=Kind.HELPER, contextual = true)
	public static String testContextualSuffixedName(
			@Parameter(name="eNamedElement") ENamedElement eNamedElement, 
			@Parameter(name="suffix") String suffix) {
		return eNamedElement.getName() + suffix;
	}
	
	@Operation (kind=Kind.HELPER)
	public String testEchoArgument(String param) {
		return param;
	}

	@Operation (kind=Kind.QUERY)
	public String staticTestEchoArgument(String param) {
		return param;
	}	
	
	@Operation(contextual=true)
	public static Set<ArrayList<String>> wrapInSet(ArrayList<String> sequence) {
		Set<ArrayList<String>> result = CollectionUtil.createNewSet();
		result.add(sequence);
		return result;
	}	
	@Operation(contextual=true)
	public static Set<ArrayList<Object>> commonCollectionInSet(Collection<Object> sequence) {
		Set<ArrayList<Object>> result = CollectionUtil.createNewSet();
		result.add(new ArrayList<Object>(sequence));
		return result;
	}		

	public Set<LinkedHashSet<List<Bag<String>>>> testComplexTypeCopy(Set<LinkedHashSet<ArrayList<Bag<String>>>> complexType) {
		String element = complexType.iterator().next().iterator()
			.next().iterator().next().iterator().next();
				
		final Bag<String> createNewBag = CollectionUtil.createNewBag();
		createNewBag.add(element);
		final List<Bag<String>> createNewSequence = CollectionUtil.createNewSequence();
		createNewSequence.add(createNewBag);
		final LinkedHashSet<List<Bag<String>>> createNewOrderedSet = CollectionUtil.createNewOrderedSet();
		createNewOrderedSet.add(createNewSequence);
		final Set<LinkedHashSet<List<Bag<String>>>> result = CollectionUtil.createNewSet();
		result.add(createNewOrderedSet);
		return result; 
	}
	
	public void noReturnType(String str) {
		// do nothing, testing void return type
	}
	
	public Model testUsedSimpleUMLModelPackage(Model model) {
		return model;
	}
	
	public String getEnumLiteralName(DirectionKind directionKind) {
		return directionKind.getName();
	}
	
	// TODO - add support for java big numbers
	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Numeric types operation
	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public BigInteger createBigInteger(String value) {
		return new BigInteger(value);
	}
		
	public BigInteger addBigInteger(BigInteger self, BigInteger big2) {
		return self.add(big2);
	}
	
	public BigDecimal createBigDecimal(String value) {
		return new BigDecimal(value);
	}
	
	public BigDecimal addBigDecimal(BigDecimal big1, BigDecimal big2) {
		return big1.add(big2);
	}

	// test lower precision types 
	public Long sumLowerPrecisions(Short s, Integer i, Long l) {
		// Byte b,
		return /*b.byteValue() + */ s.shortValue() + i.intValue() + l.longValue();		
	}

	// TODO - byte not mapped by MDT OCL, raise bugzilla 
	public long sumLowerPrecisionWithoutWrappers(/*byte b, */short s, int i, long l) {
		return /*b + */ s + i + l;
	}
	
	public Double sumLowerPrecisionDoubles(Float f, Double d) {
		return f.floatValue() + d.doubleValue();
	}

	public double sumLowerPrecisionDoublesWithoutWrappers(float f, double d) {
		return f + d;
	}
	
	@Operation(contextual=true)
	public static boolean theSameJavaBigIntegerInstance(BigInteger self, BigInteger another) {
		return self == another;
	}
	
	@Operation(contextual=true)
	public static String stringContextualEcho(String self) {
		return self;
	}

	@Operation(contextual=true)
	public static boolean nonObjectBoolContextualEcho(boolean self) {
		return self;
	}	
	
	@Operation(contextual=true)
	public static int nonObjectIntContextualEcho(int self) {
		return self;
	}
	
	@Operation(contextual=true)
	public static double nonObjectDoubleContextualEcho(double self) {
		return self;
	}
	
	@Operation(contextual=true)
	public static boolean boolContextualEcho(boolean self) {
		return self;
	}	
	
	@Operation(contextual=true)
	public static int intContextualEcho(int self) {
		return self;
	}
	
	@Operation(contextual=true)
	public static double doubleContextualEcho(double self) {
		return self;
	}

	// test data type not mapped by MDT OCL 
	public Date createDate(String dateStr) {
		return (Date)EcoreFactory.eINSTANCE.createFromString(EcorePackage.eINSTANCE.getEDate(), dateStr);
	}
	
	@Operation(contextual=true)
	public static boolean before(Date self, Date when) {
		return self.before(when);
	}		
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Collection type operation
	// TODO - MDT OCL does not support the type-checker extensibility fully yet to
	// realize collection operations  
	@Operation (kind=Kind.HELPER, contextual = true)
	public static <T> int countIgnoreUndefined(Collection<T> self) {
		int count = 0;
		if(self != null) {
			for (T element : self) {
				if(element != null) {
					count++;
				}
			}
		}

		return count;
	}
		
	// tests T, T2 parameterized types together
	@Operation (contextual = true)
	public static <T, T2> Collection<T2> shrinkAnotherCollectionToMySize(Collection<T> self, Collection<T2> another) {
		return new ArrayList<T2>(another).subList(0, self.size());
	}	
	
	public String foo() {
		return null;
	}
	
	
	@Operation (kind=Kind.HELPER, contextual = true)
	public static <T> T anyElement(Collection<T> self) {
		return self.iterator().next();
	}
	
	@Operation (kind=Kind.OPERATION, contextual = true)
	public static <T> Set<T> echoAsSet(Collection<T> self) {
		return new HashSet<T>(self);
	}
	
	public void setLibState(String newState) {
		myState = newState;
	}
	
	public String getLibState() {
		return myState;
	}
	
	// test we have access to the library state 
	@Operation (kind=Kind.HELPER, contextual = true)
	public String appendToLibState(String aString) {
		return myState = myState + aString; 
	}
	
	@Operation(contextual=true)
	public Dictionary<String, String> toString2StringDict(Dictionary<Integer, String> self) {
		Dictionary<String, String> result = Utils.createDictionary();
		for (Integer key : self.keys()) {
			result.put(key.toString(), self.get(key));
		}
		return result;
	}

	@Operation (kind=Kind.HELPER, withExecutionContext=true)
	public void logToConsole(IContext context, String param) {
		context.getLog().log(param);
	}

	@Operation (withExecutionContext=true)
	public void cancelExecution(IContext context) {
		context.getMonitor().cancel();
	}
	
	@Operation (contextual=true, withExecutionContext=true)
	public String getConfigProperty(IContext context, String strContext) {
		return context.getConfigProperty(strContext).toString();
	}
	
	@Operation (kind=Kind.OPERATION, contextual=true, withExecutionContext=true)
	public String getConfigProperty2(IContext context, String strContext, String value) {
		return context.getConfigProperty(strContext+value).toString();
	}
	
	/*
	 * This library method is used for testing UML2-like behavior 
	 * of org.eclipse.uml2.uml.internal.operations.ElementOperations::applyStereotype(..)
	 */
	public void createStereotypeApplication(ENamedElement element) {
		if (element.eResource() != null) {
			EClass stereotypeApplication = (EClass) EcoreUtil.create(EcorePackage.Literals.ECLASS);
			stereotypeApplication.setName(element.getName() + " : stereotypeApplication"); //$NON-NLS-1$
			element.eResource().getContents().add(stereotypeApplication);
		}
	}
	
	@Operation (kind=Kind.QUERY)
	public void testThrowRuntimeException() {
		throw new RuntimeException("Exception from bbox library"); //$NON-NLS-1$
	}
	
	// EMF generic test
	//
	@Operation (kind=Kind.QUERY)
	public GenericCls<?> getGenericClsWildcard() {
		GenericCls<Integer> genericCls = GenericsFactory.eINSTANCE.<Integer>createGenericCls();
		genericCls.getValues().add(1);
		return genericCls;
	}

	@Operation (kind=Kind.QUERY)
	public GenericCls<Integer> getGenericClsInteger() {
		GenericCls<Integer> genericCls = GenericsFactory.eINSTANCE.<Integer>createGenericCls();
		genericCls.getValues().add(1);
		return genericCls;
	}

	@Operation (kind=Kind.QUERY)
	public <E extends Number> GenericCls<E> getGenericClsType() {
		return GenericsFactory.eINSTANCE.createGenericCls();
	}

}

Back to the top