All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jtendermint.merkletree.MerkleTree Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2016 - 2018 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.github.jtendermint.merkletree;

import com.github.jtendermint.merkletree.byteable.types.IByteable;

import java.util.Arrays;

public class MerkleTree implements IMerkleTree {

    private MerkleNode rootNode;

    @Override
    public int size() {
        return rootNode == null ? 0 : rootNode.getSize();
    }

    @Override
    public int getHeight() {
        return rootNode == null ? 0 : rootNode.getHeight();
    }

    @Override
    public boolean contains(K key) {
        return rootNode != null && rootNode.contains(key);
    }

    @Override
    public K get(K entry) {
        return rootNode == null ? null : rootNode.get(entry);
    }

    @Override
    public KeyIndex get(int index) {
        return rootNode == null ? null : rootNode.get(index);
    }

    @Override
    public boolean add(K entry) {
        if (rootNode == null) {
            rootNode = createNode(entry);
            return false;
        } else {
            AddResult result = rootNode.add(entry);
            rootNode = result.getNode();
            return result.wasUpdated();
        }
    }

    @Override
    public RemoveResult remove(K key) {
        if (rootNode == null) {
            return null;
        }

        rootNode.remove(key);
        //boolean result = rootNode.remove(key);
        // rootNode = result.getRootNode();
        // TODO Implement
        return null;
    }

    @Override
    public HashWithCount getHashWithCount() {
        HashWithCount result = new HashWithCount(null, 0);
        if (rootNode != null) {
            result = rootNode.getHashWithCount();
        }
        return result;
    }

    @Override
    public byte[] getRootHash() {
        if (rootNode == null) {
            return null;
        } else {
            byte[] rootHash = rootNode.getHashWithCount().hash;
            return rootHash != null ? Arrays.copyOf(rootHash, rootHash.length) : null;
        }
    }

    @Override
    public MerkleNode getRoot() {
        return rootNode;
    }

    @Override
    public String toPrettyString() {
        if (rootNode == null) {
            return "()";
        }
        return rootNode.toPrettyString();
    }

    @Override
    public boolean iterateNodes(IterateFunction function) {
        if (rootNode != null) {
            return rootNode.iterateNodes(function);
        }
        return false;
    }
    
    protected MerkleNode createNode(K entry) {
       return new MerkleNode<>(entry);
    }

    @Override
    public void removeAll() {
        // resetting the rootNode invalidates all references to child nodes and so forth
        // GC will remove entries when it next runs
        this.rootNode = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy