Mercurial > hg > monetdb-java
changeset 307:05549bc7ed26
Add "final" keyword to classes, method arguments and local variables where possible.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Wed, 21 Aug 2019 19:16:02 +0200 (2019-08-21) |
parents | 7c79ef41b840 |
children | 1948dbcd9991 |
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 |
diffstat | 4 files changed, 111 insertions(+), 108 deletions(-) [+] |
line wrap: on
line diff
--- 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); } }
--- 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.Zhang@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); }
--- 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 ("; - int cols = rsmd.getColumnCount(); - short[] types = new short[cols +1]; + final int cols = rsmd.getColumnCount(); + final short[] types = new short[cols +1]; for (int i = 1; i <= cols; i++) { switch (rsmd.getColumnType(i)) { case Types.CHAR: @@ -415,7 +413,7 @@ public class SQLExporter extends Exporte } } - StringBuilder strbuf = new StringBuilder(1024); + final StringBuilder strbuf = new StringBuilder(1024); strbuf.append(statement); while (rs.next()) { for (int i = 1; i <= cols; i++) { @@ -435,7 +433,7 @@ public class SQLExporter extends Exporte } } - public void resultSetToSQLDump(ResultSet rs) { + public void resultSetToSQLDump(final ResultSet rs) { // TODO: write copy into statement } @@ -446,12 +444,12 @@ public class SQLExporter extends Exporte * @param rs the ResultSet to write out * @throws SQLException if a database related error occurs */ - public void resultSetToTable(ResultSet rs) throws SQLException { - ResultSetMetaData md = rs.getMetaData(); - int cols = md.getColumnCount(); + public void resultSetToTable(final ResultSet rs) throws SQLException { + final ResultSetMetaData md = rs.getMetaData(); + final int cols = md.getColumnCount(); // find the optimal display widths of the columns - int[] width = new int[cols + 1]; - boolean[] isSigned = new boolean[cols + 1]; // used for controlling left or right alignment of data + final int[] width = new int[cols + 1]; + final boolean[] isSigned = new boolean[cols + 1]; // used for controlling left or right alignment of data for (int j = 1; j < width.length; j++) { int coldisplaysize = md.getColumnDisplaySize(j); int collabellength = md.getColumnLabel(j).length(); @@ -462,14 +460,14 @@ public class SQLExporter extends Exporte } // use a buffer to construct the text lines - StringBuilder strbuf = new StringBuilder(1024); + final StringBuilder strbuf = new StringBuilder(1024); // construct the frame lines and header text strbuf.append('+'); for (int j = 1; j < width.length; j++) strbuf.append(repeat('-', width[j] + 1) + "-+"); - String outsideLine = strbuf.toString(); + final String outsideLine = strbuf.toString(); strbuf.setLength(0); // clear the buffer strbuf.append('|'); @@ -524,7 +522,7 @@ public class SQLExporter extends Exporte out.println(count + " row" + (count != 1 ? "s" : "")); } - private void changeSchema(String schema) { + private void changeSchema(final String schema) { if (lastSchema == null) { lastSchema = new Stack<String>(); lastSchema.push(null);
--- a/src/main/java/nl/cwi/monetdb/util/XMLExporter.java +++ b/src/main/java/nl/cwi/monetdb/util/XMLExporter.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; @@ -19,30 +18,31 @@ import java.text.SimpleDateFormat; import java.util.HashSet; import java.util.Set; -public class XMLExporter extends Exporter { +public final class XMLExporter extends Exporter { private boolean useNil; public final static int TYPE_NIL = 1; public final static int VALUE_OMIT = 0; public final static int VALUE_XSI = 1; - public XMLExporter(PrintWriter out) { + public XMLExporter(final java.io.PrintWriter out) { super(out); } 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 { 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); - if (!tbl.next()) throw new SQLException("Whoops no data for " + name); + final ResultSet tbl = dbmd.getTables(catalog, schema, name, types); + if (!tbl.next()) + throw new SQLException("Whoops no data for " + name); // This will probably only work for MonetDB out.print("<!-- unable to represent: CREATE " + type + " " + @@ -55,9 +55,9 @@ public class XMLExporter extends Exporte out.println("<xsd:schema>"); - ResultSet cols = dbmd.getColumns(catalog, schema, name, null); + final ResultSet cols = dbmd.getColumns(catalog, schema, name, null); String ident; - Set<String> types = new HashSet<String>(); + final Set<String> types = new HashSet<String>(); // walk through the ResultSet and create the types // for a bit of a clue on the types, see this url: // http://books.xmlschemata.org/relaxng/relax-CHP-19.html @@ -65,7 +65,8 @@ public class XMLExporter extends Exporte switch (cols.getInt("DATA_TYPE")) { case Types.CHAR: ident = "CHAR_" + cols.getString("COLUMN_SIZE"); - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -81,7 +82,8 @@ public class XMLExporter extends Exporte case Types.VARCHAR: case Types.LONGVARCHAR: ident = "VARCHAR_" + cols.getString("COLUMN_SIZE"); - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -96,7 +98,8 @@ public class XMLExporter extends Exporte break; case Types.CLOB: ident = "CLOB"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -109,7 +112,8 @@ public class XMLExporter extends Exporte case Types.NUMERIC: ident = "DECIMAL_" + cols.getString("COLUMN_SIZE") + "_" + cols.getString("DECIMAL_DIGITS"); - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -127,7 +131,8 @@ public class XMLExporter extends Exporte break; case Types.TINYINT: ident = "TINYINT"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -138,7 +143,8 @@ public class XMLExporter extends Exporte break; case Types.SMALLINT: ident = "SMALLINT"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -149,7 +155,8 @@ public class XMLExporter extends Exporte break; case Types.INTEGER: ident = "INTEGER"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -160,7 +167,8 @@ public class XMLExporter extends Exporte break; case Types.BIGINT: ident = "BIGINT"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -171,7 +179,8 @@ public class XMLExporter extends Exporte break; case Types.BIT: ident = "BIT"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -182,7 +191,8 @@ public class XMLExporter extends Exporte break; case Types.BOOLEAN: ident = "BOOLEAN"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -193,7 +203,8 @@ public class XMLExporter extends Exporte break; case Types.DATE: ident = "DATE"; - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -208,7 +219,8 @@ public class XMLExporter extends Exporte } else { ident = "TIME"; } - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -223,7 +235,8 @@ public class XMLExporter extends Exporte } else { ident = "TIMESTAMP"; } - if (types.contains(ident)) break; + if (types.contains(ident)) + break; types.add(ident); out.print(" <xsd:simpleType name="); @@ -337,9 +350,9 @@ public class XMLExporter extends Exporte * * @param rs the ResultSet */ - public void dumpResultSet(ResultSet rs) throws SQLException { + public void dumpResultSet(final ResultSet rs) throws SQLException { // write simple XML serialisation - ResultSetMetaData rsmd = rs.getMetaData(); + final ResultSetMetaData rsmd = rs.getMetaData(); if (!useSchema) out.println("<" + rsmd.getSchemaName(1) + ">"); out.println("<" + rsmd.getTableName(1) + ">"); @@ -387,7 +400,7 @@ public class XMLExporter extends Exporte if (!useSchema) out.println("</" + rsmd.getSchemaName(1) + ">"); } - public void setProperty(int type, int value) throws Exception { + public void setProperty(final int type, final int value) throws Exception { switch (type) { case TYPE_NIL: switch (value) { @@ -406,7 +419,7 @@ public class XMLExporter extends Exporte } } - public int getProperty(int type) throws Exception { + public int getProperty(final int type) throws Exception { switch (type) { case TYPE_NIL: return useNil ? VALUE_XSI : VALUE_OMIT;