Initial Commit V2 (Used code from OBWB8m Data Parser)
This commit is contained in:
200
src/IDGenerator/IDPrefix.java
Normal file
200
src/IDGenerator/IDPrefix.java
Normal file
@@ -0,0 +1,200 @@
|
||||
package IDGenerator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import Converters.TreeVariable;
|
||||
import Converters.Variable;
|
||||
|
||||
|
||||
|
||||
|
||||
public class IDPrefix implements Comparable<IDPrefix> {
|
||||
private static final String gapString = "_"; //Do not change, it is in other locations
|
||||
private static final ArrayList<ArrayList<IDPrefix>> existing = new ArrayList<>();
|
||||
|
||||
private ArrayList<VariablePair> variablePairs = new ArrayList<>();
|
||||
private String prefix = null;
|
||||
private int internalCount = 0;
|
||||
|
||||
|
||||
public IDPrefix( String prefix ) throws PrefixException {
|
||||
if (existingContainsPrefix(prefix)) {
|
||||
throw new PrefixException("PrefixException: Prefix '" + prefix + "'was previously created.");
|
||||
} else {
|
||||
this.prefix = prefix;
|
||||
getExisting().add(this);
|
||||
}
|
||||
}
|
||||
|
||||
private static ArrayList<IDPrefix> getExisting( ) {
|
||||
if (existing.size() <= 0) {
|
||||
existing.add(new ArrayList<IDPrefix>());
|
||||
}
|
||||
return existing.getLast();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new array for the prefixes to be created under, saves the old one
|
||||
*/
|
||||
public static void startNewSection() {
|
||||
existing.add(new ArrayList<IDPrefix>());
|
||||
}
|
||||
|
||||
// public void remove(Variable var) {
|
||||
// boolean found = false;
|
||||
// for(int i = 0; i < variablePairs.size(); i++) {
|
||||
// if (found) {
|
||||
// variablePairs.get(i).value--;
|
||||
// variablePairs.get(i).var.setHTMLID( getIDString(variablePairs.get(i).value) );
|
||||
//
|
||||
// } else if (variablePairs.get(i).var == var) {
|
||||
// variablePairs.remove(i);
|
||||
// internalCount--;
|
||||
// i--;
|
||||
// var.setHTMLID(null);
|
||||
// found = true;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }//remove()
|
||||
|
||||
|
||||
// public static void removeAllScatch( Variable var ) {
|
||||
//
|
||||
// if (var.getPrefixID() != null) {
|
||||
// var.getPrefixID().remove(var);
|
||||
// }
|
||||
//
|
||||
// if (var instanceof TreeVariable) {
|
||||
// TreeVariable treeVar = (TreeVariable) var;
|
||||
//
|
||||
// if (treeVar.getArraySize() > 0) {
|
||||
// for(int i = 0; i < treeVar.getBranches().size(); i++) {
|
||||
// removeAllScatch(treeVar.getBranches().get(i));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// if (var.getArraySize() > 0) {
|
||||
//// for(int i = 0; i < var.getArraySize(); i++) {
|
||||
//// removeAllScatch(var.getArray().get(i));
|
||||
//// }
|
||||
// //Should just erase this prefix if it exits
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public static void resetAll( ) {
|
||||
for(int i = 0; i < getExisting().size(); i++) {
|
||||
resetAll(getExisting().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
private static void resetAll(IDPrefix prefix) {
|
||||
prefix.internalCount = 0;
|
||||
prefix.variablePairs.forEach((pair) -> {
|
||||
pair.var.setHTMLID(null);
|
||||
});
|
||||
prefix.variablePairs.removeAll(prefix.variablePairs);
|
||||
}
|
||||
|
||||
|
||||
private String getIDString( int count ) {
|
||||
return prefix + gapString + (count);
|
||||
}
|
||||
|
||||
public String getNextID(Variable var) {
|
||||
variablePairs.add(new VariablePair(var, ++internalCount)); //Increment count before saving
|
||||
return getIDString(internalCount);
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public void resetCounter() {
|
||||
this.internalCount = 0;
|
||||
this.variablePairs.removeAll(variablePairs); //remove all pairs
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ArrayList<VariablePair> getVariablePairs() {
|
||||
return (ArrayList<VariablePair>) variablePairs.clone();
|
||||
}
|
||||
|
||||
|
||||
private boolean existingContainsPrefix(String prefix) {
|
||||
int i = -1;
|
||||
while ((++i) < getExisting().size()) { //Start with index 0 to last index
|
||||
if (getExisting().get(i).prefix.equals(prefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}//existingContainsPrefix
|
||||
|
||||
|
||||
public class PrefixException extends Exception {
|
||||
private static final long serialVersionUID = 1974522L;
|
||||
public PrefixException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
public PrefixException(String errorMessage, Throwable err) {
|
||||
super(errorMessage, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class VariablePair {
|
||||
Variable var = null;
|
||||
int value = 0;
|
||||
|
||||
public VariablePair(Variable var, int value) {
|
||||
this.var = var;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}//class VariablePair
|
||||
|
||||
|
||||
/**
|
||||
* Compares two IDPrefix `prefix` variables lexicographically. Uses String.compareTo() method but also aaaa goes before aaa
|
||||
*/
|
||||
public int compareTo(IDPrefix other) {
|
||||
int retVal = 0;
|
||||
int maxSize = Math.min(this.prefix.length(), other.prefix.length());
|
||||
int i = -1;
|
||||
|
||||
while(retVal == 0 && (++i) < maxSize) {
|
||||
retVal = this.prefix.charAt(i) - other.prefix.charAt(i);
|
||||
}
|
||||
|
||||
if (retVal == 0) {
|
||||
return other.prefix.length() - this.prefix.length();
|
||||
} else {
|
||||
return retVal;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
public String toString( ) {
|
||||
return "IDPrefix: `" + prefix + "`";// "` with " + internalCount + " Items";
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ArrayList<IDPrefix> getAllPrefixes() {
|
||||
return (ArrayList<IDPrefix>) getExisting().clone();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user