Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 592bcf8a81762c2d9e138a6cb5d19de7d01ab672 (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
/*****************************************************************************
 * 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;

import java.util.Iterator;

import org.eclipse.uml2.uml.OpaqueBehavior;

/**
 * This class encapsulates an <code>org.eclipse.uml2.uml.OpaqueBehavior</code><BR>
 */
public class OpaqueBehaviorUtil {

	/**
	 * Gets the value of the 'Body' attribute for the <code>OpaqueBehavior</code> at the given
	 * index.
	 * 
	 * @param index
	 * 
	 * @return the value of 'Body'at the index i
	 */
	// @unused
	public static String getBody(OpaqueBehavior behavior, int index) {
		return behavior.getBodies().get(index);
	}

	/**
	 * Gets the value of the 'Body' attribute for the <code>OpaqueBehavior</code> for a given
	 * language.
	 * 
	 * @param language
	 *        the language in which the body is written
	 * @return the value of 'Body' at the index i
	 */
	// @unused
	public static String getBody(OpaqueBehavior behavior, String language) {
		int index = 0;
		String tmp = "";

		index = getBodyIndex(behavior, language);
		if((index > -1) && (index < behavior.getBodies().size())) {
			tmp = behavior.getBodies().get(index);
		}

		return tmp;
	}

	/**
	 * Returns the index of the body for a given language.
	 * 
	 * @param language
	 *        the language of the body to find
	 * @return the index of the body in the list or -1 if not found
	 */
	public static int getBodyIndex(OpaqueBehavior behavior, String language) {
		int index = 0;
		boolean isFound = false;

		// test if the language exists
		Iterator<String> it = behavior.getLanguages().iterator();
		while(it.hasNext() && !isFound) {
			String lang = it.next();
			if(lang.equalsIgnoreCase(language)) {
				isFound = true;
			} else {
				index++;
			}
		}
		// returns -1 if not found
		if(!isFound) {
			index = -1;
		}
		return index;
	}

	/**
	 * Sets the value of the 'Body' attribute for a given language.
	 * 
	 * @param value
	 *        the new value of the 'Body'
	 * @param language
	 *        the langauge of the body to set
	 */
	// @unused
	public static void setBody(OpaqueBehavior behavior, String value, String language) {
		// get the index of the given language
		int index = getBodyIndex(behavior, language);
		if(index == -1) {
			// this language does not exists, adds it to the end of the list
			behavior.getLanguages().add(language);
			behavior.getBodies().add(value);
		} else {
			// the language was found, change the value of the language
			behavior.getBodies().set(index, value);
		}
	}

}

Back to the top