blob: 600e994135a89a3974c78e01a97d955e8e7cf383 [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2020 CEA LIST
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ansgar.radermacher@cea.fr - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.robotics.core.utils;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.NamedElement;
/**
* Simple mechanism to provide a unique named (within the owning namespace) based on a prefix
* and a 4 digit number
*/
public class NamingUtil {
public static void setName(NamedElement ne, String prefix) {
List<String> existingNames = new ArrayList<String>();
for (NamedElement existingNE : ne.getNamespace().getMembers()) {
existingNames.add(existingNE.getName());
}
for(int index = 1; index < 1000; index++) {
String name = calcName(prefix, index);
if (!existingNames.contains(name)) {
ne.setName(name);
return;
}
}
}
public static void setName(Comment comment, String prefix) {
List<String> existingNames = new ArrayList<String>();
for (Comment existingComment : comment.getOwner().getOwnedComments()) {
existingNames.add(existingComment.getBody());
}
for(int index = 1; index < 1000; index++) {
String name = calcName(prefix, index);
if (!existingNames.contains(name)) {
comment.setBody(name);
return;
}
}
}
public static String calcName(String prefix, int index) {
return String.format("%s%04d", prefix, index); //$NON-NLS-1$
}
}