Skip to content

Commit

Permalink
First pass at converting to Java 5
Browse files Browse the repository at this point in the history
  • Loading branch information
johncurrier committed Jul 29, 2008
1 parent 84fb7e3 commit 0adb9a1
Show file tree
Hide file tree
Showing 31 changed files with 455 additions and 561 deletions.
44 changes: 21 additions & 23 deletions src/net/sourceforge/schemaspy/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
public class Config
{
private static Config instance;
private List options;
private Map dbSpecificOptions;
private Map originalDbSpecificOptions;
private List<String> options;
private Map<String, String> dbSpecificOptions;
private Map<String, String> originalDbSpecificOptions;
private boolean helpRequired;
private boolean dbHelpRequired;
private File outputDir;
Expand Down Expand Up @@ -66,7 +66,7 @@ public Config()
{
if (instance == null)
setInstance(this);
options = new ArrayList();
options = new ArrayList<String>();
}

/**
Expand Down Expand Up @@ -733,7 +733,7 @@ protected String getDbPropertiesLoadedFrom() throws IOException {
return dbPropertiesLoadedFrom;
}

public List getRemainingParameters()
public List<String> getRemainingParameters()
{
try {
populate();
Expand All @@ -748,14 +748,14 @@ public List getRemainingParameters()
*
* @param dbSpecificOptions
*/
public void setDbSpecificOptions(Map dbSpecificOptions) {
public void setDbSpecificOptions(Map<String, String> dbSpecificOptions) {
this.dbSpecificOptions = dbSpecificOptions;
this.originalDbSpecificOptions = new HashMap(dbSpecificOptions);
this.originalDbSpecificOptions = new HashMap<String, String>(dbSpecificOptions);
}

public Map getDbSpecificOptions() {
public Map<String, String> getDbSpecificOptions() {
if (dbSpecificOptions == null)
dbSpecificOptions = new HashMap();
dbSpecificOptions = new HashMap<String, String>();
return dbSpecificOptions;
}

Expand Down Expand Up @@ -830,11 +830,10 @@ public boolean isDbTypeSpecific() {
* List
* @return List
*/
protected List fixupArgs(List args) {
List expandedArgs = new ArrayList();
protected List<String> fixupArgs(List<String> args) {
List<String> expandedArgs = new ArrayList<String>();

for (Iterator iter = args.iterator(); iter.hasNext(); ) {
String arg = iter.next().toString();
for (String arg : args) {
int indexOfEquals = arg.indexOf('=');
if (indexOfEquals != -1 && indexOfEquals -1 != arg.indexOf("\\=")) {
expandedArgs.add(arg.substring(0, indexOfEquals));
Expand All @@ -847,10 +846,9 @@ protected List fixupArgs(List args) {
// some OSes/JVMs do filename expansion with runtime.exec() and some don't,
// so MultipleSchemaAnalyzer has to surround params with double quotes...
// strip them here for the OSes/JVMs that don't do anything with the params
List unquotedArgs = new ArrayList();
List<String> unquotedArgs = new ArrayList<String>();

for (Iterator iter = expandedArgs.iterator(); iter.hasNext(); ) {
String arg = iter.next().toString();
for (String arg : expandedArgs) {
if (arg.startsWith("\"") && arg.endsWith("\"")) // ".*" becomes .*
arg = arg.substring(1, arg.length() - 1);
unquotedArgs.add(arg);
Expand All @@ -876,15 +874,15 @@ private void populate() throws IllegalArgumentException, IllegalAccessException,
for (int i = 0; i < props.length; ++i) {
Method readMethod = props[i].getReadMethod();
if (readMethod != null)
readMethod.invoke(this, null);
readMethod.invoke(this, (Object[])null);
}

populating = false;
}
}

public static Set getBuiltInDatabaseTypes(String loadedFromJar) {
Set databaseTypes = new TreeSet();
public static Set<String> getBuiltInDatabaseTypes(String loadedFromJar) {
Set<String> databaseTypes = new TreeSet<String>();
JarInputStream jar = null;

try {
Expand Down Expand Up @@ -970,7 +968,7 @@ public String getParam(String paramName) {
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
if (prop.getName().equalsIgnoreCase(paramName)) {
Object result = prop.getReadMethod().invoke(this, null);
Object result = prop.getReadMethod().invoke(this, (Object[])null);
return result == null ? null : result.toString();
}
}
Expand All @@ -988,8 +986,8 @@ public String getParam(String paramName) {
* @return
* @throws IOException
*/
public List asList() throws IOException {
List list = new ArrayList();
public List<String> asList() throws IOException {
List<String> list = new ArrayList<String>();

if (originalDbSpecificOptions != null) {
Iterator iter = originalDbSpecificOptions.keySet().iterator();
Expand Down Expand Up @@ -1088,7 +1086,7 @@ public List asList() throws IOException {
list.add("-maxdet");
list.add(String.valueOf(getMaxDetailedTables()));
list.add("-o");
list.add(getOutputDir());
list.add(getOutputDir().toString());

return list;
}
Expand Down
121 changes: 51 additions & 70 deletions src/net/sourceforge/schemaspy/DbAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import net.sourceforge.schemaspy.model.*;

public class DbAnalyzer {
public static List getImpliedConstraints(Collection tables) throws SQLException {
List columnsWithoutParents = new ArrayList();
Map allPrimaries = new TreeMap(new Comparator() {
public int compare(Object object1, Object object2) {
TableColumn column1 = (TableColumn)object1;
TableColumn column2 = (TableColumn)object2;
public static List<ImpliedForeignKeyConstraint> getImpliedConstraints(Collection<Table> tables) throws SQLException {
List<TableColumn> columnsWithoutParents = new ArrayList<TableColumn>();
Map<TableColumn, Table> allPrimaries = new TreeMap<TableColumn, Table>(new Comparator<TableColumn>() {
public int compare(TableColumn column1, TableColumn column2) {
int rc = column1.getName().compareTo(column2.getName());
if (rc == 0)
rc = column1.getType().compareTo(column2.getType());
Expand All @@ -24,12 +22,11 @@ public int compare(Object object1, Object object2) {
int duplicatePrimaries = 0;

// gather all the primary key columns and columns without parents
for (Iterator iter = tables.iterator(); iter.hasNext(); ) {
Table table = (Table)iter.next();
List tablePrimaries = table.getPrimaryColumns();
for (Table table : tables) {
List<TableColumn> tablePrimaries = table.getPrimaryColumns();
if (tablePrimaries.size() == 1) { // can't match up multiples...yet...
for (Iterator primariesIter = tablePrimaries.iterator(); primariesIter.hasNext(); ) {
if (allPrimaries.put(primariesIter.next(), table) != null)
for (TableColumn primary : tablePrimaries) {
if (allPrimaries.put(primary, table) != null)
++duplicatePrimaries;
}
}
Expand All @@ -45,14 +42,14 @@ public int compare(Object object1, Object object2) {
// it's most likey a database where primary key names aren't unique
// (e.g. they all have a primary key named 'ID')
if (duplicatePrimaries > allPrimaries.size()) // bizarre logic, but it does approximately what we need
return new ArrayList();
return new ArrayList<ImpliedForeignKeyConstraint>();

sortColumnsByTable(columnsWithoutParents);

List impliedConstraints = new ArrayList();
List<ImpliedForeignKeyConstraint> impliedConstraints = new ArrayList<ImpliedForeignKeyConstraint>();
for (Iterator iter = columnsWithoutParents.iterator(); iter.hasNext(); ) {
TableColumn childColumn = (TableColumn)iter.next();
Table primaryTable = (Table)allPrimaries.get(childColumn);
Table primaryTable = allPrimaries.get(childColumn);
if (primaryTable != null && primaryTable != childColumn.getTable()) {
TableColumn parentColumn = primaryTable.getColumn(childColumn.getName());
// make sure the potential child->parent relationships isn't already a
Expand All @@ -75,23 +72,20 @@ public int compare(Object object1, Object object2) {
* @param tables Collection
* @return List
*/
public static List getForeignKeyConstraints(Collection tables) {
List constraints = new ArrayList();
Iterator iter = tables.iterator();
while (iter.hasNext()) {
Table table = (Table)iter.next();
public static List<ForeignKeyConstraint> getForeignKeyConstraints(Collection<Table> tables) {
List<ForeignKeyConstraint> constraints = new ArrayList<ForeignKeyConstraint>();

for (Table table : tables) {
constraints.addAll(table.getForeignKeys());
}

return constraints;
}

public static List getOrphans(Collection tables) {
List orphans = new ArrayList();
public static List<Table> getOrphans(Collection<Table> tables) {
List<Table> orphans = new ArrayList<Table>();

Iterator iter = tables.iterator();
while (iter.hasNext()) {
Table table = (Table)iter.next();
for (Table table : tables) {
if (table.isOrphan(false)) {
orphans.add(table);
}
Expand All @@ -104,13 +98,11 @@ public static List getOrphans(Collection tables) {
* Return a list of <code>TableColumn</code>s that are both nullable
* and have an index that specifies that they must be unique (a rather strange combo).
*/
public static List getMustBeUniqueNullableColumns(Collection tables) {
List uniqueNullables = new ArrayList();
public static List<TableColumn> getMustBeUniqueNullableColumns(Collection<Table> tables) {
List<TableColumn> uniqueNullables = new ArrayList<TableColumn>();

for (Iterator tablesIter = tables.iterator(); tablesIter.hasNext(); ) {
Table table = (Table)tablesIter.next();
for (Iterator indexesIter = table.getIndexes().iterator(); indexesIter.hasNext(); ) {
TableIndex index = (TableIndex)indexesIter.next();
for (Table table : tables) {
for (TableIndex index : table.getIndexes()) {
if (index.isUniqueNullable()) {
uniqueNullables.addAll(index.getColumns());
}
Expand All @@ -123,31 +115,26 @@ public static List getMustBeUniqueNullableColumns(Collection tables) {
/**
* Return a list of <code>Table</code>s that have neither an index nor a primary key.
*/
public static List getTablesWithoutIndexes(Collection tables) {
List withoutIndexes = new ArrayList();
public static List<Table> getTablesWithoutIndexes(Collection<Table> tables) {
List<Table> withoutIndexes = new ArrayList<Table>();

for (Iterator tablesIter = tables.iterator(); tablesIter.hasNext(); ) {
Table table = (Table)tablesIter.next();
for (Table table : tables) {
if (!table.isView() && table.getIndexes().size() == 0)
withoutIndexes.add(table);
}

return sortTablesByName(withoutIndexes);
}

public static List getTablesWithIncrementingColumnNames(Collection tables) {
List denormalizedTables = new ArrayList();
public static List<Table> getTablesWithIncrementingColumnNames(Collection<Table> tables) {
List<Table> denormalizedTables = new ArrayList<Table>();

Iterator tableIter = tables.iterator();
while (tableIter.hasNext()) {
Table table = (Table)tableIter.next();
Map columnPrefixes = new HashMap();
for (Table table : tables) {
Map<String, Long> columnPrefixes = new HashMap<String, Long>();

Iterator columnIter = table.getColumns().iterator();
while (columnIter.hasNext()) {
for (TableColumn column : table.getColumns()) {
// search for columns that start with the same prefix
// and end in an incrementing number
TableColumn column = (TableColumn)columnIter.next();

String columnName = column.getName();
String numbers = null;
Expand All @@ -170,7 +157,7 @@ public static List getTablesWithIncrementingColumnNames(Collection tables) {
// that had a numeric suffix +/- 1.
String prefix = columnName.substring(0, columnName.length() - numbers.length());
long numeric = Long.parseLong(numbers);
Long existing = (Long)columnPrefixes.get(prefix);
Long existing = columnPrefixes.get(prefix);
if (existing != null && Math.abs(existing.longValue() - numeric) == 1) {
// found one so add it to our list and stop evaluating this table
denormalizedTables.add(table);
Expand All @@ -183,34 +170,30 @@ public static List getTablesWithIncrementingColumnNames(Collection tables) {
return sortTablesByName(denormalizedTables);
}

public static List getTablesWithOneColumn(Collection tables) {
List singleColumnTables = new ArrayList();
public static List<Table> getTablesWithOneColumn(Collection<Table> tables) {
List<Table> singleColumnTables = new ArrayList<Table>();

Iterator iter = tables.iterator();
while (iter.hasNext()) {
Table table = (Table)iter.next();
for (Table table : tables) {
if (table.getColumns().size() == 1)
singleColumnTables.add(table);
}

return sortTablesByName(singleColumnTables);
}

public static List sortTablesByName(List tables) {
Collections.sort(tables, new Comparator() {
public int compare(Object object1, Object object2) {
return ((Table)object1).getName().compareTo(((Table)object2).getName());
public static List<Table> sortTablesByName(List<Table> tables) {
Collections.sort(tables, new Comparator<Table>() {
public int compare(Table table1, Table table2) {
return table1.getName().compareTo(table2.getName());
}
});

return tables;
}

public static List sortColumnsByTable(List columns) {
Collections.sort(columns, new Comparator() {
public int compare(Object object1, Object object2) {
TableColumn column1 = (TableColumn)object1;
TableColumn column2 = (TableColumn)object2;
public static List<TableColumn> sortColumnsByTable(List<TableColumn> columns) {
Collections.sort(columns, new Comparator<TableColumn>() {
public int compare(TableColumn column1, TableColumn column2) {
int rc = column1.getTable().getName().compareTo(column2.getTable().getName());
if (rc == 0)
rc = column1.getName().compareTo(column2.getName());
Expand All @@ -228,13 +211,11 @@ public int compare(Object object1, Object object2) {
* @param tables Collection
* @return List
*/
public static List getDefaultNullStringColumns(Collection tables) {
List defaultNullStringColumns = new ArrayList();
public static List<TableColumn> getDefaultNullStringColumns(Collection<Table> tables) {
List<TableColumn> defaultNullStringColumns = new ArrayList<TableColumn>();

for (Iterator tablesIter = tables.iterator(); tablesIter.hasNext(); ) {
Table table = (Table)tablesIter.next();
for (Iterator columnsIter = table.getColumns().iterator(); columnsIter.hasNext(); ) {
TableColumn column = (TableColumn)columnsIter.next();
for (Table table : tables) {
for (TableColumn column : table.getColumns()) {
Object defaultValue = column.getDefaultValue();
if (defaultValue != null && defaultValue instanceof String) {
String defaultString = defaultValue.toString();
Expand All @@ -253,8 +234,8 @@ public static List getDefaultNullStringColumns(Collection tables) {
*
* @param meta DatabaseMetaData
*/
public static List getSchemas(DatabaseMetaData meta) throws SQLException {
List schemas = new ArrayList();
public static List<String> getSchemas(DatabaseMetaData meta) throws SQLException {
List<String> schemas = new ArrayList<String>();

ResultSet rs = meta.getSchemas();
while (rs.next()) {
Expand All @@ -280,11 +261,11 @@ public static List getPopulatedSchemas(DatabaseMetaData meta) throws SQLExceptio
*
* @param meta DatabaseMetaData
*/
public static List getPopulatedSchemas(DatabaseMetaData meta, String schemaSpec) throws SQLException {
Set schemas = new TreeSet(); // alpha sorted
public static List<String> getPopulatedSchemas(DatabaseMetaData meta, String schemaSpec) throws SQLException {
Set<String> schemas = new TreeSet<String>(); // alpha sorted
Pattern schemaRegex = Pattern.compile(schemaSpec);

Iterator iter = getSchemas(meta).iterator();
Iterator<String> iter = getSchemas(meta).iterator();
while (iter.hasNext()) {
String schema = iter.next().toString();
if (schemaRegex.matcher(schema).matches()) {
Expand All @@ -301,7 +282,7 @@ public static List getPopulatedSchemas(DatabaseMetaData meta, String schemaSpec)
}
}

return new ArrayList(schemas);
return new ArrayList<String>(schemas);
}

/**
Expand Down
Loading

0 comments on commit 0adb9a1

Please sign in to comment.