for(int lvl = 0;lvl<stack.getSize();lvl++)
{
generator.addForbiddenString(stack.getRepresentation(lvl).getTableName());
}
// find the existing stack that most closely match the new stack
ObjectStack closestStack = null;
int maxHeight = -1;
for (ObjectStack existing : namedStacks)
{
int maxLevel = Math.min(stack.getSize(), existing.getSize());
for (int x = 0; x < maxLevel; x++)
{
ObjectRepresentation o1 = stack.getRepresentation(x);
ObjectRepresentation o2 = existing.getRepresentation(x);
if (!o1.getRepresentedClass().equals(o2.getRepresentedClass()))
{
break;
}
else
{
if (x > maxHeight)
{
maxHeight = x;
closestStack = existing;
}
}
}
}
// copy names from existing to new stack
for (int x = 0; x <= maxHeight; x++)
{
stack.getRepresentation(x).setAsName(
closestStack.getRepresentation(x).getAsName());
}
// fill in remaining names from generator
for (int x = maxHeight + 1; x < stack.getSize(); x++)
{
stack.getRepresentation(x).setAsName(generator.next());