Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Apr 23, 2023
1 parent 12452d0 commit dde3dde
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsLast;
import static schemacrawler.utility.NamedObjectSort.alphabetical;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -41,7 +40,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;

import schemacrawler.schema.Column;
import schemacrawler.schema.ColumnReference;
import schemacrawler.schema.ForeignKey;
Expand Down Expand Up @@ -70,7 +68,7 @@ private enum TableAssociationType {

private final NamedObjectList<MutableColumn> columns = new NamedObjectList<>();
private final NamedObjectList<TableConstraint> constraints = new NamedObjectList<>();
private final StringBuffer definition;
private final StringBuilder definition;
private final NamedObjectList<MutableForeignKey> foreignKeys = new NamedObjectList<>();
private final NamedObjectList<MutableWeakAssociation> weakAssociations = new NamedObjectList<>();
private final NamedObjectList<MutableColumn> hiddenColumns = new NamedObjectList<>();
Expand All @@ -84,7 +82,7 @@ private enum TableAssociationType {

MutableTable(final Schema schema, final String name) {
super(schema, name);
definition = new StringBuffer();
definition = new StringBuilder();
}

/**
Expand Down Expand Up @@ -353,9 +351,7 @@ void removeTableConstraint(final TableConstraint tableConstraint) {
}

final void setPrimaryKey(final MutablePrimaryKey primaryKey) {
if (primaryKey == null) {
return;
} else {
if (primaryKey != null) {
this.primaryKey = primaryKey;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

package schemacrawler.schemacrawler;

import us.fatehi.utility.scheduler.TaskRunner;
import static us.fatehi.utility.scheduler.TaskRunner.MAX_THREADS;
import static us.fatehi.utility.scheduler.TaskRunner.MIN_THREADS;

public final class LoadOptionsBuilder implements OptionsBuilder<LoadOptionsBuilder, LoadOptions> {

Expand All @@ -46,7 +47,7 @@ public static LoadOptions newLoadOptions() {
/** Default options. */
private LoadOptionsBuilder() {
schemaInfoLevel = SchemaInfoLevelBuilder.standard();
maxThreads = TaskRunner.MAX_THREADS;
maxThreads = MAX_THREADS;
}

@Override
Expand Down Expand Up @@ -80,13 +81,7 @@ public LoadOptionsBuilder withInfoLevel(final InfoLevel infoLevel) {
* @return Maximum number of threads.
*/
public LoadOptionsBuilder withMaxThreads(final int maxThreads) {
if (maxThreads < TaskRunner.MIN_THREADS) {
this.maxThreads = TaskRunner.MIN_THREADS;
} else if (maxThreads > TaskRunner.MAX_THREADS) {
this.maxThreads = TaskRunner.MAX_THREADS;
} else {
this.maxThreads = maxThreads;
}
this.maxThreads = Math.min(Math.max(maxThreads, MIN_THREADS), MAX_THREADS);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void showEnvironment() {

private void showLoaded() {
final boolean isLoadedState = state.isLoaded();
System.out.println(String.format("Database metadata is %sloaded", isLoadedState ? "" : "not "));
System.out.printf("Database metadata is %sloaded%n", isLoadedState ? "" : "not ");
}

private void showStackTrace() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
import static us.fatehi.utility.html.TagBuilder.tableCell;
import static us.fatehi.utility.html.TagBuilder.tableRow;
import static us.fatehi.utility.html.TagOutputFormat.html;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import schemacrawler.crawl.NotLoadedException;
import schemacrawler.schema.Column;
import schemacrawler.schema.ColumnDataType;
Expand Down Expand Up @@ -240,15 +238,13 @@ public void handleTablesStart() {

private String arrowhead(final ForeignKeyCardinality connectivity) {
switch (connectivity) {
case unknown:
return "box";
case zero_one:
return "teeodot";
case zero_many:
return "crowodot";
case one_one:
return "teetee";
default:
default: // Including "unknown"
return "box";
}
}
Expand Down Expand Up @@ -555,13 +551,9 @@ private void printTableColumnAutoIncremented(final Column column) {
}

private void printTableColumnEnumValues(final Column column) {
if (column == null) {
return;
}
if (!column.isColumnDataTypeKnown()) {
return;
}
if (!column.getColumnDataType().isEnumerated()) {
if ((column == null)
|| !column.isColumnDataTypeKnown()
|| !column.getColumnDataType().isEnumerated()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@
import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.Utility.requireNotBlank;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import schemacrawler.schema.AttributedObject;
import schemacrawler.schema.NamedObject;
import us.fatehi.utility.ObjectToString;
Expand Down Expand Up @@ -94,7 +92,7 @@ public int compareTo(final Lint<?> lint) {
return -1;
}

int compareTo = 0;
int compareTo;
compareTo = objectType.compareTo(lint.getObjectType());
if (compareTo != 0) {
return compareTo;
Expand All @@ -112,9 +110,7 @@ public int compareTo(final Lint<?> lint) {
if (compareTo != 0) {
return compareTo;
}
compareTo = message.compareTo(lint.getMessage());

return compareTo;
return message.compareTo(lint.getMessage());
}

@Override
Expand Down Expand Up @@ -190,8 +186,6 @@ public String getValueAsString() {
}
}
valueObject = list;
} else {
valueObject = value;
}
return ObjectToString.listOrObjectToString(valueObject);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import static java.util.Objects.requireNonNull;
import static org.hsqldb.server.ServerConstants.SC_DEFAULT_ADDRESS;
import static org.hsqldb.server.ServerConstants.SC_DEFAULT_HSQL_SERVER_PORT;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
Expand All @@ -43,7 +42,6 @@
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hsqldb.server.Server;

/** Sets up a database schema for tests and examples. */
Expand Down Expand Up @@ -161,10 +159,8 @@ public void start() throws Exception {
/** Shut down the database server. */
public void stop() {
if (trace) {
System.out.println(
String.format(
"Stopping HyperSQL server for database %s:%d/%s",
getHost(), getPort(), getDatabase()));
System.out.printf(
"Stopping HyperSQL server for database %s:%d/%s%n", getHost(), getPort(), getDatabase());
}
stopServer();
}
Expand Down Expand Up @@ -208,13 +204,12 @@ private void startServer() throws IOException {
server.setDatabasePath(0, String.format("file:%s", tempDirectory));

if (trace) {
System.out.println(
String.format(
"Starting HyperSQL server for database %s:%d/%s at %s",
server.getAddress(),
server.getPort(),
server.getDatabaseName(0, true),
server.getDatabasePath(0, true)));
System.out.printf(
"Starting HyperSQL server for database %s:%d/%s at %s%n",
server.getAddress(),
server.getPort(),
server.getDatabaseName(0, true),
server.getDatabasePath(0, true));
}

// Blocked server start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@

public abstract class DatabaseConnector implements Options {

public static final DatabaseConnector UNKNOWN = new UnknownDatabaseConnector();

private final DatabaseServerType dbServerType;
private final Predicate<String> supportsUrl;
private final BiConsumer<InformationSchemaViewsBuilder, Connection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
package schemacrawler.tools.databaseconnector;

import static java.util.Comparator.naturalOrder;
import static schemacrawler.tools.databaseconnector.UnknownDatabaseConnector.UNKNOWN;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.database.DatabaseUtility.checkConnection;
import java.sql.Connection;
Expand Down Expand Up @@ -147,7 +148,7 @@ public DatabaseConnector findDatabaseConnector(final Connection connection) {
final String url = connection.getMetaData().getURL();
return findDatabaseConnectorFromUrl(url);
} catch (final SQLException e) {
return DatabaseConnector.UNKNOWN;
return UNKNOWN;
}
}

Expand All @@ -156,13 +157,13 @@ public DatabaseConnector findDatabaseConnectorFromDatabaseSystemIdentifier(
if (hasDatabaseSystemIdentifier(databaseSystemIdentifier)) {
return databaseConnectorRegistry.get(databaseSystemIdentifier);
} else {
return DatabaseConnector.UNKNOWN;
return UNKNOWN;
}
}

public DatabaseConnector findDatabaseConnectorFromUrl(final String url) {
if (isBlank(url)) {
return DatabaseConnector.UNKNOWN;
return UNKNOWN;
}

for (final DatabaseConnector databaseConnector : databaseConnectorRegistry.values()) {
Expand All @@ -171,7 +172,7 @@ public DatabaseConnector findDatabaseConnectorFromUrl(final String url) {
}
}

return DatabaseConnector.UNKNOWN;
return UNKNOWN;
}

public Collection<PluginCommand> getHelpCommands() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import schemacrawler.schemacrawler.DatabaseServerType;
import us.fatehi.utility.datasource.DatabaseConnectionSourceBuilder;

final class UnknownDatabaseConnector extends DatabaseConnector {
public final class UnknownDatabaseConnector extends DatabaseConnector {

public static final DatabaseConnector UNKNOWN = new UnknownDatabaseConnector();

/** Constructor for unknown databases. Bypass the null-checks of the main constructor */
UnknownDatabaseConnector() {
private UnknownDatabaseConnector() {
super(
DatabaseServerType.UNKNOWN,
url -> false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@

package schemacrawler.tools.options;

import java.io.Serializable;
import java.util.List;

public interface OutputFormat {
public interface OutputFormat extends Serializable {

String getDescription();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

public final class OutputFormatState implements OutputFormat {

private static final long serialVersionUID = -5715099922209080457L;

private final List<String> formatSpecifiers;
private final String description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import schemacrawler.tools.catalogloader.CatalogLoaderRegistry;
import schemacrawler.tools.databaseconnector.DatabaseConnector;
import schemacrawler.tools.databaseconnector.DatabaseConnectorRegistry;
import schemacrawler.tools.databaseconnector.UnknownDatabaseConnector;
import schemacrawler.tools.options.Config;
import us.fatehi.utility.PropertiesUtility;
import us.fatehi.utility.UtilityMarker;
Expand Down Expand Up @@ -202,7 +203,7 @@ private static SchemaRetrievalOptionsBuilder buildSchemaRetrievalOptions(
final boolean useMatchedDatabasePlugin =
useMatchedDatabasePlugin(connection, databaseServerType);
if (!useMatchedDatabasePlugin) {
dbConnector = DatabaseConnector.UNKNOWN;
dbConnector = UnknownDatabaseConnector.UNKNOWN;
}

final SchemaRetrievalOptionsBuilder schemaRetrievalOptionsBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void testDatabaseConnector() throws Exception {

@Test
public void unknownDatabaseConnector() {
final DatabaseConnector databaseConnector = DatabaseConnector.UNKNOWN;
final DatabaseConnector databaseConnector = UnknownDatabaseConnector.UNKNOWN;

final PluginCommand helpCommand = databaseConnector.getHelpCommand();
assertThat(helpCommand, is(notNullValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void run() {
}

private Throwable getCause(final Throwable e) {
Throwable cause = null;
Throwable cause;
Throwable result = e;

while (null != (cause = result.getCause()) && result != cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ protected static Properties createConnectionProperties(

jdbcConnectionProperties = new Properties();
if (user != null) {
jdbcConnectionProperties.put("user", user);
jdbcConnectionProperties.setProperty("user", user);
}
if (password != null) {
jdbcConnectionProperties.put("password", password);
jdbcConnectionProperties.setProperty("password", password);
}
if (connectionProperties != null) {
for (final Map.Entry<String, String> connectionProperty : connectionProperties.entrySet()) {
final String property = connectionProperty.getKey();
final String value = connectionProperty.getValue();
if (jdbcDriverProperties.containsKey(property.toLowerCase()) && value != null) {
jdbcConnectionProperties.put(property, value);
jdbcConnectionProperties.setProperty(property, value);
}
}
}
Expand All @@ -101,7 +101,7 @@ protected static Connection getConnection(
final String username;
final String user = jdbcConnectionProperties.getProperty("user");
if (user != null) {
username = String.format("user \'%s\'", user);
username = String.format("user '%s'", user);
} else {
username = "unspecified user";
}
Expand All @@ -110,7 +110,7 @@ protected static Connection getConnection(
LOGGER.log(
Level.INFO,
new StringFormat(
"Making connection to %s%nfor user \'%s\', with properties %s",
"Making connection to %s%nfor user '%s', with properties %s",
connectionUrl, username, safeProperties(jdbcConnectionProperties)));
// (Using java.sql.DriverManager.getConnection(String, Properties)
// to make a connection is not the best idea,
Expand Down
Loading

0 comments on commit dde3dde

Please sign in to comment.