InfoLister: sort rows in --list-java-properties and --list-environment-variables v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 24 May 2015 19:17:50 +0200
branchv_0
changeset 201d3db5a72a089
parent 200 2e351d7c26c4
child 202 01078e09b85b
InfoLister: sort rows in --list-java-properties and --list-environment-variables
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sun May 24 19:10:25 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sun May 24 19:17:50 2015 +0200
     1.3 @@ -39,6 +39,8 @@
     1.4  import java.sql.DriverPropertyInfo;
     1.5  import java.sql.SQLException;
     1.6  import java.util.ArrayList;
     1.7 +import java.util.Collections;
     1.8 +import java.util.Comparator;
     1.9  import java.util.EnumSet;
    1.10  import java.util.HashSet;
    1.11  import java.util.List;
    1.12 @@ -125,7 +127,7 @@
    1.13  		for (Entry<Object, Object> e : System.getProperties().entrySet()) {
    1.14  			data.add(new Object[]{e.getKey(), e.getValue()});
    1.15  		}
    1.16 -		printTable(formatter, header, "-- Java system properties", null, data);
    1.17 +		printTable(formatter, header, "-- Java system properties", null, data, 0);
    1.18  	}
    1.19  
    1.20  	private void listEnvironmentVariables() throws FormatterException, ConfigurationException {
    1.21 @@ -134,7 +136,7 @@
    1.22  		for (Entry<String, String> e : System.getenv().entrySet()) {
    1.23  			data.add(new Object[]{e.getKey(), e.getValue()});
    1.24  		}
    1.25 -		printTable(formatter, header, "-- environment variables", null, data);
    1.26 +		printTable(formatter, header, "-- environment variables", null, data, 0);
    1.27  	}
    1.28  
    1.29  	private void listFormatters() throws ConfigurationException, FormatterException {
    1.30 @@ -426,8 +428,24 @@
    1.31  	}
    1.32  
    1.33  	private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data) throws ConfigurationException, FormatterException {
    1.34 +		printTable(formatter, header, sql, parameters, data, null);
    1.35 +	}
    1.36 +
    1.37 +	private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data, final Integer sortByColumn) throws ConfigurationException, FormatterException {
    1.38  		printHeader(formatter, header, sql, parameters);
    1.39  
    1.40 +		if (sortByColumn != null) {
    1.41 +			Collections.sort(data, new Comparator<Object[]>() {
    1.42 +
    1.43 +				@Override
    1.44 +				public int compare(Object[] o1, Object[] o2) {
    1.45 +					String s1 = String.valueOf(o1[sortByColumn]);
    1.46 +					String s2 = String.valueOf(o2[sortByColumn]);
    1.47 +					return s1.compareTo(s2);
    1.48 +				}
    1.49 +			});
    1.50 +		}
    1.51 +
    1.52  		for (Object[] row : data) {
    1.53  			printRow(formatter, row);
    1.54  		}