package org.hibernate.metamodel.relational;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class AbstractTableSpecification implements TableSpecification {
private final static AtomicInteger tableCounter = new AtomicInteger( 0 );
private final int tableNumber;
private final LinkedHashMap<String, SimpleValue> values = new LinkedHashMap<String, SimpleValue>();
private final PrimaryKey primaryKey = new PrimaryKey( this );
private final List<ForeignKey> foreignKeys = new ArrayList<ForeignKey>();
public AbstractTableSpecification() {
this.tableNumber = tableCounter.getAndIncrement();
}
@Override
public int getTableNumber() {
return tableNumber;
}
@Override
public Iterable<SimpleValue> values() {
return values.values();
}
@Override
public Column locateOrCreateColumn(String name) {
if(values.containsKey( name )){
return (Column) values.get( name );
}
final Column column = new Column( this, values.size(), name );
values.put( name, column );
return column;
}
@Override
public DerivedValue locateOrCreateDerivedValue(String fragment) {
if(values.containsKey( fragment )){
return (DerivedValue) values.get( fragment );
}
final DerivedValue value = new DerivedValue( this, values.size(), fragment );
values.put( fragment, value );
return value;
}
@Override
public Tuple createTuple(String name) {
return new Tuple( this, name );
}
@Override
public Iterable<ForeignKey> getForeignKeys() {
return foreignKeys;
}
@Override
public ForeignKey createForeignKey(TableSpecification targetTable, String name) {
ForeignKey fk = new ForeignKey( this, targetTable, name );
foreignKeys.add( fk );
return fk;
}
@Override
public PrimaryKey getPrimaryKey() {
return primaryKey;
}
}