Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2013 GraphAware
   *
   * This file is part of GraphAware.
   *
   * GraphAware is free software: you can redistribute it and/or modify it under the terms of
   * the GNU General Public License as published by the Free Software Foundation, either
   * version 3 of the License, or (at your option) any later version.
   *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU General Public License for more details. You should have received a copy of
  * the GNU General Public License along with this program.  If not, see
  * <http://www.gnu.org/licenses/>.
  */
 
 package com.graphaware.tx.event.improved.propertycontainer.snapshot;
 
 
A decorator of a org.neo4j.graphdb.PropertyContainer that represents a snapshot of a org.neo4j.graphdb.PropertyContainer before a transaction has started. It consults com.graphaware.tx.event.improved.data.PropertyContainerTransactionData wrapped in com.graphaware.tx.event.improved.data.TransactionDataContainer, before returning information about contained properties in order to provide these as they were before the transactions started. Mutations are preformed as expected but only in the case that the mutated org.neo4j.graphdb.PropertyContainer has not been deleted in the transaction. If it has been deleted, an exception is thrown upon mutation.

Parameters:
<T> type of the wrapped property container.
 
 public abstract class PropertyContainerSnapshot<T extends PropertyContainerextends BasePropertyContainerWrapper<T> implements PropertyContainer {
     private static final Logger LOG = LoggerFactory.getLogger(PropertyContainerSnapshot.class);
 
     protected final T wrapped;
     protected final TransactionDataContainer transactionDataContainer;

    
Construct a new snapshot.

Parameters:
wrapped property container.
transactionDataContainer transaction data container.
 
     protected PropertyContainerSnapshot(T wrappedTransactionDataContainer transactionDataContainer) {
         this. = wrapped;
         this. = transactionDataContainer;
     }

    
 
     @Override
     public T getWrapped() {
         return ;
     }

    
Get data about the transaction relevant for the subclass.

Returns:
transaction data.
 
     protected abstract PropertyContainerTransactionData<T> transactionData();

    
 
     @Override
     public boolean hasProperty(String key) {
         if (transactionData().hasBeenDeleted()) {
             return transactionData().propertiesOfDeletedContainer().containsKey(key);
         }
 
         if (transactionData().hasBeenChanged()) {
             if (transactionData().hasPropertyBeenCreated(key)) {
                 return false;  //has been created in the transaction = wasn't there before
             }
             if (transactionData().hasPropertyBeenDeleted(key)) {
                 return true;   //has been deleted in the transaction = was there before
             }
         }
 
         //at this point, we know that either of the following is the case:
         //- the container has not been changed by the tx
         //- the container has been changed and the property we're looking for has not
         //- the container has been changed and the property we're looking for has been changed
         // In either case, we can find out whether it was there by delegating to the wrapped one (its current version).
 
         return super.hasProperty(key);
     }

    
    @Override
    public Object getProperty(String key) {
        if (!hasProperty(key)) {
            throw new NotFoundException("Snapshot of " + .toString() + " did not have a property with key " + key);
        }
        if (transactionData().hasBeenDeleted()) {
            return transactionData().propertiesOfDeletedContainer().get(key);
        }
        if (transactionData().hasBeenChanged()) {
            if (transactionData().hasPropertyBeenChanged(key)) {
                return transactionData().changedProperties().get(key).getPrevious();
            }
            if (transactionData().hasPropertyBeenDeleted(key)) {
                return transactionData().deletedProperties().get(key);
            }
        }
        return super.getProperty(key);
    }

    
    @Override
    public void setProperty(String keyObject value) {
        checkCanBeMutated();
        super.setProperty(keyvalue);
    }

    
    @Override
    public Object removeProperty(String key) {
        checkCanBeMutated();
        return super.removeProperty(key);
    }

    
Check whether this property container has not been deleted and can thus be mutated. In case the container can be mutated, nothing happens. In the opposite case, an java.lang.IllegalStateException exception is thrown.
    protected void checkCanBeMutated() {
        if (transactionData().hasBeenDeleted()) {
            .error("Deleted property container " +  + " should not be mutated.");
            throw new IllegalStateException("Deleted property container " +  + " should not be mutated.");
        }
    }

    
    @Override
    public Iterable<StringgetPropertyKeys() {
        if (transactionData().hasBeenDeleted()) {
            return transactionData().propertiesOfDeletedContainer().keySet();
        }
        if (!transactionData().hasBeenChanged()) {
            return super.getPropertyKeys();
        }
        Collection<Stringresult = new HashSet<>();
        for (String key : super.getPropertyKeys()) {
            if (!transactionData().hasPropertyBeenCreated(key)) {
                result.add(key);
            }
        }
        return result;
    }
New to GrepCode? Check out our FAQ X