Change.java :  » GWT » jstm4gwt » jstm4gwt » core » Java Open Source

Java Open Source » GWT » jstm4gwt 
jstm4gwt » jstm4gwt » core » Change.java
/**
 * JSTM (http://xstm.net)
 * Distributed under the Apache License Version 2.0
 * Copyright  xstm.net
 */

package jstm4gwt.core;

import jstm4gwt.misc.ImmutableClasses;

/**
 * Represents a change made to an object by a transaction. This class is not
 * used for most scenarios. It is involved only if you want to explore or
 * manipulate the changes made by a transaction. E.g. if you register a listener
 * on a transaction, instances of this class will be created and given to your
 * listener each time an object is modified. When a transaction has committed or
 * aborted, it is not possible to modify its changes anymore.
 */
public abstract class Change {

    private final Transaction _transaction;

    protected Change(Transaction transaction) {
        _transaction = transaction;
    }

    public Transaction getTransaction() {
        return _transaction;
    }

    public abstract TObject getTObject();

    public boolean replaces(Change change) {
        return false;
    }

    protected static final class Field extends Change {

        private final TRandomAccess _object;

        private final int _index;

        protected Field(Transaction transaction, TRandomAccess object, int index) {
            super(transaction);

            _object = object;
            _index = index;
        }

        public TRandomAccess getRandomAccess() {
            return (TRandomAccess) getTObject();
        }

        @Override
        public TObject getTObject() {
            return _object;
        }

        private final String writeField(TRandomAccess.Version version) {
            String value;

            if (version != null)
                value = "" + version.get(_index);
            else {
                if (getRandomAccess() instanceof TClass) {
                    Class type = ((TClass) getRandomAccess()).getFieldClass(_index);
                    value = ImmutableClasses.getDefaultString(type);
                } else
                    value = "null";
            }

            return value;
        }

        @Override
        public String toString() {
            String name;

            if (getTObject() instanceof TClass)
                name = ((TClass) getTObject()).getFieldName(_index);
            else
                name = "Field " + _index;

            TRandomAccess.Version version = (TRandomAccess.Version) getTransaction().getVersion(getRandomAccess());
            TRandomAccess.Version previous = TRandomAccess.Version.getPrevious(getRandomAccess(), _index, getTransaction(), false);
            String before = writeField(previous);
            String after = writeField(version);

            return name + ": " + before + " -> " + after;
        }
    }

    protected static final class Clear extends Change {

        private final TMapVersion _version;

        protected Clear(Transaction transaction, TMapVersion version) {
            super(transaction);

            _version = version;
        }

        @Override
        public TObject getTObject() {
            return _version.getTObject();
        }

        @Override
        public String toString() {
            return "Clear";
        }
    }

    protected static final class Set extends Change {

        private final TMapVersion _version;

        //private final Object _key;

        protected Set(Transaction transaction, TMapVersion version, Object key) {
            super(transaction);

            _version = version;
            //_key = key;
        }

        @Override
        public TObject getTObject() {
            return _version.getTObject();
        }

        @Override
        public String toString() {
//            Object write = _version.getWrites().get(_key);
//            Object previous = _version.getSharedMap().get(_key);
//
//            if (write == Removal.Instance)
//                return _key + ": " + previous + " -> null";
//
//            return _key + ": " + previous + " -> " + write;
            throw new UnsupportedOperationException();
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.