001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * OperationElement.java
044: *
045: * Created on December 6, 2005, 4:42 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.test.umllib.values.operatons;
052:
053: import java.util.LinkedList;
054: import java.util.List;
055: import java.util.StringTokenizer;
056: import org.netbeans.test.umllib.values.Argument;
057: import org.netbeans.test.umllib.values.DefaultType;
058: import org.netbeans.test.umllib.values.DefaultVisibility;
059: import org.netbeans.test.umllib.values.ElementType;
060: import org.netbeans.test.umllib.values.Type;
061: import org.netbeans.test.umllib.values.Visibility;
062: import org.netbeans.test.umllib.values.Arg;
063:
064: /**
065: *
066: * @author Alexandr Scherbatiy
067: */
068: public class OperationElement {
069:
070: /** Creates a new instance of OperationElement */
071:
072: private Visibility visibility;
073: private Type type;
074: private String name;
075:
076: private List<Argument> argList;
077:
078: public OperationElement() {
079: this ("Unnamed");
080: }
081:
082: public OperationElement(String name, List<Argument> argList) {
083: this (name, DefaultType.VOID, DefaultVisibility.PUBLIC, argList);
084: }
085:
086: public OperationElement(String name) {
087: this (name, DefaultType.VOID);
088: }
089:
090: public OperationElement(String name, Type type) {
091: this (name, type, DefaultVisibility.PUBLIC);
092: }
093:
094: public OperationElement(String name, Type type,
095: Visibility visibility) {
096: this (name, type, visibility, new LinkedList<Argument>());
097: }
098:
099: public OperationElement(String name, Type type,
100: Visibility visibility, List<Argument> argList) {
101: this .visibility = visibility;
102: this .type = type;
103: this .name = name;
104: this .argList = (argList == null) ? new LinkedList<Argument>()
105: : argList;
106: }
107:
108: public Visibility getVisibility() {
109: return visibility;
110: }
111:
112: public Type getType() {
113: return type;
114: }
115:
116: public String getName() {
117: return name;
118: }
119:
120: public List<Argument> getArguments() {
121: return argList;
122: }
123:
124: public String getText() {
125: String text = "";
126: text += visibility.getValue();
127: text += " " + type.getValue();
128: text += " " + name;
129:
130: text += "(";
131:
132: int lastIndex = argList.size() - 1;
133:
134: for (int i = 0; i < lastIndex; i++) {
135: text += argList.get(i) + ", ";
136: }
137:
138: if (lastIndex != -1) {
139: text += argList.get(lastIndex) + " ";
140: }
141:
142: text += ")";
143:
144: return text;
145: }
146:
147: public String toString() {
148:
149: return "Operation: \"" + getText() + "\"";
150: }
151:
152: public boolean isEqual(OperationElement operation) {
153:
154: boolean result = true;
155:
156: result = result
157: && visibility.isEqual(operation.getVisibility());
158: result = result && type.isEqual(operation.getType());
159: result = result && name.equals(operation.getName());
160:
161: List<Argument> otherArgList = operation.getArguments();
162: int size1 = argList.size();
163:
164: int size2 = otherArgList.size();
165:
166: if (size1 == size2) {
167: for (int i = 0; i < size1; i++) {
168: result = result
169: && argList.get(i).isEqual(otherArgList.get(i));
170: }
171:
172: } else {
173: return false;
174: }
175:
176: return result;
177: }
178:
179: // ============= Parsed Strings ===========================
180:
181: // "public MyClass( )"
182: // "public void myOperation( )"
183:
184: public static OperationElement parseOperationElement(String parse) {
185:
186: System.out.println("Parse Operation Element = \"" + parse
187: + "\"");
188:
189: String withoutBlank = parse.replaceAll("\\s+", " ");
190:
191: // parse visibility, type and name
192:
193: String description = withoutBlank.substring(0, withoutBlank
194: .indexOf('('));
195:
196: String[] split = description.split("\\s");
197:
198: int len = split.length;
199:
200: String name = null;
201: Type type = null;
202: Visibility visibility = null;
203:
204: if (len == 2 || len == 3) {
205: visibility = DefaultVisibility.getVisibility(split[0]
206: .toUpperCase());
207: if (len == 2) {
208: type = DefaultType.NONE;
209: name = split[1];
210: }
211:
212: if (len == 3) {
213: type = new ElementType(split[1]);
214: name = split[2];
215: }
216:
217: }
218:
219: // parse Argument list
220:
221: LinkedList<Argument> argList = new LinkedList<Argument>();
222:
223: int firstIndex = withoutBlank.indexOf('(');
224: int lastIndex = withoutBlank.indexOf(')');
225:
226: String argsString = withoutBlank.substring(firstIndex + 1,
227: lastIndex).trim();
228:
229: //System.out.println(" Args String = \"" + argsString + "\"");
230: StringTokenizer argToken = new StringTokenizer(argsString, ",");
231:
232: while (argToken.hasMoreTokens()) {
233: String str = argToken.nextToken();
234: String[] strSplit = str.split(" ");
235: Type argType = new ElementType(strSplit[0]);
236: String argName = strSplit[1];
237:
238: argList.addLast(new Arg(argType, argName));
239: }
240:
241: return new OperationElement(name, type, visibility, argList);
242: }
243: }
|