abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1226027 - in /abdera/abdera2: common/src/main/java/org/apache/abdera2/common/io/ core/src/main/java/org/apache/abdera2/model/ core/src/main/java/org/apache/abdera2/parser/ core/src/main/java/org/apache/abdera2/parser/axiom/ core/src/main/j...
Date Sat, 31 Dec 2011 02:39:39 GMT
Author: jmsnell
Date: Sat Dec 31 02:39:39 2011
New Revision: 1226027

URL: http://svn.apache.org/viewvc?rev=1226027&view=rev
Log:
Immutable/Threadsafe treatment for WriterOptions too

Removed:
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWriterOptions.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractWriterOptions.java
Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/model/Base.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementWrapper.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/AbstractParser.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMDocument.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWriter.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/PrettyWriter.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractWriter.java
    abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/WriterOptions.java
    abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityWriter.java
    abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/json/JSONWriter.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/parser/EncodingTest.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java Sat
Dec 31 02:39:39 2011
@@ -79,6 +79,10 @@ public class Compression {
       checkArgument(exp, "At least one codec must be specified");
     }
     
+    public static OutputStream wrap(OutputStream out, Iterable<CompressionCodec> codecs)
throws IOException {
+      return wrap(out,Iterables.toArray(codecs,CompressionCodec.class));
+    }
+    
     public static OutputStream wrap(
         OutputStream out, 
         CompressionCodec... codecs)

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/Base.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/Base.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/Base.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/Base.java Sat Dec 31 02:39:39
2011
@@ -35,6 +35,8 @@ public interface Base extends Cloneable 
      * Get the default WriterOptions for this object
      */
     WriterOptions getDefaultWriterOptions();
+    
+    WriterOptions.Builder makeDefaultWriterOptions();
 
     /**
      * Serializes the model component out to the specified stream

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementWrapper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementWrapper.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementWrapper.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementWrapper.java Sat Dec
31 02:39:39 2011
@@ -293,6 +293,10 @@ public abstract class ElementWrapper imp
         internal.writeTo(out, options);
     }
 
+    public WriterOptions.Builder makeDefaultWriterOptions() {
+      return internal.makeDefaultWriterOptions();
+    }
+    
     public WriterOptions getDefaultWriterOptions() {
         return internal.getDefaultWriterOptions();
     }

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/AbstractParser.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/AbstractParser.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/AbstractParser.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/AbstractParser.java Sat Dec
31 02:39:39 2011
@@ -136,7 +136,7 @@ public abstract class AbstractParser imp
     protected abstract ParserOptions.Builder initDefaultParserOptions();
 
     public synchronized Parser setDefaultParserOptions(ParserOptions options) {
-      this.options = options;
+      this.options = options != null ? options : initDefaultParserOptions().get();
       return this;
     }
 

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMDocument.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMDocument.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMDocument.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMDocument.java Sat
Dec 31 02:39:39 2011
@@ -325,6 +325,10 @@ public class FOMDocument<T extends Eleme
     public WriterOptions getDefaultWriterOptions() {
         return new FOMWriter().getDefaultWriterOptions();
     }
+    
+    public WriterOptions.Builder makeDefaultWriterOptions() {
+      return new FOMWriter().makeDefaultWriterOptions();
+    }
 
     /**
      * Ensure that the underlying streams are fully parsed. We might eventually need to find
a more efficient way of

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java Sat
Dec 31 02:39:39 2011
@@ -761,6 +761,10 @@ public class FOMElement extends OMElemen
     public WriterOptions getDefaultWriterOptions() {
         return new FOMWriter().getDefaultWriterOptions();
     }
+    
+    public WriterOptions.Builder makeDefaultWriterOptions() {
+      return new FOMWriter().makeDefaultWriterOptions();
+    }
 
     /**
      * Ensure that the underlying streams are fully parsed. We might eventually need to find
a more efficient way of

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWriter.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWriter.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWriter.java Sat Dec
31 02:39:39 2011
@@ -92,8 +92,8 @@ public class FOMWriter extends AbstractW
     }
 
     @Override
-    protected WriterOptions initDefaultWriterOptions() {
-        return new FOMWriterOptions();
+    protected WriterOptions.Builder initDefaultWriterOptions() {
+        return WriterOptions.make();
     }
 
 }

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/PrettyWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/PrettyWriter.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/PrettyWriter.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/PrettyWriter.java Sat
Dec 31 02:39:39 2011
@@ -34,7 +34,6 @@ import org.apache.abdera2.model.Document
 import org.apache.abdera2.model.Element;
 import org.apache.abdera2.model.ElementWrapper;
 import org.apache.abdera2.writer.AbstractWriter;
-import org.apache.abdera2.writer.AbstractWriterOptions;
 import org.apache.abdera2.writer.WriterOptions;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
@@ -284,9 +283,8 @@ public class PrettyWriter extends Abstra
     }
 
     @Override
-    protected WriterOptions initDefaultWriterOptions() {
-        return new AbstractWriterOptions() {
-        };
+    protected WriterOptions.Builder initDefaultWriterOptions() {
+        return WriterOptions.make();
     }
 
 }

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractWriter.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractWriter.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractWriter.java Sat Dec
31 02:39:39 2011
@@ -46,35 +46,19 @@ public abstract class AbstractWriter imp
     }
 
     public synchronized WriterOptions getDefaultWriterOptions() {
-        if (options == null)
-            options = initDefaultWriterOptions();
-
-        // Make a copy of the options, so that changes to it don't result in
-        // changes to the Parser's defaults. Also, this allows us to remain
-        // thread safe without having to make ParseOptions implementations
-        // synchronized.
-
-        try {
-            return (WriterOptions)options.clone();
-        } catch (CloneNotSupportedException cnse) {
-            // This shouldn't actually happen
-            throw new RuntimeException(cnse);
-        }
+      if (options == null)
+          options = initDefaultWriterOptions().get();
+      return options;
+    }
+    
+    public WriterOptions.Builder makeDefaultWriterOptions() {
+      return initDefaultWriterOptions();
     }
 
-    protected abstract WriterOptions initDefaultWriterOptions();
+    protected abstract WriterOptions.Builder initDefaultWriterOptions();
 
     public synchronized Writer setDefaultWriterOptions(WriterOptions options) {
-        // Ok, we need to make a defensive copy of the options, since otherwise
-        // the caller still has access to the object, which means our access to
-        // it isn't certain to be thread safe.
-
-        try {
-            this.options = (options != null) ? (WriterOptions)options.clone() : initDefaultWriterOptions();
-        } catch (CloneNotSupportedException cnse) {
-            // This shouldn't actually happen
-            throw new RuntimeException(cnse);
-        }
+        this.options = options != null ? options : initDefaultWriterOptions().get();
         return this;
     }
 

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/WriterOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/WriterOptions.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/WriterOptions.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/WriterOptions.java Sat Dec
31 02:39:39 2011
@@ -19,37 +19,87 @@ package org.apache.abdera2.writer;
 
 import org.apache.abdera2.common.io.Compression.CompressionCodec;
 
-public interface WriterOptions extends Cloneable {
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableSet;
 
-    /**
-     * When writing, use the specified compression codecs
-     */
-    CompressionCodec[] getCompressionCodecs();
+public class WriterOptions {
 
+  public static Builder make() {
+    return new Builder();
+  }
+  
+  public static Builder from(WriterOptions options) {
+    Builder builder = new Builder();
+    builder.charset = options.charset;
+    builder.autoclose = options.autoclose;
+    builder.codecs.addAll(options.codecs);
+    return builder;
+  }
+  
+  public static class Builder implements Supplier<WriterOptions> {
+    
+    protected String charset = "UTF-8";
+    protected ImmutableSet.Builder<CompressionCodec> codecs = 
+      ImmutableSet.builder();
+    protected boolean autoclose = false;
+    
+    public Builder charset(String charset) {
+      this.charset = charset;
+      return this;
+    }
+    
+    public Builder compression(CompressionCodec codec) {
+      this.codecs.add(codec);
+      return this;
+    }
+    
+    public Builder autoclose() {
+      this.autoclose = true;
+      return this;
+    }
+    
+    public Builder doNotAutoclose() {
+      this.autoclose = false;
+      return this;
+    }
+    
+    public WriterOptions get() {
+      // TODO Auto-generated method stub
+      return null;
+    }
+    
+    
+  }
+  
+  private final String charset;
+  private final ImmutableSet<CompressionCodec> codecs;
+  private final boolean autoclose;
+  
+  WriterOptions(Builder builder) {
+    this.charset = builder.charset;
+    this.codecs = builder.codecs.build();
+    this.autoclose = builder.autoclose;
+  }
+  
     /**
      * When writing, use the specified compression codecs
      */
-    WriterOptions setCompressionCodecs(CompressionCodec... codecs);
-
-    Object clone() throws CloneNotSupportedException;
+    public Iterable<CompressionCodec> getCompressionCodecs() {
+      return codecs;
+    }
 
     /**
      * The character encoding to use for the output
      */
-    String getCharset();
-
-    /**
-     * The character encoding to use for the output
-     */
-    WriterOptions setCharset(String charset);
+    public String getCharset() {
+      return charset;
+    }
 
     /**
      * True if the writer should close the output stream or writer when finished
      */
-    boolean getAutoClose();
+    public boolean getAutoClose() {
+      return autoclose;
+    }
 
-    /**
-     * True if the writer should close the output stream or writer when finished
-     */
-    WriterOptions setAutoClose(boolean autoclose);
 }

Modified: abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityWriter.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityWriter.java
(original)
+++ abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityWriter.java
Sat Dec 31 02:39:39 2011
@@ -14,9 +14,11 @@ import org.apache.abdera2.model.Document
 import org.apache.abdera2.model.Entry;
 import org.apache.abdera2.model.Feed;
 import org.apache.abdera2.writer.AbstractWriter;
-import org.apache.abdera2.writer.AbstractWriterOptions;
 import org.apache.abdera2.writer.Writer;
 import org.apache.abdera2.writer.WriterOptions;
+
+import com.google.common.collect.Iterables;
+
 import static com.google.common.base.Preconditions.*;
 
 @Name("activity")
@@ -49,8 +51,7 @@ public class FeedToActivityWriter 
       checkNotNull(base);
     }
     checkArgument(base instanceof Feed || base instanceof Entry);
-    if (options.getCompressionCodecs() != null && 
-        options.getCompressionCodecs().length > 0) 
+    if (!Iterables.isEmpty(options.getCompressionCodecs())) 
       out = Compression.wrap(out, options.getCompressionCodecs());
     if (base instanceof Entry) {
        f2ac.convert((Entry)base).writeTo(out,options.getCharset());
@@ -96,9 +97,8 @@ public class FeedToActivityWriter 
     }
   }
 
-  protected WriterOptions initDefaultWriterOptions() {
-    return new AbstractWriterOptions() {
-    };
+  protected WriterOptions.Builder initDefaultWriterOptions() {
+    return WriterOptions.make();
   }
 
 }

Modified: abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/json/JSONWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/json/JSONWriter.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/json/JSONWriter.java (original)
+++ abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/json/JSONWriter.java Sat Dec 31
02:39:39 2011
@@ -27,7 +27,6 @@ import org.apache.abdera2.Abdera;
 import org.apache.abdera2.common.anno.Name;
 import org.apache.abdera2.model.Base;
 import org.apache.abdera2.writer.AbstractWriter;
-import org.apache.abdera2.writer.AbstractWriterOptions;
 import org.apache.abdera2.writer.WriterOptions;
 
 @Name("json")
@@ -43,9 +42,8 @@ public class JSONWriter extends Abstract
     }
 
     @Override
-    protected WriterOptions initDefaultWriterOptions() {
-        return new AbstractWriterOptions() {
-        };
+    protected WriterOptions.Builder initDefaultWriterOptions() {
+        return WriterOptions.make();
     }
 
     public Object write(Base base, WriterOptions options) throws IOException {

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java (original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java Sat Dec
31 02:39:39 2011
@@ -142,8 +142,8 @@ public class AppTest {
 
             response.setStatus(HttpServletResponse.SC_OK);
             response.setContentType("application/atomsvc+xml; charset=utf-8");
-            WriterOptions options = service.getDefaultWriterOptions();
-            options.setCharset("UTF-8");
+            WriterOptions options = service.makeDefaultWriterOptions()
+              .charset("UTF-8").get();
             service.writeTo(response.getOutputStream(), options);
         }
     }
@@ -184,8 +184,8 @@ public class AppTest {
                 case COLLECTION:
                     response.setStatus(HttpServletResponse.SC_OK);
                     response.setContentType("application/atom+xml; charset=utf-8");
-                    WriterOptions options = feed.getDefaultWriterOptions();
-                    options.setCharset("UTF-8");
+                    WriterOptions options = feed.makeDefaultWriterOptions()
+                    .charset("UTF-8").get();
                     feed.writeTo(response.getOutputStream(), options);
                     break;
                 case ENTRY:
@@ -193,8 +193,8 @@ public class AppTest {
                         Entry entry = feed.getRoot().getEntries().get(getTarget());
                         response.setStatus(HttpServletResponse.SC_OK);
                         response.setContentType("application/atom+xml; charset=utf-8");
-                        options = entry.getDefaultWriterOptions();
-                        options.setCharset("UTF-8");
+                        options = entry.makeDefaultWriterOptions()
+                        .charset("UTF-8").get();
                         entry.writeTo(response.getOutputStream(), options);
                     } catch (Exception e) {
                         response.sendError(HttpServletResponse.SC_NOT_FOUND);
@@ -245,8 +245,8 @@ public class AppTest {
                                 response.setStatus(HttpServletResponse.SC_CREATED);
                                 response.setHeader("Location", entry.getId().toString());
                                 response.setHeader("Content-Location", entry.getId().toString());
-                                WriterOptions woptions = entry.getDefaultWriterOptions();
-                                woptions.setCharset("UTF-8");
+                                WriterOptions woptions = entry.makeDefaultWriterOptions()
+                                .charset("UTF-8").get();
                                 entry.writeTo(response.getOutputStream(), woptions);
                                 return;
                             }
@@ -272,8 +272,8 @@ public class AppTest {
                             response.setStatus(HttpServletResponse.SC_CREATED);
                             response.setHeader("Location", entry.getId().toString());
                             response.setHeader("Content-Location", entry.getId().toString());
-                            WriterOptions woptions = entry.getDefaultWriterOptions();
-                            woptions.setCharset("UTF-8");
+                            WriterOptions woptions = entry.makeDefaultWriterOptions()
+                              .charset("UTF-8").get();
                             entry.writeTo(response.getOutputStream(), woptions);
                             return;
                         }

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/parser/EncodingTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/parser/EncodingTest.java?rev=1226027&r1=1226026&r2=1226027&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/parser/EncodingTest.java (original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/parser/EncodingTest.java Sat
Dec 31 02:39:39 2011
@@ -118,10 +118,12 @@ public class EncodingTest {
         entry.setTitle("1");
 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        WriterOptions writeoptions = entry.getDefaultWriterOptions();
-        writeoptions.setCompressionCodecs(CompressionCodec.DEFLATE);
-        writeoptions.setCharset("UTF-16");
-        writeoptions.setAutoClose(true);
+        WriterOptions writeoptions = 
+          entry.makeDefaultWriterOptions()
+            .compression(CompressionCodec.DEFLATE)
+            .charset("UTF-16")
+            .autoclose()
+            .get();
         entry.getDocument().writeTo(out, writeoptions);
         out.close();
 



Mime
View raw message