Javadoc fixes
[jabaws.git] / engine / compbio / engine / FilePuller.java
index d040838..98a44bc 100644 (file)
@@ -32,284 +32,285 @@ import compbio.util.FileWatcher;
 \r
 public class FilePuller implements Delayed {\r
 \r
-    private static final Logger log = Logger.getLogger(FilePuller.class);\r
-\r
-    // 5 minutes in nanosec\r
-    private static long defaultDelay = 1000 * 1000 * 1000L * 5 * 60;\r
-    private final File file;\r
-    private long lastAccessTime;\r
-    private FileWatcher watcher;\r
-    final int chunkSize;\r
-\r
-    // used for testing only\r
-    long delay = 0;\r
-\r
-    private FilePuller(String file, int size) {\r
-       FileWatcher.validateInput(file, size);\r
-       if (compbio.util.Util.isEmpty(file)) {\r
-           throw new NullPointerException("File name must be provided!");\r
+       private static final Logger log = Logger.getLogger(FilePuller.class);\r
+\r
+       // 5 minutes in nanosec\r
+       private static long defaultDelay = 1000 * 1000 * 1000L * 5 * 60;\r
+       private final File file;\r
+       private long lastAccessTime;\r
+       private FileWatcher watcher;\r
+       final int chunkSize;\r
+\r
+       // used for testing only\r
+       long delay = 0;\r
+\r
+       private FilePuller(String file, int size) {\r
+               FileWatcher.validateInput(file, size);\r
+               if (compbio.util.Util.isEmpty(file)) {\r
+                       throw new NullPointerException("File name must be provided!");\r
+               }\r
+               // The fact that the file may not exist at a time does not matter here\r
+               if (!PathValidator.isAbsolutePath(file)) {\r
+                       throw new IllegalArgumentException("Absolute path to the File "\r
+                                       + file + " is expected but not provided!");\r
+               }\r
+               this.file = new File(file);\r
+               this.chunkSize = size;\r
+               this.lastAccessTime = System.nanoTime();\r
        }\r
-       // The fact that the file may not exist at a time does not matter here\r
-       if (!PathValidator.isAbsolutePath(file)) {\r
-           throw new IllegalArgumentException("Absolute path to the File "\r
-                   + file + " is expected but not provided!");\r
+\r
+       private FilePuller(String file) {\r
+               if (compbio.util.Util.isEmpty(file)) {\r
+                       throw new NullPointerException("File name must be provided!");\r
+               }\r
+               // The fact that the file may not exist at a time does not matter here\r
+               if (!PathValidator.isAbsolutePath(file)) {\r
+                       throw new IllegalArgumentException("Absolute path to the File "\r
+                                       + file + " is expected but not provided!");\r
+               }\r
+               this.file = new File(file);\r
+               this.chunkSize = 3;\r
+               this.lastAccessTime = System.nanoTime();\r
        }\r
-       this.file = new File(file);\r
-       this.chunkSize = size;\r
-       this.lastAccessTime = System.nanoTime();\r
-    }\r
-\r
-    private FilePuller(String file) {\r
-       if (compbio.util.Util.isEmpty(file)) {\r
-           throw new NullPointerException("File name must be provided!");\r
+\r
+       public static FilePuller newFilePuller(String file, int chunkSize) {\r
+               return new FilePuller(file, chunkSize);\r
        }\r
-       // The fact that the file may not exist at a time does not matter here\r
-       if (!PathValidator.isAbsolutePath(file)) {\r
-           throw new IllegalArgumentException("Absolute path to the File "\r
-                   + file + " is expected but not provided!");\r
+\r
+       /**\r
+        * Progress Puller is designed to read 3 characters from the beginning of\r
+        * the file, nothing more. Intended to be used in conjunction with a tool\r
+        * which output progress as a percent value from 0 to 100. In any cases\r
+        * progress could not be more than the largest byte value 255.\r
+        * \r
+        * @param file\r
+        * @return instance\r
+        */\r
+       public static FilePuller newProgressPuller(String file) {\r
+               return new FilePuller(file);\r
        }\r
-       this.file = new File(file);\r
-       this.chunkSize = 3;\r
-       this.lastAccessTime = System.nanoTime();\r
-    }\r
-\r
-    public static FilePuller newFilePuller(String file, int chunkSize) {\r
-       return new FilePuller(file, chunkSize);\r
-    }\r
-\r
-    /**\r
-     * Progress Puller is designed to read 3 characters from the beginning of\r
-     * the file, nothing more. Intended to be used in conjunction with a tool\r
-     * which output progress as a percent value from 0 to 100. In any cases\r
-     * progress could not be more than the largest byte value 255.\r
-     * \r
-     * @param file\r
-     * @return\r
-     */\r
-    public static FilePuller newProgressPuller(String file) {\r
-       return new FilePuller(file);\r
-    }\r
-\r
-    public ChunkHolder pull(long position) throws IOException {\r
-       initPull();\r
-       String valueUTF16 = watcher.pull(position);\r
-       String value = null;\r
-       if (valueUTF16 != null) {\r
-           value = removeInvalidXMLCharacters(valueUTF16);\r
+\r
+       public ChunkHolder pull(long position) throws IOException {\r
+               initPull();\r
+               String valueUTF16 = watcher.pull(position);\r
+               String value = null;\r
+               if (valueUTF16 != null) {\r
+                       value = removeInvalidXMLCharacters(valueUTF16);\r
+               }\r
+               return new ChunkHolder(value, watcher.getCursorPosition());\r
        }\r
-       return new ChunkHolder(value, watcher.getCursorPosition());\r
-    }\r
-\r
-    /**\r
-     * This method ensures that the output String has only valid XML unicode\r
-     * characters as specified by the XML 1.0 standard. For reference, please\r
-     * see the standard.\r
-     * \r
-     * @param The\r
-     *            String whose non-valid characters we want to remove.\r
-     * \r
-     * @return The in String, stripped of non-valid characters.\r
-     */\r
-    static String removeInvalidXMLCharacters(String str) {\r
-       assert str != null;\r
-\r
-       StringBuilder out = new StringBuilder(); // Used to hold the output.\r
-       int codePoint; // Used to reference the current character.\r
-\r
-       // For test\r
-       // String ss = "\ud801\udc00"; // This is actualy one unicode character,\r
-       // represented by two code units!!!.\r
-       // System.out.println(ss.codePointCount(0, ss.length()));// See: 1\r
-       int i = 0;\r
-       String value = null;\r
-       try {\r
-           // make sure the string contain only UTF-8 characters\r
-           value = new String(str.getBytes("UTF-8"), "UTF-8");\r
-       } catch (UnsupportedEncodingException e) {\r
-           // will not happen\r
-           throw new AssertionError("UTF-8 charset is not supported!!!");\r
+\r
+       /**\r
+        * This method ensures that the output String has only valid XML unicode\r
+        * characters as specified by the XML 1.0 standard. For reference, please\r
+        * see the standard.\r
+        * \r
+        * @param The\r
+        *            String whose non-valid characters we want to remove.\r
+        * \r
+        * @return The in String, stripped of non-valid characters.\r
+        */\r
+       static String removeInvalidXMLCharacters(String str) {\r
+               assert str != null;\r
+\r
+               StringBuilder out = new StringBuilder(); // Used to hold the output.\r
+               int codePoint; // Used to reference the current character.\r
+\r
+               // For test\r
+               // String ss = "\ud801\udc00"; // This is actualy one unicode character,\r
+               // represented by two code units!!!.\r
+               // System.out.println(ss.codePointCount(0, ss.length()));// See: 1\r
+               int i = 0;\r
+               String value = null;\r
+               try {\r
+                       // make sure the string contain only UTF-8 characters\r
+                       value = new String(str.getBytes("UTF-8"), "UTF-8");\r
+               } catch (UnsupportedEncodingException e) {\r
+                       // will not happen\r
+                       throw new AssertionError("UTF-8 charset is not supported!!!");\r
+               }\r
+               while (i < value.length()) {\r
+                       codePoint = value.codePointAt(i); // This is the unicode code of the\r
+                       // character.\r
+                       if ((codePoint == 0x9)\r
+                                       || // Consider testing larger ranges first to\r
+                                               // improve speed.\r
+                                       (codePoint == 0xA) || (codePoint == 0xD)\r
+                                       || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))\r
+                                       || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))\r
+                                       || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {\r
+\r
+                               out.append(Character.toChars(codePoint));\r
+                       }\r
+\r
+                       i += Character.charCount(codePoint);\r
+                       /*\r
+                        * Increment with the number of code units(java chars) needed to\r
+                        * represent a Unicode char.\r
+                        */\r
+               }\r
+               return out.toString();\r
        }\r
-       while (i < value.length()) {\r
-           codePoint = value.codePointAt(i); // This is the unicode code of the\r
-           // character.\r
-           if ((codePoint == 0x9)\r
-                   || // Consider testing larger ranges first to\r
-                   // improve speed.\r
-                   (codePoint == 0xA) || (codePoint == 0xD)\r
-                   || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))\r
-                   || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))\r
-                   || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {\r
-\r
-               out.append(Character.toChars(codePoint));\r
-           }\r
-\r
-           i += Character.charCount(codePoint);\r
-           /*\r
-            * Increment with the number of code units(java chars) needed to\r
-            * represent a Unicode char.\r
-            */\r
+\r
+       public void initPull() {\r
+               this.lastAccessTime = System.nanoTime();\r
+               if (!isFileCreated()) {\r
+                       throw new IllegalStateException("File " + file.getAbsolutePath()\r
+                                       + " has not been created yet! Cannot pull.");\r
+               }\r
+               if (watcher == null) {\r
+                       init();\r
+               }\r
        }\r
-       return out.toString();\r
-    }\r
-\r
-    public void initPull() {\r
-       this.lastAccessTime = System.nanoTime();\r
-       if (!isFileCreated()) {\r
-           throw new IllegalStateException("File " + file.getAbsolutePath()\r
-                   + " has not been created yet! Cannot pull.");\r
+\r
+       public String getFile() {\r
+               return file.getAbsolutePath();\r
        }\r
-       if (watcher == null) {\r
-           init();\r
+\r
+       public boolean isFileCreated() {\r
+               this.lastAccessTime = System.nanoTime();\r
+               return file.exists();\r
        }\r
-    }\r
-\r
-    public String getFile() {\r
-       return file.getAbsolutePath();\r
-    }\r
-\r
-    public boolean isFileCreated() {\r
-       this.lastAccessTime = System.nanoTime();\r
-       return file.exists();\r
-    }\r
-\r
-    public void waitForFile(long maxWaitSeconds) {\r
-       long waited = 0;\r
-       int step = 500;\r
-       while (true) {\r
-           if (isFileCreated()) {\r
-               break;\r
-           }\r
-           try {\r
-               Thread.sleep(step);\r
-               // TODO is this needed? this.lastAccessTime = System.nanoTime();\r
-           } catch (InterruptedException e) {\r
-               // Propagate interruption up the stack trace for anyone\r
-               // interested\r
-               Thread.currentThread().interrupt();\r
-               log.debug("Thread interruped during waiting for file "\r
-                       + file.getAbsolutePath() + " to be created. Message: "\r
-                       + e.getMessage());\r
-               break;\r
-           }\r
-           waited += step;\r
-           if (waited / 1000 >= maxWaitSeconds) {\r
-               break;\r
-           }\r
+\r
+       public void waitForFile(long maxWaitSeconds) {\r
+               long waited = 0;\r
+               int step = 500;\r
+               while (true) {\r
+                       if (isFileCreated()) {\r
+                               break;\r
+                       }\r
+                       try {\r
+                               Thread.sleep(step);\r
+                               // TODO is this needed? this.lastAccessTime = System.nanoTime();\r
+                       } catch (InterruptedException e) {\r
+                               // Propagate interruption up the stack trace for anyone\r
+                               // interested\r
+                               Thread.currentThread().interrupt();\r
+                               log.debug("Thread interruped during waiting for file "\r
+                                               + file.getAbsolutePath() + " to be created. Message: "\r
+                                               + e.getMessage());\r
+                               break;\r
+                       }\r
+                       waited += step;\r
+                       if (waited / 1000 >= maxWaitSeconds) {\r
+                               break;\r
+                       }\r
+               }\r
        }\r
-    }\r
 \r
-    public boolean hasMoreData() throws IOException {\r
-       this.lastAccessTime = System.nanoTime();\r
-       if (!isFileCreated()) {\r
-           throw new IllegalStateException("File " + file.getAbsolutePath()\r
-                   + " has not been created yet! Cannot pull.");\r
+       public boolean hasMoreData() throws IOException {\r
+               this.lastAccessTime = System.nanoTime();\r
+               if (!isFileCreated()) {\r
+                       throw new IllegalStateException("File " + file.getAbsolutePath()\r
+                                       + " has not been created yet! Cannot pull.");\r
+               }\r
+               if (watcher == null) {\r
+                       init();\r
+               }\r
+               return watcher.hasMore();\r
        }\r
-       if (watcher == null) {\r
-           init();\r
+\r
+       private synchronized void init() {\r
+               // Check watcher==null is duplicated to avoid adding synchronization to\r
+               // access methods\r
+               if (watcher == null) {\r
+                       if (chunkSize < FileWatcher.MIN_CHUNK_SIZE_BYTES) {\r
+                               watcher = FileWatcher\r
+                                               .newProgressWatcher(file.getAbsolutePath());\r
+                               log.debug("Init Progress watcher with file: "\r
+                                               + file.getAbsolutePath());\r
+                       } else {\r
+                               watcher = FileWatcher.newFileWatcher(file.getAbsolutePath(),\r
+                                               chunkSize);\r
+                               log.debug("Init File watcher with file: "\r
+                                               + file.getAbsolutePath());\r
+                       }\r
+\r
+               }\r
        }\r
-       return watcher.hasMore();\r
-    }\r
-\r
-    private synchronized void init() {\r
-       // Check watcher==null is duplicated to avoid adding synchronization to\r
-       // access methods\r
-       if (watcher == null) {\r
-           if (chunkSize < FileWatcher.MIN_CHUNK_SIZE_BYTES) {\r
-               watcher = FileWatcher\r
-                       .newProgressWatcher(file.getAbsolutePath());\r
-               log.debug("Init Progress watcher with file: "\r
-                       + file.getAbsolutePath());\r
-           } else {\r
-               watcher = FileWatcher.newFileWatcher(file.getAbsolutePath(),\r
-                       chunkSize);\r
-               log.debug("Init File watcher with file: "\r
-                       + file.getAbsolutePath());\r
-           }\r
 \r
+       @Override\r
+       public int compareTo(Delayed o) {\r
+               return new Long(this.getDelay(TimeUnit.NANOSECONDS)).compareTo(o\r
+                               .getDelay(TimeUnit.NANOSECONDS));\r
        }\r
-    }\r
-\r
-    @Override\r
-    public int compareTo(Delayed o) {\r
-       return new Long(this.getDelay(TimeUnit.NANOSECONDS)).compareTo(o\r
-               .getDelay(TimeUnit.NANOSECONDS));\r
-    }\r
-\r
-    /*\r
-     * This must return remaining delay associated with the object!\r
-     * (non-Javadoc)\r
-     * \r
-     * @see java.util.concurrent.Delayed#getDelay(java.util.concurrent.TimeUnit)\r
-     */\r
-    @Override\r
-    public long getDelay(final TimeUnit unit) {\r
-       long idleTime = System.nanoTime() - lastAccessTime;\r
-       long delayVal = (delay == 0 ? defaultDelay : delay);\r
-       return unit.convert(delayVal - idleTime, TimeUnit.NANOSECONDS);\r
-    }\r
-\r
-    void setDelay(long delay, TimeUnit unit) {\r
-       assert delay > 0;\r
-       this.delay = TimeUnit.NANOSECONDS.convert(delay, unit);\r
-       assert delay < defaultDelay;\r
-    }\r
-\r
-    long getDelayValue(TimeUnit unit) {\r
-       return (delay == 0 ? unit.convert(defaultDelay, TimeUnit.NANOSECONDS)\r
-               : unit.convert(delay, TimeUnit.NANOSECONDS));\r
-    }\r
-\r
-    public void disconnect() {\r
-       synchronized (this) {\r
-           if (watcher != null) {\r
-               watcher.disconnect();\r
-               watcher = null;\r
-           }\r
+\r
+       /*\r
+        * This must return remaining delay associated with the object!\r
+        * (non-Javadoc)\r
+        * \r
+        * @see java.util.concurrent.Delayed#getDelay(java.util.concurrent.TimeUnit)\r
+        */\r
+       @Override\r
+       public long getDelay(final TimeUnit unit) {\r
+               long idleTime = System.nanoTime() - lastAccessTime;\r
+               long delayVal = (delay == 0 ? defaultDelay : delay);\r
+               return unit.convert(delayVal - idleTime, TimeUnit.NANOSECONDS);\r
        }\r
-    }\r
-\r
-    boolean disconnected() {\r
-       return watcher == null;\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-       String value = "File: " + this.file.getAbsolutePath() + "\n";\r
-       value += "Delay (s): " + getDelayValue(TimeUnit.SECONDS) + "\n";\r
-       long exp = getDelay(TimeUnit.MILLISECONDS);\r
-       if (exp > 0) {\r
-           value += "Expire in (ms): " + exp + "\n";\r
-       } else {\r
-           value += "This object has expired" + "\n";\r
+\r
+       void setDelay(long delay, TimeUnit unit) {\r
+               assert delay > 0;\r
+               this.delay = TimeUnit.NANOSECONDS.convert(delay, unit);\r
+               assert delay < defaultDelay;\r
        }\r
-       value += "ChunkSize  " + this.chunkSize + "\n";\r
-       return value;\r
-    }\r
-\r
-    @Override\r
-    public boolean equals(Object obj) {\r
-       if (obj == null) {\r
-           return false;\r
+\r
+       long getDelayValue(TimeUnit unit) {\r
+               return (delay == 0\r
+                               ? unit.convert(defaultDelay, TimeUnit.NANOSECONDS)\r
+                               : unit.convert(delay, TimeUnit.NANOSECONDS));\r
+       }\r
+\r
+       public void disconnect() {\r
+               synchronized (this) {\r
+                       if (watcher != null) {\r
+                               watcher.disconnect();\r
+                               watcher = null;\r
+                       }\r
+               }\r
        }\r
-       if (!(obj instanceof FilePuller)) {\r
-           return false;\r
+\r
+       boolean disconnected() {\r
+               return watcher == null;\r
+       }\r
+\r
+       @Override\r
+       public String toString() {\r
+               String value = "File: " + this.file.getAbsolutePath() + "\n";\r
+               value += "Delay (s): " + getDelayValue(TimeUnit.SECONDS) + "\n";\r
+               long exp = getDelay(TimeUnit.MILLISECONDS);\r
+               if (exp > 0) {\r
+                       value += "Expire in (ms): " + exp + "\n";\r
+               } else {\r
+                       value += "This object has expired" + "\n";\r
+               }\r
+               value += "ChunkSize  " + this.chunkSize + "\n";\r
+               return value;\r
        }\r
-       FilePuller fp = (FilePuller) obj;\r
-       if (!this.file.equals(fp.file)) {\r
-           return false;\r
+\r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (obj == null) {\r
+                       return false;\r
+               }\r
+               if (!(obj instanceof FilePuller)) {\r
+                       return false;\r
+               }\r
+               FilePuller fp = (FilePuller) obj;\r
+               if (!this.file.equals(fp.file)) {\r
+                       return false;\r
+               }\r
+               // Other fields does not matter\r
+               return true;\r
+       }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               return this.file.hashCode();\r
+       }\r
+\r
+       public byte getProgress() throws IOException {\r
+               initPull();\r
+               return watcher.getProgress();\r
        }\r
-       // Other fields does not matter\r
-       return true;\r
-    }\r
-\r
-    @Override\r
-    public int hashCode() {\r
-       return this.file.hashCode();\r
-    }\r
-\r
-    public byte getProgress() throws IOException {\r
-       initPull();\r
-       return watcher.getProgress();\r
-    }\r
 \r
 }\r