View Javadoc
1   /*****************************************************************************
2    * Copyright (c) 2013 CEA LIST.
3    *
4    *
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *  Ansgar Radermacher  ansgar.radermacher@cea.fr
12   *
13   *****************************************************************************/
14  package org.eclipse.papyrus.designer.transformation.base.utils;
15  
16  import org.eclipse.uml2.uml.Class;
17  import org.eclipse.uml2.uml.Interface;
18  import org.eclipse.uml2.uml.InterfaceRealization;
19  
20  public class RealizationUtils {
21  	
22  	public static final String REALIZATION_OF = "realization of "; //$NON-NLS-1$
23  
24  	/**
25  	 * Add an interface realization relationship from an implementation (Class) towards an interface
26  	 *
27  	 * @param implementation
28  	 *            A class
29  	 * @param intf
30  	 *            The interface it should realize
31  	 */
32  	public static void addRealization(final Class implementation, final Interface intf) {
33  		if (!hasRealization(implementation, intf)) {
34  			InterfaceRealization ir =
35  					implementation.createInterfaceRealization(calcRealizationName(intf), intf);
36  			ir.getClients().add(implementation);
37  			ir.getSuppliers().add(intf);
38  		}
39  	}
40  
41  	/**
42  	 * check, whether a realization relationship to a given interface exists
43  	 *
44  	 * @param implementation
45  	 *            A class
46  	 * @param intf
47  	 *            The interface it should realize
48  	 * @return true, if the class has an (interface) realization relationship to the passed interface
49  	 */
50  	public static boolean hasRealization(Class implementation, Interface intf) {
51  		return implementation.getInterfaceRealization(null, intf) != null;
52  	}
53  
54  	/**
55  	 * Calculate the name of an interface realization to an interface
56  	 *
57  	 * @param intf
58  	 *            an interface
59  	 * @return the calculated name for the realization relationship
60  	 */
61  	public static String calcRealizationName(Interface intf) {
62  		String name = intf.getName();
63  		if (name == null) {
64  			name = "undefined"; //$NON-NLS-1$
65  		}
66  		return REALIZATION_OF + name;
67  	}
68  
69  	public static void addUsage(Class portType, Interface usedInterface) {
70  		if (!portType.getUsedInterfaces().contains(usedInterface)) {
71  			portType.createUsage(usedInterface);
72  		}
73  	}
74  }