Changeset: 05549bc7ed26 for monetdb-java URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=05549bc7ed26 Modified Files: src/main/java/nl/cwi/monetdb/util/Exporter.java src/main/java/nl/cwi/monetdb/util/Extract.java src/main/java/nl/cwi/monetdb/util/SQLExporter.java src/main/java/nl/cwi/monetdb/util/XMLExporter.java Branch: default Log Message:
Add "final" keyword to classes, method arguments and local variables where possible. diffs (truncated from 594 to 300 lines): diff --git a/src/main/java/nl/cwi/monetdb/util/Exporter.java b/src/main/java/nl/cwi/monetdb/util/Exporter.java --- a/src/main/java/nl/cwi/monetdb/util/Exporter.java +++ b/src/main/java/nl/cwi/monetdb/util/Exporter.java @@ -12,31 +12,30 @@ import java.io.PrintWriter; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Arrays; public abstract class Exporter { protected PrintWriter out; protected boolean useSchema; - protected Exporter(PrintWriter out) { + protected Exporter(final PrintWriter out) { this.out = out; } public abstract void dumpSchema( - DatabaseMetaData dbmd, - String type, - String catalog, - String schema, - String name) throws SQLException; + final DatabaseMetaData dbmd, + final String type, + final String catalog, + final String schema, + final String name) throws SQLException; - public abstract void dumpResultSet(ResultSet rs) throws SQLException; + public abstract void dumpResultSet(final ResultSet rs) throws SQLException; - public abstract void setProperty(int type, int value) throws Exception; - public abstract int getProperty(int type) throws Exception; + public abstract void setProperty(final int type, final int value) throws Exception; + public abstract int getProperty(final int type) throws Exception; //=== shared utilities - public void useSchemas(boolean use) { + public void useSchemas(final boolean use) { useSchema = use; } @@ -47,7 +46,7 @@ public abstract class Exporter { * @param in the string to quote * @return the quoted string */ - protected static String dq(String in) { + protected static String dq(final String in) { return "\"" + in.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"") + "\""; } @@ -58,7 +57,7 @@ public abstract class Exporter { * @param in the string to quote * @return the quoted string */ - protected static String q(String in) { + protected static String q(final String in) { return "'" + in.replaceAll("\\\\", "\\\\\\\\").replaceAll("'", "\\\\'") + "'"; } @@ -70,9 +69,9 @@ public abstract class Exporter { * @param cnt the number of times to repeat chr * @return a String holding cnt times chr */ - protected static String repeat(char chr, int cnt) { - char[] buf = new char[cnt]; - Arrays.fill(buf, chr); + protected static String repeat(final char chr, final int cnt) { + final char[] buf = new char[cnt]; + java.util.Arrays.fill(buf, chr); return new String(buf); } } diff --git a/src/main/java/nl/cwi/monetdb/util/Extract.java b/src/main/java/nl/cwi/monetdb/util/Extract.java --- a/src/main/java/nl/cwi/monetdb/util/Extract.java +++ b/src/main/java/nl/cwi/monetdb/util/Extract.java @@ -12,9 +12,6 @@ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - /** * This file contains a function to extract files from its including Jar @@ -23,41 +20,37 @@ import java.io.InputStreamReader; * @author Ying Zhang "y.zh...@cwi.nl" * @version 0.1 */ - public class Extract { private static final int DEFAULT_BUFSIZE = 16386; - public Extract() {} + public Extract() {} - /** - * Extracts a file from the Jar package which includes this class to - * the given destination - * @param fromFile The file to extract, including it absolute path - * inside its including Jar package. - * @param toFile Destination for the extracted file - * @throws FileNotFoundException If the file to extract can not be - * found in its including Jar package. - * @throws IOException If any error happens during - * creating/reading/writing files. - */ - public static void extractFile(String fromFile, String toFile) + /** + * Extracts a file from the Jar package which includes this class to + * the given destination + * @param fromFile The file to extract, including it absolute path + * inside its including Jar package. + * @param toFile Destination for the extracted file + * @throws FileNotFoundException If the file to extract can not be + * found in its including Jar package. + * @throws IOException If any error happens during + * creating/reading/writing files. + */ + public static void extractFile(final String fromFile, final String toFile) throws FileNotFoundException, IOException { - char[] cbuf = new char[DEFAULT_BUFSIZE]; - int ret = 0; - - InputStream is = new Extract().getClass().getResourceAsStream(fromFile); - - if(is == null) { + java.io.InputStream is = new Extract().getClass().getResourceAsStream(fromFile); + if (is == null) { throw new FileNotFoundException("File " + fromFile + " does not exist in the JAR package."); } - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + BufferedReader reader = new BufferedReader(new java.io.InputStreamReader(is)); FileWriter writer = new FileWriter(toFile, false); - ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE); - while(ret > 0){ + final char[] cbuf = new char[DEFAULT_BUFSIZE]; + int ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE); + while (ret > 0) { writer.write(cbuf, 0, ret); ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE); } diff --git a/src/main/java/nl/cwi/monetdb/util/SQLExporter.java b/src/main/java/nl/cwi/monetdb/util/SQLExporter.java --- a/src/main/java/nl/cwi/monetdb/util/SQLExporter.java +++ b/src/main/java/nl/cwi/monetdb/util/SQLExporter.java @@ -8,7 +8,6 @@ package nl.cwi.monetdb.util; -import java.io.PrintWriter; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.ResultSetMetaData; @@ -18,11 +17,9 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; -import java.util.SortedMap; import java.util.Stack; -import java.util.TreeMap; -public class SQLExporter extends Exporter { +public final class SQLExporter extends Exporter { private int outputMode; private Stack<String> lastSchema; @@ -31,7 +28,7 @@ public class SQLExporter extends Exporte public final static int VALUE_COPY = 1; public final static int VALUE_TABLE = 2; - public SQLExporter(PrintWriter out) { + public SQLExporter(final java.io.PrintWriter out) { super(out); } @@ -48,11 +45,11 @@ public class SQLExporter extends Exporte * @throws SQLException if a database related error occurs */ public void dumpSchema( - DatabaseMetaData dbmd, - String type, - String catalog, - String schema, - String name) + final DatabaseMetaData dbmd, + final String type, + final String catalog, + final String schema, + final String name) throws SQLException { assert dbmd != null; @@ -60,21 +57,21 @@ public class SQLExporter extends Exporte assert schema != null; assert name != null; - String fqname = (!useSchema ? dq(schema) + "." : "") + dq(name); + final String fqname = (!useSchema ? dq(schema) + "." : "") + dq(name); if (useSchema) changeSchema(schema); // handle views directly if (type.indexOf("VIEW") != -1) { - String[] types = new String[1]; + final String[] types = new String[1]; types[0] = type; - ResultSet tbl = dbmd.getTables(catalog, schema, name, types); + final ResultSet tbl = dbmd.getTables(catalog, schema, name, types); if (!tbl.next()) throw new SQLException("Whoops no meta data for view " + fqname); // This will probably only work for MonetDB - String remarks = tbl.getString("REMARKS"); // for MonetDB driver this contains the view definition + final String remarks = tbl.getString("REMARKS"); // for MonetDB driver this contains the view definition if (remarks == null) { out.println("-- invalid " + type + " " + fqname + ": no definition found"); } else { @@ -93,15 +90,16 @@ public class SQLExporter extends Exporte int colNmIndex = cols.findColumn("COLUMN_NAME"); int colTypeNmIndex = cols.findColumn("TYPE_NAME"); - ResultSetMetaData rsmd = cols.getMetaData(); - int colwidth = rsmd.getColumnDisplaySize(colNmIndex); + final ResultSetMetaData rsmd = cols.getMetaData(); + final int colwidth = rsmd.getColumnDisplaySize(colNmIndex); int typewidth = rsmd.getColumnDisplaySize(colTypeNmIndex); if (typewidth < 13) typewidth = 13; // use minimal 13 characters for the typename (same as used in mclient) - StringBuilder sb = new StringBuilder(128); + final StringBuilder sb = new StringBuilder(128); for (i = 0; cols.next(); i++) { - if (i > 0) out.println(","); + if (i > 0) + out.println(","); // print column name (with double quotes) s = dq(cols.getString(colNmIndex)); @@ -185,7 +183,7 @@ public class SQLExporter extends Exporte // key sequence order. So we have to sort ourself :( cols = dbmd.getPrimaryKeys(catalog, schema, name); // first make an 'index' of the KEY_SEQ column - SortedMap<Integer, Integer> seqIndex = new TreeMap<Integer, Integer>(); + final java.util.SortedMap<Integer, Integer> seqIndex = new java.util.TreeMap<Integer, Integer>(); for (i = 1; cols.next(); i++) { seqIndex.put(Integer.valueOf(cols.getInt("KEY_SEQ")), Integer.valueOf(i)); } @@ -319,7 +317,7 @@ public class SQLExporter extends Exporte * @param rs the ResultSet to dump * @throws SQLException if a database error occurs */ - public void dumpResultSet(ResultSet rs) throws SQLException { + public void dumpResultSet(final ResultSet rs) throws SQLException { switch (outputMode) { case VALUE_INSERT: resultSetToSQL(rs); @@ -333,7 +331,7 @@ public class SQLExporter extends Exporte } } - public void setProperty(int type, int value) throws Exception { + public void setProperty(final int type, final int value) throws Exception { switch (type) { case TYPE_OUTPUT: switch (value) { @@ -351,7 +349,7 @@ public class SQLExporter extends Exporte } } - public int getProperty(int type) throws Exception { + public int getProperty(final int type) throws Exception { switch (type) { case TYPE_OUTPUT: return outputMode; @@ -371,10 +369,10 @@ public class SQLExporter extends Exporte * @param absolute if true, dumps table name prepended with schema name * @throws SQLException if a database related error occurs */ - private void resultSetToSQL(ResultSet rs) + private void resultSetToSQL(final ResultSet rs) throws SQLException { - ResultSetMetaData rsmd = rs.getMetaData(); + final ResultSetMetaData rsmd = rs.getMetaData(); String statement = "INSERT INTO "; if (!useSchema) { String schema = rsmd.getSchemaName(1); @@ -383,8 +381,8 @@ public class SQLExporter extends Exporte } statement += dq(rsmd.getTableName(1)) + " VALUES ("; _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list