Ranides Atterwim 9 gadi atpakaļ
vecāks
revīzija
e7cfcdc8f2

+ 30 - 73
assira/src/main/java/net/ranides/assira/io/uri/URIHandle.java

@@ -23,13 +23,7 @@ import net.ranides.assira.collection.query.CQuery;
  *
  * @author Ranides Atterwim <ranides@gmail.com>
  */
-public abstract class URIHandle {
-    
-    private transient final URIHandler system;
-
-    protected URIHandle(URIHandler handler) {
-        this.system = handler;
-    }
+public interface URIHandle {
     
     public static URIHandle resolve(String location) throws IOException {
         return URIResolver.DEFAULT.resolve(location);
@@ -43,92 +37,55 @@ public abstract class URIHandle {
         return URIResolver.DEFAULT.resolve(location);
     }
     
-    public URI uri() throws IOException {
-        return system.uri(this);
-    }
+    public URIResolver resolver();
     
-    public File file() throws IOException {
-        return system.file(this);
-    }
+    public URI uri() throws IOException;
     
-    public Path path() throws IOException {
-        return system.path(this);
-    }
+    public String scheme() throws IOException;
     
-    public Charset charset() throws IOException {
-        return system.charset(this);
-    }
+    public File file() throws IOException;
     
-    public Set<URIFlags> flags() throws IOException {
-        return system.flags(this);
-    }
+    public Path path() throws IOException;
     
-    public boolean exists() throws IOException {
-        return URIFlags.EXISTS.matches(this);
-    }
+    public Charset charset() throws IOException;
     
-    public Instant time(URITime ut) throws IOException {
-        return system.time(this, ut);
-    }
+    public Set<URIFlags> flags() throws IOException;
     
-    public long size() throws IOException {
-        return system.size(this);
-    }
+    public boolean exists() throws IOException;
     
-    public URIHandle parent() throws IOException {
-        return system.parent(this);
-    }
+    public Instant time(URITime ut) throws IOException;
     
-    public CQuery<URIHandle> scan() throws IOException {
-        return system.scan(this);
-    }
+    public long size() throws IOException;
     
-    public InputStream istream() throws IOException {
-        return system.istream(this);
-    }
+    public URIHandle parent() throws IOException;
     
-    public InputStream istream(long offset, long size) throws IOException {
-        return system.istream(this, offset, size);
-    }
+    public CQuery<URIHandle> scan() throws IOException;
     
-    public OutputStream ostream() throws IOException {
-        return system.ostream(this);
-    }
+    public InputStream istream() throws IOException;
     
-    public Reader reader() throws IOException {
-        return system.reader(this);
-    }
+    public InputStream istream(long offset, long size) throws IOException;
     
-    public Reader reader(Charset cs) throws IOException {
-        return system.reader(this, cs);
-    }
+    public OutputStream ostream() throws IOException;
     
-    public Writer writer() throws IOException {
-        return system.writer(this);
-    }
+    public Reader reader() throws IOException;
     
-    public Writer writer(Charset cs) throws IOException {
-        return system.writer(this, cs);
-    }
+    public Reader reader(Charset cs) throws IOException;
     
-    public void delete() throws IOException {
-        system.delete(this);
-    }
+    public Writer writer() throws IOException;
     
-    public void create() throws IOException {
-        system.create(this);
-    }
+    public Writer writer(Charset cs) throws IOException;
     
-    public void move(String location) throws IOException {
-        move(URIHandle.resolve(location));
-    }
+    public void delete() throws IOException;
     
-    public void move(URIHandle location) throws IOException {
-        system.move(this, location);
-    }
+    public void create() throws IOException;
     
-    public void copy(URIHandle location) throws IOException {
-        system.copy(this, location);
-    }
+    public void move(String target) throws IOException;
+//        move(URIHandle.resolve(location));
+    
+    public void move(URIHandle target) throws IOException;
+    
+    public void copy(String target) throws IOException;
+    
+    public void copy(URIHandle target) throws IOException;
     
 }

+ 2 - 61
assira/src/main/java/net/ranides/assira/io/uri/URIHandler.java

@@ -6,18 +6,9 @@
  */
 package net.ranides.assira.io.uri;
 
-import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
 import java.net.URI;
-import java.nio.charset.Charset;
-import java.nio.file.Path;
-import java.time.Instant;
-import java.util.Set;
-import net.ranides.assira.collection.query.CQuery;
+import java.util.Collection;
 
 /**
  *
@@ -27,56 +18,6 @@ public interface URIHandler {
     
     URIHandle resolve(URI uri) throws IOException;
     
-    String scheme();
+    Collection<String> schemes();
     
-    URI uri(URIHandle ustream) throws IOException;
-    
-    File file(URIHandle ustream) throws IOException;
-    
-    Path path(URIHandle ustream) throws IOException;
-    
-    Charset charset(URIHandle ustream) throws IOException;
-    
-    Set<URIFlags> flags(URIHandle ustream) throws IOException;
-    
-    default boolean exists(URIHandle ustream) throws IOException {
-        return URIFlags.EXISTS.matches(ustream);
-    }
-    
-    Instant time(URIHandle ustream, URITime ut) throws IOException;
-    
-    long size(URIHandle ustream) throws IOException;
-    
-    URIHandle parent(URIHandle ustream) throws IOException;
-    
-    CQuery<URIHandle> scan(URIHandle ustream) throws IOException;
-
-    
-    InputStream istream(URIHandle ustream) throws IOException;
-    
-    InputStream istream(URIHandle ustream, long begin, long end) throws IOException;
-    
-    OutputStream ostream(URIHandle ustream) throws IOException;
-    
-    default Reader reader(URIHandle ustream) throws IOException {
-        return reader(ustream, ustream.charset());
-    }
-    
-    Reader reader(URIHandle ustream, Charset cs) throws IOException;
-    
-    default Writer writer(URIHandle ustream) throws IOException {
-        return writer(ustream, ustream.charset());
-    }
-    
-    Writer writer(URIHandle ustream, Charset cs) throws IOException;
-    
-    
-    void delete(URIHandle ustream) throws IOException;
-    
-    void create(URIHandle ustream) throws IOException;
-    
-    void move(URIHandle ustream, URIHandle location) throws IOException;
-    
-    void copy(URIHandle ustream, URIHandle location) throws IOException;
-        
 }

+ 3 - 1
assira/src/main/java/net/ranides/assira/io/uri/URIResolver.java

@@ -58,7 +58,9 @@ public final class URIResolver {
     }
     
     public URIResolver register(URIHandler handler) {
-        handlers.put(handler.scheme(), handler);
+        for(String s : handler.schemes()) {
+            handlers.put(s, handler);
+        }
         return this;
     }
 

+ 18 - 21
assira/src/main/java/net/ranides/assira/io/uri/impl/CFileHandler.java

@@ -17,6 +17,8 @@ import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.time.Instant;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Optional;
 import java.util.Set;
 import net.ranides.assira.collection.query.CQuery;
@@ -24,44 +26,39 @@ import net.ranides.assira.collection.query.CQueryBuilder;
 import net.ranides.assira.io.uri.URITime;
 import net.ranides.assira.io.uri.URIFlags;
 import net.ranides.assira.io.uri.URIHandle;
+import net.ranides.assira.io.uri.URIHandler;
+import net.ranides.assira.io.uri.URIResolver;
 import net.ranides.assira.io.uri.URIUtils;
 
 /**
  *
  * @author Ranides Atterwim <ranides@gmail.com>
  */
-public class CFileHandler extends CHandler {
+public class CFileHandler implements URIHandler {
     
-    final static CFileHandler THAT = new CFileHandler();
-
-    @Override
-    public URIHandle resolve(URI uri) {
-        return new CFileHandle(uri);
-    }
-
-    @Override
-    public String scheme() {
-        return "file";
+    private final URIResolver resolver;
+    
+    public CFileHandler() {
+        this.resolver = URIResolver.DEFAULT;
     }
 
-    @Override
-    public URI uri(URIHandle ustream) {
-        return ((CFileHandle)ustream).uri;
+    public CFileHandler(URIResolver resolver) {
+        this.resolver = resolver;
     }
 
     @Override
-    public File file(URIHandle ustream) {
-        return ((CFileHandle)ustream).file;
+    public void bind(URIResolver resolver) {
+        this.resolver = resolver;
     }
 
     @Override
-    public Path path(URIHandle ustream) {
-        return ((CFileHandle)ustream).path;
+    public URIHandle resolve(URI uri) {
+        return new CFileHandle(uri);
     }
 
     @Override
-    public Charset charset(URIHandle ustream) {
-        return ((CFileHandle)ustream).charset;
+    public Collection<String> schemes() {
+        return Arrays.asList("file");
     }
 
     @Override
@@ -160,7 +157,7 @@ public class CFileHandler extends CHandler {
         }
     }
     
-    private static class CFileHandle extends URIHandle {
+    private class CFileHandle extends CHandle {
         
         public final URI uri;
         public final File file;

+ 49 - 31
assira/src/main/java/net/ranides/assira/io/uri/impl/CHandler.java

@@ -31,111 +31,129 @@ import net.ranides.assira.text.Charsets;
  *
  * @author Ranides Atterwim <ranides@gmail.com>
  */
-public abstract class CHandler implements URIHandler {
+public abstract class CHandle implements URIHandle {
     
     private static final String NSE = "Not supported by URIHandler: ";
+
+    @Override
+    public String scheme() throws IOException {
+        return uri().getScheme();
+    }
     
     @Override
-    public URI uri(URIHandle ustream) throws IOException {
+    public URI uri() throws IOException {
         throw new IOException(NSE + scheme());
     }
-
+    
     @Override
-    public File file(URIHandle ustream) throws IOException {
+    public File file() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public Path path(URIHandle ustream) throws IOException {
+    public Path path() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public Charset charset(URIHandle ustream) throws IOException {
+    public Charset charset() throws IOException {
         return Charsets.UTF8;
     }
-    
 
     @Override
-    public Set<URIFlags> flags(URIHandle ustream) throws IOException {
+    public Set<URIFlags> flags() throws IOException {
         return URIFlags.collect();
-        
     }
 
     @Override
-    public Instant time(URIHandle ustream, URITime ut) throws IOException {
+    public boolean exists() throws IOException {
+        return URIFlags.EXISTS.matches(this);
+    }
+    
+    @Override
+    public Instant time(URITime ut) throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public long size(URIHandle ustream) throws IOException {
+    public long size() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public URIHandle parent(URIHandle ustream) throws IOException {
+    public URIHandle parent() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public CQuery<URIHandle> scan(URIHandle ustream) throws IOException {
+    public CQuery<URIHandle> scan() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public InputStream istream(URIHandle ustream) throws IOException {
+    public InputStream istream() throws IOException {
         throw new IOException(NSE + scheme());
     }
     
     @Override
-    public InputStream istream(URIHandle ustream, long begin, long end) throws IOException {
-        return IOStreams.limit(istream(ustream), begin, end);
+    public InputStream istream(long begin, long end) throws IOException {
+        return IOStreams.limit(istream(), begin, end);
     }
 
     @Override
-    public OutputStream ostream(URIHandle ustream) throws IOException {
+    public OutputStream ostream() throws IOException {
         throw new IOException(NSE + scheme());
     }
     
     @Override
-    public Reader reader(URIHandle ustream, Charset cs) throws IOException {
-        return new InputStreamReader(istream(ustream), cs);
+    public Reader reader(Charset cs) throws IOException {
+        return new InputStreamReader(istream(), cs);
     }
 
     @Override
-    public Reader reader(URIHandle ustream) throws IOException {
-        return new InputStreamReader(istream(ustream), charset(ustream));
+    public Reader reader() throws IOException {
+        return new InputStreamReader(istream(), charset());
     }
 
     @Override
-    public Writer writer(URIHandle ustream, Charset cs) throws IOException {
-        return new OutputStreamWriter(ostream(ustream), cs);
+    public Writer writer(Charset cs) throws IOException {
+        return new OutputStreamWriter(ostream(), cs);
     }
     
     @Override
-    public Writer writer(URIHandle ustream) throws IOException {
-        return new OutputStreamWriter(ostream(ustream), charset(ustream));
+    public Writer writer() throws IOException {
+        return new OutputStreamWriter(ostream(), charset());
     }
 
     @Override
-    public void delete(URIHandle ustream) throws IOException {
+    public void delete() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public void create(URIHandle ustream) throws IOException {
+    public void create() throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public void move(URIHandle ustream, URIHandle location) throws IOException {
+    public void move(String target) throws IOException {
+        move(resolver().resolve(target));
+    }
+    
+    @Override
+    public void move(URIHandle target) throws IOException {
         throw new IOException(NSE + scheme());
     }
 
     @Override
-    public void copy(URIHandle ustream, URIHandle location) throws IOException {
-        try(InputStream istream = ustream.istream()) {
-            try(OutputStream ostream = location.ostream()) {
+    public void copy(String target) throws IOException {
+        copy(resolver().resolve(target));
+    }
+    
+    @Override
+    public void copy(URIHandle target) throws IOException {
+        try(InputStream istream = istream()) {
+            try(OutputStream ostream = target.ostream()) {
                 IOStreams.copy(istream, ostream);
             }
         } 

+ 1 - 1
assira/src/main/java/net/ranides/assira/io/uri/impl/CHttpHandler.java

@@ -36,7 +36,7 @@ import org.slf4j.Logger;
  *
  * @author Ranides Atterwim <ranides@gmail.com>
  */
-public class CHttpHandler extends CHandler {
+public class CHttpHandler extends CHandle {
     
     private static final Logger LOGGER = LoggerUtils.getLogger();