Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3d6e915282a76a6192275f4e504e0cf616e20df2 (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
/*****************************************************************************
 * Copyright (c) 2008 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:
 *  Remi SCHNEKENBURGER (CEA LIST) Remi.schnekenburger@cea.fr - Initial API and implementation
 *  
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.utils;


/**
 * Util class for {@link OpaqueExpression}
 */
public class OpaqueExpressionUtil {

	/**
	 * Returns the body for an OpaqueExpression for the given language
	 * 
	 * @param opaqueExpression
	 *        the opaque expression to edit.
	 * @param language
	 *        the language in which the body is written
	 * @return the body for the given language or the empty string if the language was not found
	 */
	public static String getBodyForLanguage(org.eclipse.uml2.uml.OpaqueExpression opaqueExpression, String language) {
		String body = "";
		if(language == null) {
			if(!opaqueExpression.getBodies().isEmpty()) {
				body = opaqueExpression.getBodies().get(0);
			}
		} else {
			// retrieve the index of the given language in the opaque Expression
			int index = opaqueExpression.getLanguages().indexOf(language);
			if(index != -1) {
				// language found. return the corresponding body in the bodies list.
				// List should be synchronized, ie having the same size, but be sure...
				if(index < opaqueExpression.getBodies().size()) {
					body = opaqueExpression.getBodies().get(index);
				}
			}
		}
		return body;
	}

	/**
	 * sets the body for an OpaqueExpression for the given language.
	 * <p>
	 * If the language was already defined, it replaces the corresponding body. If the language was not already defined, it adds it to the list of
	 * languages and adds the corresponding body.
	 * <p>
	 * A utility method, {@link OpaqueExpression#checkAndCorrectLists(org.eclipse.uml2.uml.OpaqueExpression)} is used to correct the language and body
	 * lists.
	 * 
	 * @param opaqueExpression
	 *        the opaque expression to edit.
	 * @param language
	 *        the language in which the body is written
	 * @param body
	 *        the body to save
	 */
	public static void setBodyForLanguage(org.eclipse.uml2.uml.OpaqueExpression opaqueExpression, String language, String body) {
		// checks both lists by size
		checkAndCorrectLists(opaqueExpression);
		// checks if language exists, if not, creates one
		if(!opaqueExpression.getLanguages().contains(language)) {
			opaqueExpression.getLanguages().add(language);
			opaqueExpression.getBodies().add(body);
		} else {
			// retrieve the index of the given language in the opaque Expression
			int index = opaqueExpression.getLanguages().indexOf(language);
			// sets the body at the given index in the list of bodies.
			opaqueExpression.getBodies().set(index, body);
		}
	}

	/**
	 * Checks body and languages list of an opaque expression.
	 * <p>
	 * It returns <code>true</code> if both lists have the same size. It returns <code>false</code> if one of the list was bigger than the other one.
	 * In this latter case, one of the list was corrected, ie enough elements where added in the list
	 * 
	 * @param opaqueExpression
	 *        the opaque expression to check
	 * @return <code>true</code> if both lists already had the same size, <code>false</code> in
	 *         other cases.
	 */
	public static boolean checkAndCorrectLists(org.eclipse.uml2.uml.OpaqueExpression opaqueExpression) {
		// both lists, languages and bodies, should have the same size
		final int bodySize = opaqueExpression.getBodies().size();
		final int languageSize = opaqueExpression.getLanguages().size();
		// check both size
		// if equals, lists are supposed synchronized, it is ok
		// if less body than languages, add bodies
		// if more body, add enough languages
		if(bodySize == languageSize) {
			return true;
		} else {
			final int difference = languageSize - bodySize;
			if(difference > 0) {
				// more languages strings than body strings, add enough bodies
				for(int i = 0; i < difference; i++) {
					opaqueExpression.getBodies().add("");
				}
			} else {
				// more body strings than language strings, add enough languages
				for(int i = 0; i < (-difference); i++) {
					opaqueExpression.getLanguages().add("");
				}
			}
			// lists had to be modified, return false...
			return false;
		}
	}
}

Back to the top