Просмотр исходного кода

IOEvent - nowy idiom na zdarzenia wymyślony :)

Ranides Atterwim 13 лет назад
Родитель
Сommit
638cbd728c

+ 4 - 4
src/main/java/net/ranides/assira/io/FileHelper.java

@@ -234,7 +234,7 @@ public final class FileHelper {
     }
 
 
-    private static final class TempService { // NOPMD
+    private static final class Li { // NOPMD - lazy init idiom
 
         private static final File LOCATION =  new File(PrivilegedActions.getProperty("java.io.tmpdir"));
 
@@ -262,7 +262,7 @@ public final class FileHelper {
      */
     public static File createTempFile(String prefix, String suffix, Class<?> clazz) throws IOException {
         final String dir = PathHelper.getClassDir(clazz);
-        final File file = TempService.location(dir);
+        final File file = Li.location(dir);
         if(!file.exists() && !file.mkdirs()) {
             throw new IOException("can't create directory: " + file);
         }
@@ -285,14 +285,14 @@ public final class FileHelper {
      */
     public static File createTempFile(String suffix, Class<?> clazz) throws IOException {
         final String dir = PathHelper.getClassDir(clazz);
-        final File file = TempService.location(dir);
+        final File file = Li.location(dir);
         if(!file.exists() && !file.mkdirs()) {
             throw new IOException("can't create directory: " + file);
         }
         if(!file.isDirectory()) {
             throw new IOException("path is not a directory: " + file);
         }
-        return new File(TempService.path() + File.separator +dir + File.separator + clazz.getSimpleName() + ValueUtils.or(suffix,".tmp"));
+        return new File(Li.path() + File.separator +dir + File.separator + clazz.getSimpleName() + ValueUtils.or(suffix,".tmp"));
     }
 
 }

+ 150 - 0
src/main/java/net/ranides/assira/io/IOEvent.java

@@ -0,0 +1,150 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.io;
+
+import edu.umd.cs.findbugs.annotations.SuppressWarnings;
+import java.io.IOException;
+import java.util.Arrays;
+import net.ranides.assira.events.Event;
+
+/**
+ *
+ * @author ranides
+ */
+@SuppressWarnings({
+    "EI_EXPOSE_REP", 
+    "PMD"
+})
+public abstract class IOEvent implements Event {
+    
+    private static final class Li { // NOPMD lazy init idiom
+        static final Close CLOSE = new Close();
+        static final Flush FLUSH = new Flush();
+        static final Open OPEN = new Open();
+    }
+    
+    public static Close close() {
+        return Li.CLOSE;
+    }
+    
+    public static Flush flush() {
+        return Li.FLUSH;
+    }
+    
+    public static Open open() {
+        return Li.OPEN;
+    }
+    
+    public static Failure failure(IOException cause) {
+        return new Failure(cause);
+    }
+    
+    public static ReadByte read(int value) {
+        return new ReadByte(value);
+    }
+    
+    public static ReadByteArray read(byte[] array) {
+        return new ReadByteArray(Arrays.copyOf(array, array.length));
+    }
+    
+    public static ReadByteArray read(byte[] array, int offset, int length) {
+        return new ReadByteArray(Arrays.copyOfRange(array, offset, offset+length));
+    }
+    
+    public static WriteByte write(int value) {
+        return new WriteByte(value);
+    }
+    
+    public static WriteByteArray write(byte[] array) {
+        return new WriteByteArray(Arrays.copyOf(array, array.length));
+    }
+    
+    public static WriteByteArray write(byte[] array, int offset, int length) {
+        return new WriteByteArray(Arrays.copyOfRange(array, offset, offset+length));
+    }
+
+    
+    public static class Failure extends IOEvent {
+        
+        private final IOException cause;
+
+        protected Failure(IOException cause) {
+            this.cause = cause;
+        }
+
+        public IOException cause() {
+            return cause;
+        }
+        
+    }
+    
+    public static class Close extends IOEvent {
+        protected Close() { }
+    }
+    
+    public static class Flush extends IOEvent {
+        protected Flush() { }
+    }
+    
+    public static class Open extends IOEvent {
+        protected Open() { }
+    }
+    
+    public static class Read extends IOEvent {
+        protected Read() { }
+    }
+    
+    public static class ReadByte extends Read {
+        private final int data;
+
+        protected ReadByte(int data) {
+            this.data = data;
+        }
+        public final int data() {
+            return data;
+        }
+    }
+
+    @SuppressWarnings({"EI_EXPOSE_REP", "PMD"})
+    public static class ReadByteArray extends Read {
+        private final byte[] data;
+
+        protected ReadByteArray(byte[] data) {
+            this.data = data;
+        }
+        public final byte[] data() {
+            return data;
+        }
+    }
+    
+    public static class Write extends IOEvent {
+        protected Write() { }
+    }
+    
+    public static class WriteByte extends Write {
+        private final int data;
+
+        protected WriteByte(int data) {
+            this.data = data;
+        }
+        public final int data() {
+            return data;
+        }
+    }
+    
+    public static class WriteByteArray extends Write {
+        private final byte[] data;
+
+        protected WriteByteArray(byte[] data) {
+            this.data = data;
+        }
+        public final byte[] data() {
+            return data;
+        }
+    }
+    
+}

+ 12 - 80
src/main/java/net/ranides/assira/io/OutputStreamThread.java

@@ -9,7 +9,6 @@ package net.ranides.assira.io;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.Arrays;
 import net.ranides.assira.annotations.Meta;
 import net.ranides.assira.events.*;
 
@@ -37,28 +36,28 @@ public final class OutputStreamThread extends OutputStream {
     
     @Override
     public void write(int data) {
-        router.signalEvent(new IOWriteEvent(data));
+        router.signalEvent(IOEvent.write(data));
     }
 
     @Override
     public void write(byte[] data) {
-        router.signalEvent(new IOWriteArrayEvent(data));
+        router.signalEvent(IOEvent.write(data));
     }
 
     @Override
     public void write(byte[] data, int offset, int length) {
-        router.signalEvent(new IOWriteArrayEvent(data, offset, length));
+        router.signalEvent(IOEvent.write(data, offset, length));
     }
 
     @Override
     public void close() {
-        router.signalEvent(CLOSE);
+        router.signalEvent(IOEvent.close());
         router.dispose();
     }
 
     @Override
     public void flush() {
-        router.signalEvent(FLUSH);
+        router.signalEvent(IOEvent.flush());
     }
     
     /**
@@ -73,14 +72,14 @@ public final class OutputStreamThread extends OutputStream {
      * </p>
      * @param handler
      */
-    public void onError(EventListener<IOExceptionEvent> handler) {
-        listeners.addEventListener(IOExceptionEvent.class, handler);
+    public void onError(EventListener<IOEvent.Failure> handler) {
+        listeners.addEventListener(IOEvent.Failure.class, handler);
     }
     
 /* ************************************************************************** */
     
     @Meta.EventHandler
-    void handleEvent(IOCloseEvent event) {
+    void handleEvent(IOEvent.Close event) {
         try {
             ostream.close();
         } catch (IOException ex) {
@@ -89,7 +88,7 @@ public final class OutputStreamThread extends OutputStream {
     }
     
     @Meta.EventHandler
-    void handleEvent(IOFlushEvent event) {
+    void handleEvent(IOEvent.Flush event) {
         try {
             ostream.flush();
         } catch (IOException ex) {
@@ -98,7 +97,7 @@ public final class OutputStreamThread extends OutputStream {
     }
     
     @Meta.EventHandler
-    void handleEvent(IOWriteArrayEvent event) {
+    void handleEvent(IOEvent.WriteByteArray event) {
         try {
             ostream.write(event.data());
         } catch (IOException ex) {
@@ -107,7 +106,7 @@ public final class OutputStreamThread extends OutputStream {
     }
     
     @Meta.EventHandler
-    void handleEvent(IOWriteEvent event) {
+    void handleEvent(IOEvent.WriteByte event) {
         try {
             ostream.write(event.data());
         } catch (IOException ex) {
@@ -116,74 +115,7 @@ public final class OutputStreamThread extends OutputStream {
     }
     
     void handleError(IOException cause) {
-        listeners.signalEvent(new IOExceptionEvent(ostream, cause));
-    }
-    
-/* ************************************************************************** */
-    
-    
-    private static final IOCloseEvent CLOSE = new IOCloseEvent();
-    
-    private static final IOCloseEvent FLUSH = new IOCloseEvent();
-    
-    @SuppressWarnings("PMD")
-    private static class IOEvent implements Event { }
-    
-    private static class IOCloseEvent extends IOEvent { };
-    
-    private static class IOFlushEvent extends IOEvent { };
-    
-    private static class IOWriteArrayEvent extends IOEvent {
-        private final byte[] data;
-
-        public IOWriteArrayEvent(byte[] data) {
-            this.data = Arrays.copyOf(data, data.length);
-        }
-        public IOWriteArrayEvent(byte[] data, int offset, int length) {
-            this.data = Arrays.copyOfRange(data, offset, offset+length);
-        }
-        public byte[] data() {
-            return data; // NOPMD
-        }
-    }
-    
-    private static class IOWriteEvent extends IOEvent {
-        private final int data;
-
-        public IOWriteEvent(int data) {
-            this.data = data;
-        }
-        public int data() {
-            return data;
-        }
-    }
-    
-    /**
-     * Zdarzenie wysyłane po wystąpieniu wyjątku IOException do obserwatorów
-     * zarejestrowanych za pomocą metody {@link #onError}
-     */
-    public static class IOExceptionEvent extends IOEvent {
-        private final OutputStream stream;
-        private final IOException cause;
-
-        private IOExceptionEvent(OutputStream stream, IOException cause) {
-            this.stream = stream;
-            this.cause = cause;
-        }
-        /**
-         * Napotkany wyjątek
-         * @return
-         */
-        public IOException cause() {
-            return cause;
-        }
-        /**
-         * Strumień, który spowodował wyjątek
-         * @return
-         */
-        public OutputStream stream() {
-            return stream;
-        }
+        listeners.signalEvent(IOEvent.failure(cause));
     }
 
 }

+ 2 - 2
src/main/java/net/ranides/assira/text/AbstractStrBuilder.java

@@ -627,7 +627,7 @@ public abstract class AbstractStrBuilder<Self extends AbstractStrBuilder<Self>>
         return new StringBuilder(size).append(buffer, 0, size);
     }
 
-    private static final class LC { //NOPMD - lazy init idiom
+    private static final class Li { //NOPMD - lazy init idiom
         static final Constructor<String> NEW_STRING = ClassInspector.getConstructor(String.class, int.class, int.class, char[].class);
     }
     
@@ -648,7 +648,7 @@ public abstract class AbstractStrBuilder<Self extends AbstractStrBuilder<Self>>
             buffer = null;
             size = -1;
             nullText = null;
-            return ObjectInspector.createInstance(LC.NEW_STRING, 0, size, data);
+            return ObjectInspector.createInstance(Li.NEW_STRING, 0, size, data);
         } catch (InspectException ex) {
             throw new UnsupportedOperationException("StrBuilder.toSharedString is unsupported: " + ex.getMessage());
         }