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.data.lazy;
 
 
 
 import java.util.Map;
 
 import static com.graphaware.common.util.PropertyContainerUtils.id;

com.graphaware.tx.event.improved.data.PropertyContainerTransactionData that lazily initializes its internal structures (indexed transaction data) as they are needed by callers to prevent unnecessary overheads.

Parameters:
<T> type of the property container.
 
 public abstract class LazyPropertyContainerTransactionData<T extends PropertyContainerimplements PropertyContainerTransactionData<T> {
     private static final Logger LOG = LoggerFactory.getLogger(LazyPropertyContainerTransactionData.class);
 
     private Map<Long, T> created = null;
     private Map<Long, T> deleted = null;
     private Map<LongChange<T>> changed = null;

    
<ID, <key, new value>>
 
     private Map<LongMap<StringObject>> createdProperties = null;
    
<ID, <key, old value>>
 
     private Map<LongMap<StringObject>> deletedProperties = null;
    
<ID, <key, old and new value>>
 
     private Map<LongMap<StringChange<Object>>> changedProperties = null;
    
<ID, <key, old value>> of properties of deleted property containers
 
     private Map<LongMap<StringObject>> deletedContainersProperties = null;

    
Create an old snapshot of an original property container.

Parameters:
original to create a snapshot from.
Returns:
the snapshot.
 
     protected abstract T oldSnapshot(T original);

    
Create a new snapshot of an original property container.

Parameters:
original to create a snapshot from.
Returns:
the snapshot.
 
     protected abstract T newSnapshot(T original);

    
 
     @Override
     public boolean hasBeenCreated(T container) {
         initializeCreated();
         return .containsKey(id(container));
     }

    
 
     @Override
     public Collection<T> getAllCreated() {
         initializeCreated();
         return Collections.unmodifiableCollection(.values());
     }
 
     private void initializeCreated() {
         if ( == null) {
             = new HashMap<>();
            for (T created : created()) {
                this..put(id(created), newSnapshot(created));
            }
        }
    }

    
Get all property containers created in the transaction from the Neo4j API.

Returns:
created property containers.
    protected abstract Iterable<T> created();

    
    @Override
    public boolean hasBeenDeleted(T container) {
        initializeDeleted();
        return .containsKey(id(container));
    }

    
    @Override
    public T getDeleted(T container) {
        initializeDeleted();
        if (!hasBeenDeleted(container)) {
            throw new IllegalArgumentException(container + " has not been deleted!");
        }
        return .get(id(container));
    }

    
    @Override
    public Collection<T> getAllDeleted() {
        initializeDeleted();
        return Collections.unmodifiableCollection(.values());
    }
    private void initializeDeleted() {
        if ( == null) {
             = new HashMap<>();
            for (T deleted : deleted()) {
                this..put(id(deleted), oldSnapshot(deleted));
            }
        }
    }

    
Get all property containers deleted in the transaction from the Neo4j API.

Returns:
created property containers.
    protected abstract Iterable<T> deleted();

    
    @Override
    public boolean hasBeenChanged(T container) {
        initializeChanged();
        return changedContainsKey(container);
    }

    
    @Override
    public Change<T> getChanged(T container) {
        initializeChanged();
        if (!hasBeenChanged(container)) {
            throw new IllegalArgumentException(container + " has not been changed!");
        }
        return .get(id(container));
    }

    
    @Override
    public Collection<Change<T>> getAllChanged() {
        initializeChanged();
        return Collections.unmodifiableCollection(.values());
    }
    protected void initializeChanged() {
        initializeCreated();
        initializeDeleted();
        if ( == null) {
             = new HashMap<>();
            for (PropertyEntry<T> propertyEntry : assignedProperties()) {
                if (hasNotActuallyChanged(propertyEntry)) {
                    continue;
                }
                T candidate = propertyEntry.entity();
                if (!hasBeenCreated(candidate)) {
                    registerChange(candidate);
                }
            }
            for (PropertyEntry<T> propertyEntry : removedProperties()) {
                T candidate = propertyEntry.entity();
                if (!hasBeenDeleted(candidate)) {
                    registerChange(candidate);
                }
            }
            doInitializeChanged();
        }
    }
    protected void doInitializeChanged() {
        //for subclasses
    }
    protected void registerChange(T candidate) {
        if (!changedContainsKey(candidate)) {
            Change<T> change = new Change<>(oldSnapshot(candidate), newSnapshot(candidate));
            .put(id(candidate), change);
        }
    }
    protected boolean changedContainsKey(T candidate) {
        return .containsKey(id(candidate));
    }

    
Get all assigned properties from the Neo4j API.

Returns:
assigned properties.
    protected abstract Iterable<PropertyEntry<T>> assignedProperties();

    
Get all removed properties from the Neo4j API.

Returns:
removed properties.
    protected abstract Iterable<PropertyEntry<T>> removedProperties();

    
    @Override
    public boolean hasPropertyBeenCreated(T containerString key) {
        initializeProperties();
        if (!hasBeenChanged(container)) {
            .warn(container + " has not been changed but the caller thinks it should have created properties.");
            return false;
        }
        if (!.containsKey(id(container))) {
            return false;
        }
        return .get(id(container)).containsKey(key);
    }

    
    @Override
    public Map<StringObjectcreatedProperties(T container) {
        initializeProperties();
        if (!hasBeenChanged(container)) {
            .warn(container + " has not been changed but the caller thinks it should have created properties.");
            return Collections.emptyMap();
        }
        if (!.containsKey(id(container))) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(.get(id(container)));
    }

    
    @Override
    public boolean hasPropertyBeenDeleted(T containerString key) {
        initializeProperties();
        if (!hasBeenChanged(container)) {
            .warn(container + " has not been changed but the caller thinks it should have deleted properties.");
            return false;
        }
        if (!.containsKey(id(container))) {
            return false;
        }
        return .get(id(container)).containsKey(key);
    }

    
    @Override
    public Map<StringObjectdeletedProperties(T container) {
        initializeProperties();
        if (!hasBeenChanged(container)) {
            .warn(container + " has not been changed but the caller thinks it should have deleted properties.");
            return Collections.emptyMap();
        }
        if (!.containsKey(id(container))) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(.get(id(container)));
    }

    
    @Override
    public Map<StringObjectpropertiesOfDeletedContainer(T container) {
        initializeProperties();
        if (!hasBeenDeleted(container)) {
            .error(container + " has not been deleted but the caller thinks it has! This is a bug.");
            throw new IllegalStateException(container + " has not been deleted but the caller thinks it has! This is a bug.");
        }
        if (!.containsKey(id(container))) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(.get(id(container)));
    }

    
    @Override
    public boolean hasPropertyBeenChanged(T containerString key) {
        initializeProperties();
        if (!hasBeenChanged(container)) {
            .warn(container + " has not been changed but the caller thinks it should have changed properties.");
            return false;
        }
        if (!.containsKey(id(container))) {
            return false;
        }
        return .get(id(container)).containsKey(key);
    }

    
    @Override
    public Map<StringChange<Object>> changedProperties(T container) {
        initializeProperties();
        if (!hasBeenChanged(container)) {
            .warn(container + " has not been changed but the caller thinks it should have changed properties.");
            return Collections.emptyMap();
        }
        if (!.containsKey(id(container))) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(.get(id(container)));
    }
    private void initializeProperties() {
        if ( != null) {
            assert  != null;
            assert  != null;
            assert  != null;
            return;
        }
        //initializeCreated(); // - called by initializeChanged()
        //initializeDeleted(); // - called by initializeChanged()
        initializeChanged();
         = new HashMap<>();
         = new HashMap<>();
         = new HashMap<>();
         = new HashMap<>();
        for (PropertyEntry<T> propertyEntry : assignedProperties()) {
            T container = propertyEntry.entity();
            if (hasBeenCreated(container)) {
                continue;
            }
            if (hasNotActuallyChanged(propertyEntry)) {
                continue;
            }
            if (propertyEntry.previouslyCommitedValue() == null) {
                if (!.containsKey(id(container))) {
                    .put(id(container), new HashMap<StringObject>());
                }
                .get(id(container)).put(propertyEntry.key(), propertyEntry.value());
            } else {
                if (!.containsKey(id(container))) {
                    .put(id(container), new HashMap<StringChange<Object>>());
                }
                .get(id(container)).put(propertyEntry.key(), new Change<>(propertyEntry.previouslyCommitedValue(), propertyEntry.value()));
            }
        }
        for (PropertyEntry<T> propertyEntry : removedProperties()) {
            T container = propertyEntry.entity();
            if (.containsKey(id(container))) {
                if (!.containsKey(id(container))) {
                    .put(id(container), new HashMap<StringObject>());
                }
                .get(id(container)).put(propertyEntry.key(), propertyEntry.previouslyCommitedValue());
                continue;
            }
            if (!changedContainsKey(container)) {
                throw new IllegalStateException(container + " seems to have not been deleted or changed, this is a bug");
            }
            assert changedContainsKey(container);
            if (!.containsKey(id(container))) {
                .put(id(container), new HashMap<StringObject>());
            }
            .get(id(container)).put(propertyEntry.key(), propertyEntry.previouslyCommitedValue());
        }
    }
    private boolean hasNotActuallyChanged(PropertyEntry<T> propertyEntry) {
        return propertyEntry.previouslyCommitedValue() != null && propertyEntry.previouslyCommitedValue().equals(propertyEntry.value());
    }
New to GrepCode? Check out our FAQ X