go through the changes and revise comments

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_opentype@378 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java
index 26455f5..c41d908 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java
@@ -47,25 +47,26 @@
  * Dynamically cast a variable from its type to a specified required type, in a
  * recursive way. It enables: 1. bag-based fields in a record, 2. bidirectional
  * cast of a open field and a matched closed field, and 3. put in null fields
- * when necessary
+ * when necessary.
  * 
- * Here is example: A record { "hobby": {{"music", "coding"}}, "id": "001",
+ * Here is an example: A record { "hobby": {{"music", "coding"}}, "id": "001",
  * "name": "Person Three"} which confirms to closed type ( id: string, name:
- * string, hobby: {{string}}? ) can be casted to a open type (id: string ), or
+ * string, hobby: {{string}}? ) can be cast to an open type (id: string ), or
  * vice versa.
  * 
- * However, if input record is a variable, then we don't know its exact field
- * layout at compile time, for example, records confirming the same type can
- * different field ordering, different open part. That's why we need dynamic
- * type casting.
+ * However, if the input record is a variable, then we don't know its exact
+ * field layout at compile time. For example, records conforming to the same
+ * type can have different field orderings and different open parts. That's why
+ * we need dynamic type casting.
  * 
  * Note that as we can see in the example, the ordering of fields of a record is
- * not required. Since the open/close part of a record has completely different
+ * not required. Since the open/closed part of a record has completely different
  * underlying memory/storage layout, a cast-record function will change the
  * layout as specified at runtime.
  * 
  * Implementation wise, this rule checks the target dataset type and the input
- * record type, if the types are different, then enforce a cast function.
+ * record type, and if the types are different, then it plugs in an assign with
+ * a cast-record function, and projects away the original (uncast) field.
  */
 public class IntroduceDynamicTypeCastRule implements IAlgebraicRewriteRule {
 
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
index e14f53d..7dc35fe 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
@@ -54,30 +54,30 @@
 import edu.uci.ics.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 
 /**
- * Statically cast a constant from its type produced by the originated
- * expression to its required type, in a recursive way it enables: 1. bag-based
- * fields in a record, 2. bidirectional cast of a open field and a matched
- * closed field, and 3. put in null fields when necessary. It should be fired
- * before the constant folding rule.
+ * Statically cast a constant from its type to a specified required type, in a
+ * recursive way. It enables: 1. bag-based fields in a record, 2. bidirectional
+ * cast of a open field and a matched closed field, and 3. put in null fields
+ * when necessary. It should be fired before the constant folding rule.
  * 
  * This rule is not responsible for type casting between primitive types.
  * 
- * Here is example: A record { "hobby": {{"music", "coding"}}, "id": "001",
+ * Here is an example: A record { "hobby": {{"music", "coding"}}, "id": "001",
  * "name": "Person Three"} which confirms to closed type ( id: string, name:
- * string, hobby: {{string}}? ) can be casted to a open type (id: string ), or
+ * string, hobby: {{string}}? ) can be cast to an open type (id: string ), or
  * vice versa.
  * 
- * If the record is a constant, the situation that we are going into insert the
- * record into a dataset with a different type can be captured at the compile
- * time, and type cast is done in the rule.
- * 
  * Implementation wise: first, we match the record's type and its target dataset
  * type to see if it is "cast-able"; second, if the types are cast-able, we
- * embed the require type to the original producer expression. If the types are
- * not cast-able, we throw compile time exceptions.
+ * embed the required type into the original producer expression. If the types
+ * are not cast-able, we throw a compile time exception.
  * 
- * Then, at runtime (not in that rule), the constructors know what to do by
- * checking the required output type.
+ * Then, at runtime (not in this rule), the corresponding record/list
+ * constructors know what to do by checking the required output type.
+ * 
+ * TODO: right now record/list constructor of the cast result is not done in the
+ * ConstantFoldingRule and has to go to the runtime, because the
+ * ConstantFoldingRule uses ARecordSerializerDeserializer which seems to have
+ * some problem.
  */
 public class IntroduceStaticTypeCastRule implements IAlgebraicRewriteRule {
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java
index 41d55b6..a3547a4 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java
@@ -19,12 +19,12 @@
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
 import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
-import edu.uci.ics.asterix.runtime.util.container.IElementFactory;
+import edu.uci.ics.asterix.runtime.util.container.IObjectFactory;
 import edu.uci.ics.hyracks.data.std.api.IValueReference;
 
 /**
- * This class is to represent a flat field, e.g., int field, string field, and
- * so on, based on an binary representation
+ * This class represents a flat field, e.g., int field, string field, and
+ * so on, based on a binary representation.
  */
 public class AFlatValuePointable extends AbstractVisitablePointable {
 
@@ -33,8 +33,8 @@
      * use object pool based allocator. The factory is not public so that it
      * cannot called in other places than PointableAllocator.
      */
-    static IElementFactory<IVisitablePointable, IAType> FACTORY = new IElementFactory<IVisitablePointable, IAType>() {
-        public AFlatValuePointable createElement(IAType type) {
+    static IObjectFactory<IVisitablePointable, IAType> FACTORY = new IObjectFactory<IVisitablePointable, IAType>() {
+        public AFlatValuePointable create(IAType type) {
             return new AFlatValuePointable();
         }
     };
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java
index 9bcf3f5..6f85694 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java
@@ -29,22 +29,22 @@
 import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
 import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.asterix.runtime.util.ResettableByteArrayOutputStream;
-import edu.uci.ics.asterix.runtime.util.container.IElementFactory;
+import edu.uci.ics.asterix.runtime.util.container.IObjectFactory;
 
 /**
- * This class is to interpret the binary data representation of a list, one can
+ * This class interprets the binary data representation of a list, one can
  * call getItems and getItemTags to get pointable objects for items and item
- * type tags
+ * type tags.
  * 
  */
 public class AListPointable extends AbstractVisitablePointable {
 
     /**
      * DO NOT allow to create AListPointable object arbitrarily, force to use
-     * object pool based allocator, in order to have object reuse
+     * object pool based allocator, in order to have object reuse.
      */
-    static IElementFactory<IVisitablePointable, IAType> FACTORY = new IElementFactory<IVisitablePointable, IAType>() {
-        public IVisitablePointable createElement(IAType type) {
+    static IObjectFactory<IVisitablePointable, IAType> FACTORY = new IObjectFactory<IVisitablePointable, IAType>() {
+        public IVisitablePointable create(IAType type) {
             return new AListPointable((AbstractCollectionType) type);
         }
     };
@@ -64,7 +64,6 @@
      * private constructor, to prevent constructing it arbitrarily
      * 
      * @param inputType
-     *            , the input type
      */
     private AListPointable(AbstractCollectionType inputType) {
         if (inputType != null && inputType.getItemType() != null) {
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java
index 6634252..5904170 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java
@@ -32,12 +32,12 @@
 import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
 import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.asterix.runtime.util.ResettableByteArrayOutputStream;
-import edu.uci.ics.asterix.runtime.util.container.IElementFactory;
+import edu.uci.ics.asterix.runtime.util.container.IObjectFactory;
 import edu.uci.ics.hyracks.api.dataflow.value.INullWriter;
 
 /**
- * This class is to interpret the binary data representation of a record, one
- * can call getFieldNames, getFieldTypeTags and getFieldValues to get pointable
+ * This class interprets the binary data representation of a record. One can
+ * call getFieldNames, getFieldTypeTags and getFieldValues to get pointable
  * objects for field names, field type tags, and field values.
  * 
  */
@@ -47,8 +47,8 @@
      * DO NOT allow to create ARecordPointable object arbitrarily, force to use
      * object pool based allocator, in order to have object reuse
      */
-    static IElementFactory<IVisitablePointable, IAType> FACTORY = new IElementFactory<IVisitablePointable, IAType>() {
-        public IVisitablePointable createElement(IAType type) {
+    static IObjectFactory<IVisitablePointable, IAType> FACTORY = new IObjectFactory<IVisitablePointable, IAType>() {
+        public IVisitablePointable create(IAType type) {
             return new ARecordPointable((ARecordType) type);
         }
     };
@@ -71,7 +71,7 @@
 
     private final int numberOfSchemaFields;
     private final int[] fieldOffsets;
-    private final IVisitablePointable nullReference = AFlatValuePointable.FACTORY.createElement(null);
+    private final IVisitablePointable nullReference = AFlatValuePointable.FACTORY.create(null);
 
     private int closedPartTypeInfoSize = 0;
     private int offsetArrayOffset;
@@ -105,7 +105,7 @@
                 int tagStart = typeBos.size();
                 typeDos.writeByte(ftypeTag.serialize());
                 int tagEnd = typeBos.size();
-                IVisitablePointable typeTagReference = AFlatValuePointable.FACTORY.createElement(null);
+                IVisitablePointable typeTagReference = AFlatValuePointable.FACTORY.create(null);
                 typeTagReference.set(typeBos.getByteArray(), tagStart, tagEnd - tagStart);
                 fieldTypeTags.add(typeTagReference);
 
@@ -114,7 +114,7 @@
                 typeDos.writeByte(ATypeTag.STRING.serialize());
                 typeDos.writeUTF(fieldNameStrs[i]);
                 int nameEnd = typeBos.size();
-                IVisitablePointable typeNameReference = AFlatValuePointable.FACTORY.createElement(null);
+                IVisitablePointable typeNameReference = AFlatValuePointable.FACTORY.create(null);
                 typeNameReference.set(typeBos.getByteArray(), nameStart, nameEnd - nameStart);
                 fieldNames.add(typeNameReference);
             }
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java
index 6b844a0..8cfe661 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java
@@ -20,7 +20,7 @@
 
 /**
  * This class implements several "routine" methods in IVisitablePointable
- * interface, so that subclasses do not need to repeat the same code
+ * interface, so that subclasses do not need to repeat the same code.
  * 
  */
 public abstract class AbstractVisitablePointable implements IVisitablePointable {
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java
index e6f7b5e..b6116be 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java
@@ -24,8 +24,7 @@
 
 /**
  * This class is the ONLY place to create IVisitablePointable object instances,
- * so that all the client code are forced to have object reuse for those
- * IVisitablePointable objects
+ * to enforce use of an object pool.
  */
 public class PointableAllocator {
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java
index 0c39aa0..987ac02 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java
@@ -23,10 +23,10 @@
 import edu.uci.ics.asterix.om.types.IAType;
 
 /**
- * This class serves as the repository for default record type and list type
+ * This class serves as the repository for the default record type and list type
  * fields in the open part, e.g., a "record" (nested) field in the open part is
  * always a fully open one, and a "list" field in the open part is always a list
- * of "ANY"
+ * of "ANY".
  * 
  */
 public class DefaultOpenFieldType {
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java
index f630e24..28c61c2 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java
@@ -21,7 +21,7 @@
 
 /**
  * This interface extends IPointable with a visitor interface in order to ease
- * programming for recursive record structures
+ * programming for recursive record structures.
  */
 public interface IVisitablePointable extends IPointable {
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java
index 9d3fe73..822e37c 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java
@@ -31,14 +31,14 @@
 
 /**
  * This class is a IVisitablePointableVisitor implementation which recursively
- * visit a given record, list or flat value of a given type, and recursively
- * cast it to a specified type For example,
+ * visit a given record, list or flat value of a given type, and cast it to a
+ * specified type. For example:
  * 
  * A record { "hobby": {{"music", "coding"}}, "id": "001", "name":
  * "Person Three"} which confirms to closed type ( id: string, name: string,
  * hobby: {{string}}? ) can be casted to a open type (id: string )
  * 
- * Since the open/close part of a record has completely different underlying
+ * Since the open/closed part of a record has a completely different underlying
  * memory/storage layout, the visitor will change the layout as specified at
  * runtime.
  */
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java
index d3e4a12..e10fb72 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java
@@ -38,8 +38,7 @@
 
 /**
  * This class is to do the runtime type cast for a list. It is ONLY visible to
- * ACastVisitor, so that no other client places can call into that caster and
- * unnecessary bugs could be prevented.
+ * ACastVisitor.
  */
 class AListCaster {
     // pointable allocator
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java
index 561c113..50c8b37 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java
@@ -45,8 +45,7 @@
 
 /**
  * This class is to do the runtime type cast for a record. It is ONLY visible to
- * ACastVisitor, so that no other client places can call into that caster and
- * unnecessary bugs could be prevented.
+ * ACastVisitor.
  */
 class ARecordCaster {
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java
index 3f99509..669e3fc 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java
@@ -20,6 +20,11 @@
 import edu.uci.ics.asterix.runtime.pointables.AListPointable;
 import edu.uci.ics.asterix.runtime.pointables.ARecordPointable;
 
+/**
+ * This interface is a visitor for all the three different IVisitablePointable
+ * (Note that right now we have three pointable implementations for type
+ * casting) implementations.
+ */
 public interface IVisitablePointableVisitor<R, T> {
 
     public R visit(AListPointable accessor, T arg) throws AsterixException;
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java
index 117b7f4..5aecd08 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java
@@ -2,6 +2,11 @@
 
 import edu.uci.ics.hyracks.dataflow.common.comm.io.ByteArrayAccessibleOutputStream;
 
+/**
+ * This class extends ByteArrayAccessibleOutputStream to allow reset to a given
+ * size.
+ * 
+ */
 public class ResettableByteArrayOutputStream extends ByteArrayAccessibleOutputStream {
 
     public void reset(int size) {
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IElementFactory.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectFactory.java
similarity index 76%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IElementFactory.java
rename to asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectFactory.java
index f914d13..85f0fa3 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IElementFactory.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectFactory.java
@@ -16,17 +16,15 @@
 package edu.uci.ics.asterix.runtime.util.container;
 
 /**
- * A factory interface to create elements. Elements in a reusable pool/container
- * can ONLY be instantiated from the corresponding factory
+ * A factory interface to create objects.
  */
-public interface IElementFactory<E, T> {
+public interface IObjectFactory<E, T> {
 
     /**
      * create an element of type E
      * 
      * @param arg
-     *            of type T
      * @return an E element
      */
-    public E createElement(T arg);
+    public E create(T arg);
 }
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java
index 9738985..dd2c3bf 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java
@@ -16,7 +16,7 @@
 package edu.uci.ics.asterix.runtime.util.container;
 
 /**
- * A reusable object pool interface
+ * A reusable object pool interface.
  */
 public interface IObjectPool<E, T> {
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
index 569c555..b8f8e3a 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
@@ -28,10 +28,10 @@
  */
 public class ListObjectPool<E, T> implements IObjectPool<E, T> {
 
-    private IElementFactory<E, T> factory;
+    private IObjectFactory<E, T> factory;
 
     /**
-     * element reusable object pool
+     * cache of objects
      */
     private List<E> pool = new ArrayList<E>();
 
@@ -45,7 +45,7 @@
      */
     private BitSet usedBits = new BitSet();
 
-    public ListObjectPool(IElementFactory<E, T> factory) {
+    public ListObjectPool(IObjectFactory<E, T> factory) {
         this.factory = factory;
     }
 
@@ -68,7 +68,7 @@
         }
 
         // if we do not find a reusable object, allocate a new one
-        E element = factory.createElement(arg);
+        E element = factory.create(arg);
         pool.add(element);
         args.add(arg);
         usedBits.set(pool.size() - 1);