Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CopyHelper |
|
| 7.0;7 |
1 | /* $Id: CopyHelper.java 17765 2010-01-11 21:20:14Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2003-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.model.mdr; | |
40 | ||
41 | import java.util.Collection; | |
42 | import java.util.Iterator; | |
43 | ||
44 | import org.omg.uml.foundation.core.DataType; | |
45 | import org.omg.uml.foundation.core.Feature; | |
46 | import org.omg.uml.foundation.core.Interface; | |
47 | import org.omg.uml.foundation.core.ModelElement; | |
48 | import org.omg.uml.foundation.core.Namespace; | |
49 | import org.omg.uml.foundation.core.Stereotype; | |
50 | import org.omg.uml.foundation.core.TagDefinition; | |
51 | import org.omg.uml.foundation.core.UmlClass; | |
52 | import org.omg.uml.modelmanagement.UmlPackage; | |
53 | ||
54 | /** | |
55 | * Utility class to facilitate copying model elements. | |
56 | * | |
57 | * @author Michael Stockman | |
58 | * @since 0.13.2 | |
59 | */ | |
60 | final class CopyHelper implements org.argouml.model.CopyHelper { | |
61 | ||
62 | /** | |
63 | * The model implementation. | |
64 | */ | |
65 | private MDRModelImplementation modelImpl; | |
66 | ||
67 | /** | |
68 | * Constructor to forbid creation of this object. | |
69 | * | |
70 | * @param implementation | |
71 | * To get other helpers and factories. | |
72 | */ | |
73 | 900 | CopyHelper(MDRModelImplementation implementation) { |
74 | 900 | modelImpl = implementation; |
75 | 900 | } |
76 | ||
77 | /** | |
78 | * @see org.argouml.model.CopyHelper#copy(java.lang.Object, java.lang.Object) | |
79 | * | |
80 | * Make a copy of element in the given namespace.<p> | |
81 | * | |
82 | * This function is a dispatcher that calls the | |
83 | * copyElement(Element,Namespace) function from XXXFactory.<p> | |
84 | * | |
85 | * This function may fail and return null for any of the following reasons: | |
86 | * <ol> | |
87 | * <li>No copy function is known for element's type. | |
88 | * <li>The copy function fails or throws. | |
89 | * </ol> | |
90 | * | |
91 | * @param anelement | |
92 | * is the element to copy. | |
93 | * @param ans | |
94 | * the namespace | |
95 | * @return a copy of element, or null. | |
96 | * | |
97 | * @throws IllegalArgumentException | |
98 | * if element is null. | |
99 | */ | |
100 | public Object copy(Object anelement, Object ans) { | |
101 | // Don't explicitly check if element is null | |
102 | 0 | ModelElement element = (ModelElement) anelement; |
103 | 0 | Namespace ns = (Namespace) ans; |
104 | ||
105 | 0 | if (element instanceof UmlPackage) { |
106 | 0 | return modelImpl.getModelManagementFactory().copyPackage(element, |
107 | ns); | |
108 | } | |
109 | 0 | if (element instanceof UmlClass) { |
110 | 0 | return modelImpl.getCoreFactory().copyClass(element, ns); |
111 | } | |
112 | 0 | if (element instanceof DataType) { |
113 | 0 | return modelImpl.getCoreFactory().copyDataType(element, ns); |
114 | } | |
115 | 0 | if (element instanceof Interface) { |
116 | 0 | return modelImpl.getCoreFactory().copyInterface(element, ns); |
117 | } | |
118 | 0 | if (element instanceof Feature) { |
119 | 0 | return modelImpl.getCoreFactory().copyFeature(element, ns); |
120 | } | |
121 | 0 | if (element instanceof Stereotype) { |
122 | 0 | return modelImpl.getExtensionMechanismsFactory().copyStereotype( |
123 | element, ns); | |
124 | } | |
125 | 0 | if (element instanceof TagDefinition) { |
126 | 0 | return modelImpl.getExtensionMechanismsFactory().copyTagDefinition( |
127 | element, ns); | |
128 | } | |
129 | 0 | throw new IllegalArgumentException("anelement:" + anelement + ", ans: " |
130 | + ans); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Copy an element and its children into a namespace. | |
135 | * | |
136 | * @param anelement element to be copied | |
137 | * @param ans namespace to copy into | |
138 | * @return Object copy of given element and its children | |
139 | */ | |
140 | Object fullCopy(Object anelement, Object ans) { | |
141 | 0 | ModelElement copy = (ModelElement) copy(anelement, ans); |
142 | 0 | if (anelement instanceof Namespace) { |
143 | 0 | Collection children = ((Namespace) anelement).getOwnedElement(); |
144 | 0 | if (!children.isEmpty()) { |
145 | 0 | Iterator it = children.iterator(); |
146 | 0 | while (it.hasNext()) { |
147 | 0 | Object childToCopy = it.next(); |
148 | 0 | fullCopy(childToCopy, copy); |
149 | 0 | } |
150 | } | |
151 | } | |
152 | 0 | return copy; |
153 | } | |
154 | ||
155 | } |