org.umlg.java.metamodel.OJPathName Maven / Gradle / Ivy
package org.umlg.java.metamodel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import org.umlg.java.metamodel.generated.OJPathNameGEN;
import org.umlg.java.metamodel.utilities.JavaUtil;
public class OJPathName extends OJPathNameGEN implements Comparable {
private List generics = new ArrayList();
// static public OJPathName VOID = new OJPathName("java.lang.void");
/******************************************************
* The constructor for this classifier.
*******************************************************/
public OJPathName() {
super();
}
public OJPathName(String name) {
super();
StringTokenizer st = new StringTokenizer(name, ".");
while (st.hasMoreTokens()) {
this.addToNames(st.nextToken());
}
}
/******************************************************
* The following operations are the implementations of the operations
* defined for this classifier.
*******************************************************/
public String getFirst() {
String result = "";
if (getNames().size() > 0)
result = (String) getNames().get(0);
return result;
}
public String getLast() {
String result = "";
if (getNames().size() > 0) {
result = (String) getNames().get(getNames().size() - 1);
}
StringBuilder pathInfo = new StringBuilder();
if (!this.generics.isEmpty()) {
boolean first = true;
pathInfo.append("<");
for (OJPathName pathName : this.generics) {
if (first) {
first = false;
} else {
pathInfo.append(",");
}
pathInfo.append(pathName.getLast());
}
pathInfo.append(">");
result += pathInfo.toString();
}
return result;
}
public OJPathName replaceGeneric(int index, String ojPathName) {
this.generics.remove(index);
this.generics.add(index, new OJPathName(ojPathName));
return this;
}
public OJPathName replaceGeneric(int index, OJPathName ojPathName) {
this.generics.remove(index);
this.generics.add(index, ojPathName);
return this;
}
public OJPathName getHead() {
OJPathName result = new OJPathName();
if (getNames().size() > 0)
result.setNames(getNames().subList(0, getNames().size() - 1));
return result;
}
public OJPathName getTail() {
OJPathName result = new OJPathName();
if (getNames().size() > 0)
result.setNames(getNames().subList(1, getNames().size()));
return result;
}
public String getTypeName() {
return getLast();
}
public String getCollectionTypeName() {
if (getElementTypes().isEmpty()) {
return getLast();
} else {
return getLast() + elementTypesToJavaString();
}
}
/******************************************************
* End of implemented operations.
*******************************************************/
public boolean equals(Object other) {
return other instanceof OJPathName && super.equals((OJPathName) other);
}
public int hashCode() {
return this.getLast().hashCode();
}
public String toJavaString() {
StringBuilder pathInfo = new StringBuilder();
boolean first = true;
Iterator it = getNames().iterator();
while (it.hasNext()) {
if (first) {
first = false;
} else {
pathInfo.append(".");
}
String elem = (String) it.next();
pathInfo.append(elem);
}
if (!this.generics.isEmpty()) {
pathInfo.append("<");
first = true;
for (OJPathName pathName : this.generics) {
if (first) {
first = false;
} else {
pathInfo.append(",");
}
pathInfo.append(pathName.toJavaString());
}
pathInfo.append(">");
}
return pathInfo.toString();
}
private String elementTypesToJavaString() {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator it = getElementTypes().iterator();
while (it.hasNext()) {
OJPathName elemType = (OJPathName) it.next();
if (!elemType.getLast().equals("void")) {
if (first) {
first = false;
result.append("<");
} else {
result.append(", ");
}
result.append(elemType.getCollectionTypeName());
}
}
if (result.length() != 0)
result.append(">");
return result.toString();
}
//
public String toString() {
return JavaUtil.collectionToString(getNames(), ".");
}
/**
* @return
*/
public OJPathName getCopy() {
OJPathName result = new OJPathName();
result.setNames(new ArrayList(this.getNames()));
result.generics = new ArrayList(this.getGenerics());
return result;
}
public OJPathName getDeepCopy() {
OJPathName result = new OJPathName();
result.setNames(new ArrayList(this.getNames()));
List elementTypes = getElementTypes();
for (OJPathName elementType : elementTypes) {
OJPathName elementTypeCopy = elementType.getDeepCopy();
if (elementType.getLast().contains("extends")) {
System.out.println();
}
result.addToElementTypes(elementTypeCopy);
}
return result;
}
/**
* @return
*/
public boolean isSingleName() {
return getNames().size() == 1;
}
@SuppressWarnings("unchecked")
public void replaceTail(String newtail) {
getNames().set(getNames().size() - 1, newtail);
}
@SuppressWarnings("unchecked")
public void insertBeforeTail(String name) {
getNames().add(getNames().size() - 1, name);
}
public OJPathName append(String str) {
this.addToNames(str);
return this;
}
// TODO This is done dum, redo
public void renameAll(Set renamePathNames, String newName) {
if (getLast().charAt(0) == '?') {
replaceTail(replaceAll(getLast(), renamePathNames, newName));
} else if (renamePathNames.contains(this)) {
replaceTail(getLast() + newName);
}
for (OJPathName elementType : getElementTypes()) {
elementType.renameAll(renamePathNames, newName);
}
}
@Override
public int compareTo(OJPathName o) {
List myNames = getNames();
List otherNames = o.getNames();
for (int i = 0; i < myNames.size(); i++) {
if (otherNames.size() > i) {
int compareTo = otherNames.get(i).compareTo(myNames.get(i));
if (compareTo != 0) {
return compareTo;
}
} else {
return 1;// Longest is greater
}
}
if (myNames.size() < otherNames.size()) {
return -1;// Shortest is less
}
return 0;
}
public void clearGenerics() {
this.generics.clear();
}
public OJPathName addToGenerics(OJPathName path) {
this.generics.add(path);
return this;
}
public List getGenerics() {
List copy = new ArrayList();
for (OJPathName o : this.generics) {
copy.add(o.getCopy());
}
return copy;
}
public OJPathName renameLast(String name) {
removeFromNames(getLast());
append(name);
return this;
}
public OJPathName addToGenerics(String string) {
return addToGenerics(new OJPathName(string));
}
public OJPathName appendToTail(String s) {
return renameLast(getLast() + s);
}
public void appendToGeneric(String string) {
for (OJPathName g : this.generics) {
g.appendToTail(string);
}
}
}