Revision: 4870 http://sourceforge.net/p/jump-pilot/code/4870 Author: michaudm Date: 2016-03-28 13:36:32 +0000 (Mon, 28 Mar 2016) Log Message: ----------- Cleaning, formatting rewriting jump.util package
Modified Paths: -------------- core/trunk/src/com/vividsolutions/jump/util/CollectionWrapper.java core/trunk/src/com/vividsolutions/jump/util/CoordinateArrays.java core/trunk/src/com/vividsolutions/jump/util/DebugTimer.java core/trunk/src/com/vividsolutions/jump/util/FileUtil.java core/trunk/src/com/vividsolutions/jump/util/FlexibleDateParser.java core/trunk/src/com/vividsolutions/jump/util/Fmt.java core/trunk/src/com/vividsolutions/jump/util/ListWrapper.java core/trunk/src/com/vividsolutions/jump/util/StringUtil.java Added Paths: ----------- core/trunk/src/jumptest/junit/FileUtilTestCase.java Modified: core/trunk/src/com/vividsolutions/jump/util/CollectionWrapper.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/CollectionWrapper.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/CollectionWrapper.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -3,9 +3,10 @@ import java.util.Collection; import java.util.Iterator; -public abstract class CollectionWrapper implements Collection { - public abstract Collection getCollection(); +public abstract class CollectionWrapper<T> implements Collection<T> { + public abstract Collection<T> getCollection(); + public int size() { return getCollection().size(); } @@ -22,7 +23,7 @@ return getCollection().toArray(); } - public boolean add(Object o) { + public boolean add(T o) { return getCollection().add(o); } @@ -34,27 +35,27 @@ return getCollection().remove(o); } - public boolean addAll(Collection c) { + public boolean addAll(Collection<? extends T> c) { return getCollection().addAll(c); } - public boolean containsAll(Collection c) { + public boolean containsAll(Collection<?> c) { return getCollection().containsAll(c); } - public boolean removeAll(Collection c) { + public boolean removeAll(Collection<?> c) { return getCollection().removeAll(c); } - public boolean retainAll(Collection c) { + public boolean retainAll(Collection<?> c) { return getCollection().retainAll(c); } - public Iterator iterator() { + public Iterator<T> iterator() { return getCollection().iterator(); } - public Object[] toArray(Object[] a) { + public <T> T[] toArray(T[] a) { return getCollection().toArray(a); } } \ No newline at end of file Modified: core/trunk/src/com/vividsolutions/jump/util/CoordinateArrays.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/CoordinateArrays.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/CoordinateArrays.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -36,7 +36,6 @@ import java.util.*; import com.vividsolutions.jts.algorithm.CGAlgorithms; -import com.vividsolutions.jts.algorithm.RobustCGAlgorithms; import com.vividsolutions.jts.geom.*; import com.vividsolutions.jts.util.Assert; Modified: core/trunk/src/com/vividsolutions/jump/util/DebugTimer.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/DebugTimer.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/DebugTimer.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -37,15 +37,17 @@ public class DebugTimer { - private static final int TIME_LEN = 10; + private static DebugTimer timer = new DebugTimer(); + private final String blankStr; + private final int TIME_LEN; private Stopwatch sw = null; - private String blankStr; public DebugTimer() { sw = new Stopwatch(); sw.start(); - blankStr = fillString(TIME_LEN, ' '); + blankStr = " "; + TIME_LEN = blankStr.length(); } public static void startStatic(String msg) { @@ -74,18 +76,8 @@ return filled.substring(start); } - // don't pad if it's already longer return timeStr; } - public String fillString(int len, char ch) { - StringBuffer buf = new StringBuffer(len); - - for (int i = 0; i < len; i++) { - buf.append(ch); - } - - return buf.toString(); - } } Modified: core/trunk/src/com/vividsolutions/jump/util/FileUtil.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/FileUtil.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/FileUtil.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -61,26 +61,23 @@ /** * Gets the content of filename as a list of lines. * - * @param filename - * name of the input file + * @param filename name of the input file * @return a list of strings representing the file's lines * @throws IOException */ - public static List getContents(String filename) throws IOException { + public static List<String> getContents(String filename) throws IOException { return getContents(new FileInputStream(filename)); } /** * Gets the content of filename as a list of lines. * - * @param filename - * name of the input file - * @param encoding - * charset to use to decode filename + * @param filename name of the input file + * @param encoding charset to use to decode filename * @return a list of strings representing the file's lines * @throws IOException */ - public static List getContents(String filename, String encoding) + public static List<String> getContents(String filename, String encoding) throws IOException { return getContents(new FileInputStream(filename), encoding); } @@ -88,23 +85,22 @@ /** * Gets the content a compressed file passed as an URI. * - * @param uri - * uri of the input resource + * @param uri uri of the input resource * @return a list of strings representing the compressed file's lines * @throws IOException */ - public static List getContents(URI uri) throws IOException { + public static List<String> getContents(URI uri) throws IOException { return getContents(CompressedFile.openFile(uri)); } /** * Gets the content of an inputSteam as a list of lines. * - * @param inputStream + * @param inputStream inputStream to read from * @return a list of lines * @throws IOException */ - public static List getContents(InputStream inputStream) throws IOException { + public static List<String> getContents(InputStream inputStream) throws IOException { return getContents(inputStream, Charset.defaultCharset().name()); } @@ -112,28 +108,20 @@ * Gets the content of an inputSteam as a list of lines. inputStream is * decoded with the specified charset. * - * @param inputStream + * @param inputStream inputStream to read from + * @param encoding encoding of the inputStream * @return a list of lines * @throws IOException */ - public static List getContents(InputStream inputStream, String encoding) + public static List<String> getContents(InputStream inputStream, String encoding) throws IOException { - ArrayList contents = new ArrayList(); - InputStreamReader inputStreamReader = new InputStreamReader(inputStream, - encoding); - try { - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); - try { - String line = bufferedReader.readLine(); - while (line != null) { + List<String> contents = new ArrayList<>(); + try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream,encoding); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { + String line; + while (null != (line = bufferedReader.readLine())) { contents.add(line); - line = bufferedReader.readLine(); } - } finally { - bufferedReader.close(); - } - } finally { - inputStreamReader.close(); } return contents; } @@ -141,12 +129,9 @@ /** * Saves the String to a file with the given filename. * - * @param filename - * the pathname of the file to create (or overwrite) - * @param contents - * the data to save - * @throws IOException - * if an I/O error occurs. + * @param filename the pathname of the file to create (or overwrite) + * @param contents the data to save + * @throws IOException if an I/O error occurs. */ public static void setContents(String filename, String contents) throws IOException { @@ -156,14 +141,11 @@ /** * Saves the List of Strings to a file with the given filename. * - * @param filename - * the pathname of the file to create (or overwrite) - * @param lines - * the Strings to save as lines in the file - * @throws IOException - * if an I/O error occurs. + * @param filename the pathname of the file to create (or overwrite) + * @param lines the Strings to save as lines in the file + * @throws IOException if an I/O error occurs. */ - public static void setContents(String filename, List lines) + public static void setContents(String filename, List<String> lines) throws IOException { setContents(filename, lines, Charset.defaultCharset().name()); } @@ -171,16 +153,13 @@ /** * Saves lines into a file named filename, using encoding charset. * - * @param filename - * the pathname of the file to create (or overwrite) - * @param contents - * the data to save - * @throws IOException - * if an I/O error occurs. + * @param filename the pathname of the file to create (or overwrite) + * @param contents the data to save + * @throws IOException if an I/O error occurs. */ public static void setContents(String filename, String contents, String encoding) throws IOException { - List<String> lines = new ArrayList<String>(); + List<String> lines = new ArrayList<>(); lines.add(contents); setContents(filename, lines, encoding); } @@ -188,72 +167,41 @@ /** * Saves lines into a file named filename, using encoding charset. * - * @param filename - * the pathname of the file to create (or overwrite) - * @param lines - * the data to save - * @throws IOException - * if an I/O error occurs. + * @param filename the pathname of the file to create (or overwrite) + * @param lines the data to save + * @throws IOException if an I/O error occurs. */ public static void setContents(String filename, List<String> lines, String encoding) throws IOException { - OutputStreamWriter osw = null; - try { - osw = new OutputStreamWriter(new FileOutputStream(filename), encoding); - String lineSep = System.getProperty("line.separator"); + try (FileOutputStream fos = new FileOutputStream(filename); + OutputStreamWriter osw = new OutputStreamWriter(fos, encoding)) { + String lineSep = System.lineSeparator(); for (Iterator<String> it = lines.iterator(); it.hasNext();) { osw.write(it.next()); if (it.hasNext()) { osw.write(lineSep); } } - } finally { - if (osw != null) { - try { - osw.close(); - } catch (IOException e) { - } - } } } - public static void zip(Collection files, File zipFile) throws IOException { - FileOutputStream fos = new FileOutputStream(zipFile); - try { - BufferedOutputStream bos = new BufferedOutputStream(fos); - try { - ZipOutputStream zos = new ZipOutputStream(bos); - try { - for (Iterator i = files.iterator(); i.hasNext();) { - File file = (File) i.next(); - zos.putNextEntry(new ZipEntry(file.getName())); - FileInputStream fis = new FileInputStream(file); - try { - BufferedInputStream bis = new BufferedInputStream(fis); - try { - while (true) { - int j = bis.read(); - if (j == -1) { - break; - } - zos.write(j); - } - } finally { - bis.close(); - } - } finally { - fis.close(); - zos.closeEntry(); + public static void zip(Collection<File> files, File zipFile) throws IOException { + try (FileOutputStream fos = new FileOutputStream(zipFile); + BufferedOutputStream bos = new BufferedOutputStream(fos); + ZipOutputStream zos = new ZipOutputStream(bos)) { + for (File file : files) { + zos.putNextEntry(new ZipEntry(file.getName())); + try(FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis)) { + while (true) { + int j = bis.read(); + if (j == -1) { + break; } + zos.write(j); } - } finally { - zos.close(); } - } finally { - bos.close(); } - } finally { - fos.close(); } } @@ -315,13 +263,11 @@ * utility method to copy an inputstream to a temp file for further processing * NOTE: prefix, suffix, monitor are optional and may be {@link <code>null</code>} * - * @param in - * @param prefix - * - default "openjump" - * @param suffix - * - default ".tmp" - * @param monitor - * @return + * @param in inputSteam to copy + * @param prefix file name prefix - default "openjump" + * @param suffix file name suffix - default ".tmp" + * @param monitor to get feedback during the stream reading + * @return the temp file * @throws IOException */ public static File copyInputStreamToTempFile(InputStream in, String prefix, @@ -334,12 +280,12 @@ tempFile)); long counter = 0; - int r = 0; + int r; byte[] b = new byte[1024]; long startCounter = counter; long stopCounter = counter; long startTime = System.currentTimeMillis(); - long stopTime = 0; + long stopTime; int i = 0; float speed = 0; while ((r = in.read(b)) != -1) { Modified: core/trunk/src/com/vividsolutions/jump/util/FlexibleDateParser.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/FlexibleDateParser.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/FlexibleDateParser.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -32,7 +32,6 @@ package com.vividsolutions.jump.util; import com.vividsolutions.jts.util.Assert; -import com.vividsolutions.jump.I18N; import java.awt.Color; import java.awt.Component; @@ -61,8 +60,8 @@ */ public class FlexibleDateParser { - private static Collection lenientFormatters = null; - private static Collection unlenientFormatters = null; + private static Collection<SimpleDateFormat> lenientFormatters = null; + private static Collection<SimpleDateFormat> unlenientFormatters = null; //CellEditor used to be a static field CELL_EDITOR, but I was getting //problems calling it from ESETextField (it simply didn't appear). @@ -114,7 +113,7 @@ public Object getCellEditorValue() { return value; } - }; + } public static final class CellRenderer extends DefaultTableCellRenderer { @@ -132,21 +131,21 @@ private boolean verbose = false; - private Collection sortByComplexity(Collection patterns) { + private Collection<DatePattern> sortByComplexity(Collection<DatePattern> patterns) { //Least complex to most complex. [Jon Aquino] - TreeSet sortedPatterns = new TreeSet(new Comparator() { - public int compare(Object o1, Object o2) { - int result = complexity(o1.toString()) - complexity(o2.toString()); + TreeSet<DatePattern> sortedPatterns = new TreeSet<>(new Comparator<DatePattern>() { + public int compare(DatePattern o1, DatePattern o2) { + int result = complexity(o1.pattern) - complexity(o2.pattern); if (result == 0) { //The two patterns have the same level of complexity. //Sort by order of appearance (e.g. to resolve //MM/dd/yyyy vs dd/MM/yyyy [Jon Aquino] - result = ((Pattern) o1).index - ((Pattern) o2).index; + result = o1.index - o2.index; } return result; } - private TreeSet uniqueCharacters = new TreeSet(); + private TreeSet<String> uniqueCharacters = new TreeSet<>(); private int complexity(String pattern) { uniqueCharacters.clear(); @@ -165,19 +164,17 @@ return sortedPatterns; } - private Collection lenientFormatters() { + private Collection<SimpleDateFormat> lenientFormatters() { if (lenientFormatters == null) { load(); } - return lenientFormatters; } - private Collection unlenientFormatters() { + private Collection<SimpleDateFormat> unlenientFormatters() { if (unlenientFormatters == null) { load(); } - return unlenientFormatters; } @@ -211,11 +208,10 @@ } } - private Date parse(String s, Collection formatters) throws ParseException { + private Date parse(String s, Collection<SimpleDateFormat> formatters) throws ParseException { ParseException firstParseException = null; - for (Iterator i = formatters.iterator(); i.hasNext();) { - SimpleDateFormat formatter = (SimpleDateFormat) i.next(); + for (SimpleDateFormat formatter : formatters) { if (verbose) { System.out.println( @@ -258,17 +254,19 @@ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); - if ((calendar.get(Calendar.YEAR) == 1970) && (s.indexOf("70") == -1)) { + // If the parser found date is in year 1970 and 70 is not in the + // original String, set year to current year : why ??? + if ((calendar.get(Calendar.YEAR) == 1970) && !s.contains("70")) { calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR)); } return calendar.getTime(); } - private static class Pattern { + private static class DatePattern { private String pattern; private int index; - public Pattern(String pattern, int index) { + public DatePattern(String pattern, int index) { this.pattern = pattern; this.index = index; } @@ -292,53 +290,38 @@ // Does not use 18N to be able to reload another language file dynamically // (with 18N, things seems to be started only once at the start of the application) ResourceBundle resourceBundle = ResourceBundle.getBundle("language/jump"); - InputStream inputStream = - getClass().getResourceAsStream(resourceBundle - .getString("com.vividsolutions.jump.util.FlexibleDateParser")); - try { - try { - Collection patterns = new ArrayList(); - int index = 0; - for (Iterator i = FileUtil.getContents(inputStream).iterator(); - i.hasNext(); - ) { - String line = ((String) i.next()).trim(); + try (InputStream inputStream = + getClass().getResourceAsStream(resourceBundle.getString( + "com.vividsolutions.jump.util.FlexibleDateParser") + )) { + Collection<DatePattern> patterns = new ArrayList<>(); + int index = 0; + for (String line : FileUtil.getContents(inputStream)) { - if (line.startsWith("#")) { - continue; - } - - if (line.length() == 0) { - continue; - } - - patterns.add(new Pattern(line, index)); + if (line.trim().length() > 0 && !line.startsWith("#")) { + patterns.add(new DatePattern(line, index)); index++; } - unlenientFormatters = toFormatters(false, patterns); - lenientFormatters = toFormatters(true, patterns); - } finally { - inputStream.close(); } + unlenientFormatters = toFormatters(false, patterns); + lenientFormatters = toFormatters(true, patterns); } catch (IOException e) { Assert.shouldNeverReachHere(e.toString()); } } } - private Collection toFormatters(boolean lenient, Collection patterns) { - ArrayList formatters = new ArrayList(); + private Collection<SimpleDateFormat> toFormatters(boolean lenient, Collection<DatePattern> patterns) { + List<SimpleDateFormat> formatters = new ArrayList<>(); //Sort from least complex to most complex; otherwise, ddMMMyyyy //instead of MMMd will match "May 15". [Jon Aquino] - for (Iterator i = sortByComplexity(patterns).iterator(); i.hasNext();) { - Pattern pattern = (Pattern) i.next(); + for (DatePattern pattern : sortByComplexity(patterns)) { SimpleDateFormat formatter = new SimpleDateFormat(pattern.pattern); formatter.setLenient(lenient); formatters.add(formatter); } - return formatters; } Modified: core/trunk/src/com/vividsolutions/jump/util/Fmt.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/Fmt.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/Fmt.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -603,7 +603,7 @@ ((expStr.length() != 0) ? ("e" + expStr) : ""); } - /****************************************************************************** + ///****************************************************************************** /// Test program. public static void main( String[] args ) { @@ -673,5 +673,5 @@ { System.out.println( "#" + str + "#" ); } - ******************************************************************************/ + //******************************************************************************/ } Modified: core/trunk/src/com/vividsolutions/jump/util/ListWrapper.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/ListWrapper.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/ListWrapper.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -4,20 +4,21 @@ import java.util.List; import java.util.ListIterator; -public abstract class ListWrapper extends CollectionWrapper implements List { - public List getList() { - return (List) getCollection(); +public abstract class ListWrapper<T> extends CollectionWrapper<T> implements List<T> { + + public List<T> getList() { + return (List<T>) getCollection(); } - public Object get(int index) { + public T get(int index) { return getList().get(index); } - public Object remove(int index) { + public T remove(int index) { return getList().remove(index); } - public void add(int index, Object element) { + public void add(int index, T element) { getList().add(index, element); } @@ -29,23 +30,23 @@ return getList().lastIndexOf(o); } - public boolean addAll(int index, Collection c) { + public boolean addAll(int index, Collection<? extends T> c) { return getList().addAll(index, c); } - public List subList(int fromIndex, int toIndex) { + public List<T> subList(int fromIndex, int toIndex) { return getList().subList(fromIndex, toIndex); } - public ListIterator listIterator() { + public ListIterator<T> listIterator() { return getList().listIterator(); } - public ListIterator listIterator(int index) { + public ListIterator<T> listIterator(int index) { return getList().listIterator(index); } - public Object set(int index, Object element) { + public T set(int index, T element) { return getList().set(index, element); } } \ No newline at end of file Modified: core/trunk/src/com/vividsolutions/jump/util/StringUtil.java =================================================================== --- core/trunk/src/com/vividsolutions/jump/util/StringUtil.java 2016-03-27 15:37:31 UTC (rev 4869) +++ core/trunk/src/com/vividsolutions/jump/util/StringUtil.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -363,4 +363,14 @@ public static boolean isEmpty(String value) { return (value == null || value.trim().length() == 0); } + + public static String fillString(int len, char ch) { + StringBuilder buf = new StringBuilder(len); + + for (int i = 0; i < len; i++) { + buf.append(ch); + } + + return buf.toString(); + } } Added: core/trunk/src/jumptest/junit/FileUtilTestCase.java =================================================================== --- core/trunk/src/jumptest/junit/FileUtilTestCase.java (rev 0) +++ core/trunk/src/jumptest/junit/FileUtilTestCase.java 2016-03-28 13:36:32 UTC (rev 4870) @@ -0,0 +1,60 @@ +package jumptest.junit; + +import com.vividsolutions.jump.util.FileUtil; +import junit.framework.TestCase; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Arrays; + +/** + * Created by UMichael on 28/03/2016. + */ +public class FileUtilTestCase extends TestCase { + + public FileUtilTestCase(String Name_) { + super(Name_); + } + + public static void main(String[] args) { + String[] testCaseName = {FileUtilTestCase.class.getName()}; + junit.textui.TestRunner.main(testCaseName); + } + + public void testZip() throws IOException { + File tmp1 = File.createTempFile("testZip",".txt"); + File tmp2 = File.createTempFile("testZip",".txt"); + File tmp3 = File.createTempFile("testZip",".txt"); + File zip1 = File.createTempFile("testZip",".zip"); + try (FileWriter fw1 = new FileWriter(tmp1); + FileWriter fw2 = new FileWriter(tmp2); + FileWriter fw3 = new FileWriter(tmp3); + BufferedWriter bw1 = new BufferedWriter(fw1); + BufferedWriter bw2 = new BufferedWriter(fw2); + BufferedWriter bw3 = new BufferedWriter(fw3)) { + for (int i = 0 ; i < 1000 ; i++) { + bw1.write("azertyuiop\n"); + bw2.write("qsdfghjklm\n"); + bw3.write("wxcvbn\n"); + } + } + FileUtil.zip(Arrays.asList(tmp1, tmp2, tmp3), zip1); + assertTrue(isClosed(tmp1)); + assertTrue(isClosed(tmp2)); + assertTrue(isClosed(tmp3)); + assertTrue(isClosed(zip1)); + assertTrue(tmp1.delete()); + assertTrue(tmp2.delete()); + assertTrue(tmp3.delete()); + assertTrue(zip1.delete()); + } + + // small tip to check the file is lock-free + // (this is just a tip. It seems very difficult to check a file is + // lockfree in a way that works in all the cases for any platform) + private boolean isClosed(File file) { + return file.renameTo(file); + } +} ------------------------------------------------------------------------------ Transform Data into Opportunity. Accelerate data analysis in your applications with Intel Data Analytics Acceleration Library. Click to learn more. http://pubads.g.doubleclick.net/gampad/clk?id=278785471&iu=/4140 _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel