aboutsummaryrefslogtreecommitdiffstats
blob: 41ed2a695c08722ef77727c3cf27820f36412b00 (plain)
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
/*******************************************************************************
 * Copyright (c) 2004 INRIA.
 * 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:
 * 	   Frederic Jouault (INRIA) - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2m.atl.engine.vm.nativelib;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.m2m.atl.engine.vm.StackFrame;

/**
 * @author <a href="mailto:frederic.jouault@univ-nantes.fr">Frederic Jouault</a>
 */
public class ASMBag extends ASMCollection {

	public static ASMOclType myType = ASMOclParametrizedType.getASMOclParametrizedType("Bag", getOclAnyType(), ASMCollection.myType);

	public ASMBag() {
		super(myType);
		s = new ArrayList();
	}

	public ASMBag(ASMBag init) {
		super(myType);
		s = new ArrayList(init.s);
	}

	public ASMBag(Collection init) {
		super(myType);
		s = new ArrayList(init);
	}

	public static ASMBag including(StackFrame frame, ASMBag self, ASMOclAny o) {
		ASMBag ret = new ASMBag(self);

		ret.s.add(o);

		return ret;
	}
	
	public static ASMBag excluding(StackFrame frame, ASMBag self, ASMOclAny o) {
		ASMBag ret = new ASMBag(self);

		ret.s.removeAll(Arrays.asList(new ASMOclAny[] {o}));

		return ret;
	}

	public String toString() {
		StringBuffer ret = new StringBuffer();

		ret.append("Bag {");
		for(Iterator i = s.iterator() ; i.hasNext() ; ) {
			ret.append(i.next());
			if(i.hasNext()) ret.append(", ");
		}
		ret.append("}");

		return ret.toString();
	}

	public void add(ASMOclAny o) {
		s.add(o);
	}

	public Iterator iterator() {
		return s.iterator();
	}

	public Collection collection() {
		return s;
	}

	public boolean equals(Object o) {
		return (o instanceof ASMBag) && (((ASMBag)o).s.equals(s));
	}

	public int hashCode() {
		return s.hashCode();
	}
	
	// Native operations below
	
	public static ASMBag asBag(StackFrame frame, ASMBag self) {
		return self;
	}
	
	public static ASMBag flatten(StackFrame frame, ASMBag self) {
		List base = null;
		List ret = new ArrayList(self.collection());
		boolean containsCollection;
		do {
			containsCollection = false;
			base = ret;
			ret = new ArrayList();
			Iterator iterator = base.iterator();
			while (iterator.hasNext()) {
				ASMOclAny object = (ASMOclAny) iterator.next();
				if (object instanceof ASMCollection) {
					ASMCollection subCollection = (ASMCollection) object;
					ret.addAll(subCollection.collection());
					Iterator subIterator = subCollection.iterator();
					while (containsCollection == false && subIterator.hasNext()) {
						ASMOclAny subCollectionObject = (ASMOclAny) subIterator.next();
						if (subCollectionObject instanceof ASMCollection) {
							containsCollection = true;
						}
					}
				} else {
					ret.add(object);
				}
			}
		} while (containsCollection);
		return new ASMBag(ret);
	}
	
	private List s;
}