package swift.crdt.operations;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import swift.clocks.CausalityClock;
import swift.clocks.Timestamp;
import swift.crdt.CRDTIdentifier;
import swift.crdt.interfaces.CRDTOperation;

/**
 * Representation of a sequence of operations on an object that are considered
 * atomic.
 * <p>
 * The sequence of operations share the same state when they were issued, and a
 * base timestamp (two dimensional timestamp). Individual operations may have
 * unique TripleTimestamp based on this timestamp
 * <p>
 * Thread-hostile.
 * 
 * @author mzawirski
 */
public class CRDTObjectOperationsGroup {

    protected CRDTIdentifier id;
    protected CausalityClock dependencyClock;
    protected Timestamp baseTimestamp;
    protected List<CRDTOperation> operations;

    // Fake constructor for Kryo serialization. Do NOT use.
    public CRDTObjectOperationsGroup() {
    }

    public CRDTObjectOperationsGroup(CRDTIdentifier id, CausalityClock dependencyClock, Timestamp baseTimestamp) {
        this.id = id;
        this.dependencyClock = dependencyClock;
        this.baseTimestamp = baseTimestamp;
        this.operations = new LinkedList<CRDTOperation>();
    }

    /**
     * @return CRDT identifier on which operations are executed
     */
    public CRDTIdentifier getTargetUID() {
        return id;
    }

    /**
     * @return the base timestamp of all operations in the group
     */
    public Timestamp getBaseTimestamp() {
        return baseTimestamp;
    }

    /**
     * Replaces old base timestamp with the new base timestamp for all
     * operations in the group.
     * 
     * @param ts
     *            new base timestamp to use by all operations
     */
    public void replaceBaseTimestamp(Timestamp newBaseTimestamp) {
        baseTimestamp = newBaseTimestamp;
        for (CRDTOperation op : operations) {
            op.replaceBaseTimestamp(newBaseTimestamp);
        }
    }

    /**
     * Returns the causality clock for the objects on which the operation is to
     * be executed.
     * 
     * @return causality clock of object state when operations have been issued.
     */
    public CausalityClock getDependency() {
        return dependencyClock;
    }

    /**
     * Returns all operations in the group. Note that the returned list a
     * reference to the internal structure and should be retrieved only when all
     * {@link #addOperation(CRDTOperation)} have been performed.
     * 
     * @return all operations on an object, in order they were recorded and
     *         should be applied
     */
    public List<CRDTOperation> getOperations() {
        return Collections.unmodifiableList(operations);
    }

    /**
     * Adds an operation to the sequence.
     * 
     * @param op
     *            operation to add
     */
    public void addOperation(CRDTOperation op) {
        operations.add(op);
    }
}

@@ -321,37 +321,44 @@
 ect 
-that are considered%0A * atomic
+belonging to the same%0A * transaction
 .%0A *
@@ -401,62 +401,18 @@
 hare
- the same state when they were issued, and a%0A *
+s a
  base 
-t
+T
 imes
@@ -432,16 +432,19 @@
 ensional
+%0A *
  timesta
@@ -448,17 +448,22 @@
 stamp). 
-I
+Each i
 ndividua
@@ -479,19 +479,13 @@
 ons 
-may have%0A *
+has a
  uni
@@ -512,21 +512,30 @@
 based on
- this
+%0A * the common
  timesta
@@ -536,16 +536,17 @@
 imestamp
+.
 %0A * %3Cp%3E%0A
@@ -1242,16 +1242,31 @@
 ntifier 
+for the object 
 on which
@@ -1266,16 +1266,20 @@
 n which 
+the 
 operatio
@@ -1284,16 +1284,31 @@
 ions are
+%0A     *        
  execute
@@ -1409,20 +1409,16 @@
 @return 
-the 
 base tim
@@ -1449,21 +1449,24 @@
  in the 
-group
+sequence
 %0A     */
@@ -1721,19 +1721,23 @@
 tamp to 
+be 
 use
+d
  by all 
@@ -2039,17 +2039,16 @@
 e object
-s
  on whic
@@ -2062,19 +2062,21 @@
 peration
- is
+s are
  to%0A    
@@ -2178,17 +2178,16 @@
 n issued
-.
 %0A     */
@@ -2756,14 +2756,20 @@
  * A
-dds an
+ppends a new
  ope
@@ -2790,16 +2790,30 @@
 sequence
+ of operations
 .%0A     *
@@ -2841,32 +2841,37 @@
    *            
+next 
 operation to add
@@ -2867,19 +2867,49 @@
 tion to 
-add
+be applied within the transaction
 %0A     */

Work on comments.