integrate list printer visitors

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_printerfix@918 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java
index 563b14c..935ea0b 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java
@@ -2,22 +2,21 @@
 
 import java.io.PrintStream;
 
-import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
-import edu.uci.ics.asterix.formats.nontagged.AqlPrinterFactoryProvider;
 import edu.uci.ics.asterix.om.types.AOrderedListType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
-import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
+import edu.uci.ics.asterix.runtime.pointables.PointableAllocator;
+import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.runtime.pointables.printer.APrintVisitor;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
 
 public class AOrderedlistPrinterFactory implements IPrinterFactory {
-
+    
     private static final long serialVersionUID = 1L;
-
     private AOrderedListType orderedlistType;
 
     public AOrderedlistPrinterFactory(AOrderedListType orderedlistType) {
@@ -27,77 +26,29 @@
     @Override
     public IPrinter createPrinter() {
 
-        return new IPrinter() {
+        PointableAllocator allocator = new PointableAllocator();
+        final IAType inputType = orderedlistType == null ? DefaultOpenFieldType
+                .getDefaultOpenFieldType(ATypeTag.ORDEREDLIST) : orderedlistType;
+        final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
+        final APrintVisitor printVisitor = new APrintVisitor();
+        final Pair<PrintStream, ATypeTag> arg = new Pair<PrintStream, ATypeTag>(null, null);
 
-            private IPrinter itemPrinter;
-            private IAType itemType;
-            private ATypeTag itemTag;
-            private boolean typedItemList = false;
+        return new IPrinter() {
 
             @Override
             public void init() throws AlgebricksException {
-
-                if (orderedlistType != null && orderedlistType.getItemType() != null) {
-                    itemType = orderedlistType.getItemType();
-                    if (itemType.getTypeTag() == ATypeTag.ANY) {
-                        this.typedItemList = false;
-                        this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                    } else {
-                        this.typedItemList = true;
-                        itemPrinter = AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(itemType).createPrinter();
-                        itemTag = orderedlistType.getItemType().getTypeTag();
-                    }
-                } else {
-                    this.typedItemList = false;
-                    this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                }
-                itemPrinter.init();
-
+                arg.second = inputType.getTypeTag();
             }
 
             @Override
-            public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
-                ps.print("[ ");
-                int numberOfitems = AInt32SerializerDeserializer.getInt(b, s + 6);
-                int itemOffset;
-                if (typedItemList) {
-                    switch (itemTag) {
-                        case STRING:
-                        case RECORD:
-                        case ORDEREDLIST:
-                        case UNORDEREDLIST:
-                        case ANY:
-                            itemOffset = s + 10 + (numberOfitems * 4);
-                            break;
-                        default:
-                            itemOffset = s + 10;
-                    }
-                } else
-                    itemOffset = s + 10 + (numberOfitems * 4);
-                int itemLength = 0;
+            public void print(byte[] b, int start, int l, PrintStream ps) throws AlgebricksException {
                 try {
-                    if (typedItemList) {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, false);
-                            itemPrinter.print(b, itemOffset - 1, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    } else {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[itemOffset]);
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, true) + 1;
-                            itemPrinter.print(b, itemOffset, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    }
-                } catch (AsterixException e) {
-                    throw new AlgebricksException(e);
+                    listAccessor.set(b, start, l);
+                    arg.first = ps;
+                    listAccessor.accept(printVisitor, arg);
+                } catch (Exception ioe) {
+                    throw new AlgebricksException(ioe);
                 }
-                ps.print(" ]");
             }
         };
     }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java
index cc9e0bd..def51ed 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java
@@ -4,6 +4,7 @@
 
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.runtime.pointables.PointableAllocator;
 import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
 import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
@@ -14,9 +15,8 @@
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
 
 public class ARecordPrinterFactory implements IPrinterFactory {
-
+    
     private static final long serialVersionUID = 1L;
-
     private final ARecordType recType;
 
     public ARecordPrinterFactory(ARecordType recType) {
@@ -27,8 +27,8 @@
     public IPrinter createPrinter() {
 
         PointableAllocator allocator = new PointableAllocator();
-        final ARecordType inputType = recType == null ? (ARecordType) DefaultOpenFieldType
-                .getDefaultOpenFieldType(ATypeTag.RECORD) : recType;
+        final IAType inputType = recType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.RECORD)
+                : recType;
         final IVisitablePointable recAccessor = allocator.allocateRecordValue(inputType);
         final APrintVisitor printVisitor = new APrintVisitor();
         final Pair<PrintStream, ATypeTag> arg = new Pair<PrintStream, ATypeTag>(null, null);
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java
index 59af365..9bb7189 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java
@@ -2,22 +2,21 @@
 
 import java.io.PrintStream;
 
-import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
-import edu.uci.ics.asterix.formats.nontagged.AqlPrinterFactoryProvider;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AUnorderedListType;
-import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
+import edu.uci.ics.asterix.runtime.pointables.PointableAllocator;
+import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.runtime.pointables.printer.APrintVisitor;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
 
 public class AUnorderedlistPrinterFactory implements IPrinterFactory {
 
     private static final long serialVersionUID = 1L;
-
     private AUnorderedListType unorderedlistType;
 
     public AUnorderedlistPrinterFactory(AUnorderedListType unorderedlistType) {
@@ -27,77 +26,29 @@
     @Override
     public IPrinter createPrinter() {
 
-        return new IPrinter() {
+        PointableAllocator allocator = new PointableAllocator();
+        final IAType inputType = unorderedlistType == null ? DefaultOpenFieldType
+                .getDefaultOpenFieldType(ATypeTag.UNORDEREDLIST) : unorderedlistType;
+        final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
+        final APrintVisitor printVisitor = new APrintVisitor();
+        final Pair<PrintStream, ATypeTag> arg = new Pair<PrintStream, ATypeTag>(null, null);
 
-            private IPrinter itemPrinter;
-            private IAType itemType;
-            private ATypeTag itemTag;
-            private boolean typedItemList = false;
+        return new IPrinter() {
 
             @Override
             public void init() throws AlgebricksException {
-
-                if (unorderedlistType != null && unorderedlistType.getItemType() != null) {
-                    itemType = unorderedlistType.getItemType();
-                    if (itemType.getTypeTag() == ATypeTag.ANY) {
-                        this.typedItemList = false;
-                        this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                    } else {
-                        this.typedItemList = true;
-                        itemPrinter = AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(itemType).createPrinter();
-                        itemTag = unorderedlistType.getItemType().getTypeTag();
-                    }
-                } else {
-                    this.typedItemList = false;
-                    this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                }
-                itemPrinter.init();
+                arg.second = inputType.getTypeTag();
             }
 
             @Override
-            public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
-                ps.print("{{ ");
-                int numberOfitems = AInt32SerializerDeserializer.getInt(b, s + 6);
-                int itemOffset;
-                if (typedItemList) {
-                    switch (itemTag) {
-                        case STRING:
-                        case RECORD:
-                        case ORDEREDLIST:
-                        case UNORDEREDLIST:
-                        case ANY:
-                            itemOffset = s + 10 + (numberOfitems * 4);
-                            break;
-                        default:
-                            itemOffset = s + 10;
-                    }
-                } else
-                    itemOffset = s + 10 + (numberOfitems * 4);
-                int itemLength;
-
+            public void print(byte[] b, int start, int l, PrintStream ps) throws AlgebricksException {
                 try {
-                    if (typedItemList) {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, false);
-                            itemPrinter.print(b, itemOffset - 1, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    } else {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[itemOffset]);
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, true) + 1;
-                            itemPrinter.print(b, itemOffset, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    }
-                } catch (AsterixException e) {
-                    throw new AlgebricksException(e);
+                    listAccessor.set(b, start, l);
+                    arg.first = ps;
+                    listAccessor.accept(printVisitor, arg);
+                } catch (Exception ioe) {
+                    throw new AlgebricksException(ioe);
                 }
-                ps.print(" }}");
             }
         };
     }