Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6a7e31f10c28b00b6c1d521740714fb8b3536da9 (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.utils;

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

import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.osgi.util.NLS;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;

/**
 * 
 * This class provides utilities for Enum
 * 
 */
public class EnumerationUtil {


	private EnumerationUtil() {
		// to prevent instanciation
	}

	/**
	 * 
	 * @param enumeration
	 *        the uml enumeration
	 * @param enumerator
	 *        the enumerator to adapt to a uml EnumerationLiteral
	 * @return
	 */
	public static final EnumerationLiteral findEnumerationLiteral(final Enumeration enumeration, final Enumerator enumerator) {
		final EnumerationLiteral lit = enumeration.getOwnedLiteral(enumerator.getLiteral());
		if(lit != null) {
			return lit;
		}
		Activator.log.error(NLS.bind("The EnumerationLiteral for {0} has not been found", enumerator), new NullPointerException()); //$NON-NLS-1$
		return null;
	}


	/**
	 * 
	 * @param enumeration
	 *        the uml enumeration
	 * @param toAdapt
	 *        the list of the element to adapt to UML EnumerationLiteral
	 * @return
	 */
	public static final List<EnumerationLiteral> adaptToEnumerationLiteralList(final Enumeration enumeration, final Collection<?> toAdapt) {
		final List<EnumerationLiteral> returnedValue = new ArrayList<EnumerationLiteral>();
		for(Object object : toAdapt) {
			if(object instanceof EnumerationLiteral) {
				returnedValue.add((EnumerationLiteral)object);
			} else if(object instanceof Enumerator) {
				returnedValue.add(EnumerationUtil.findEnumerationLiteral(enumeration, (Enumerator)object));
			}
		}
		assert returnedValue.size() == toAdapt.size();
		return returnedValue;
	}

	/**
	 * 
	 * @param eenum
	 *        an eemf enumeration
	 * @param toConvert
	 *        a list of enumeration literal to convert
	 * @return
	 *         the list of the converted element (/!\ in case of fail, the returned list contains less elements than the initial list)
	 */
	public static final List<Enumerator> adaptToEnumeratorList(final EEnum eenum, final Collection<?> toConvert) {
		final List<Enumerator> convertedvalues = new ArrayList<Enumerator>();
		for(final Object object : toConvert) {
			if(object instanceof EnumerationLiteral) {
				final EEnumLiteral literal = eenum.getEEnumLiteral(((EnumerationLiteral)object).getName());
				if(literal != null) {
					convertedvalues.add(literal.getInstance());
				}
			}
		}
		return convertedvalues;

	}

	/**
	 * 
	 * @param eenum
	 *        an emf enumeration
	 * @param umlLiteral
	 *        a uml literal
	 * @return
	 *         the enumerator to use for this enumeration literal (/!\ can be null in case of fail)
	 */
	public static final Enumerator adaptToEnumerator(final EEnum eenum, final EnumerationLiteral umlLiteral) {
		final EEnumLiteral literal = eenum.getEEnumLiteral(umlLiteral.getName());
		if(literal != null) {
			return literal.getInstance();
		}
		return null;
	}

}

Back to the top