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

com.tinkerpop.blueprints.impls.tg.TinkerIndex Maven / Gradle / Ivy

The newest version!
package com.tinkerpop.blueprints.impls.tg;

import com.tinkerpop.blueprints.CloseableIterable;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Index;
import com.tinkerpop.blueprints.util.StringFactory;
import com.tinkerpop.blueprints.util.WrappingCloseableIterable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
class TinkerIndex implements Index, Serializable {

    protected Map>> index = new HashMap>>();
    protected final String indexName;
    protected final Class indexClass;

    public TinkerIndex(final String indexName, final Class indexClass) {
        this.indexName = indexName;
        this.indexClass = indexClass;
    }

    public String getIndexName() {
        return this.indexName;
    }

    public Class getIndexClass() {
        return this.indexClass;
    }

    public void put(final String key, final Object value, final T element) {
        Map> keyMap = this.index.get(key);
        if (keyMap == null) {
            keyMap = new HashMap>();
            this.index.put(key, keyMap);
        }
        Set objects = keyMap.get(value);
        if (null == objects) {
            objects = new HashSet();
            keyMap.put(value, objects);
        }
        objects.add(element);

    }

    public CloseableIterable get(final String key, final Object value) {
        final Map> keyMap = this.index.get(key);
        if (null == keyMap) {
            return new WrappingCloseableIterable((Iterable) Collections.emptyList());
        } else {
            Set set = keyMap.get(value);
            if (null == set)
                return new WrappingCloseableIterable((Iterable) Collections.emptyList());
            else
                return new WrappingCloseableIterable(new ArrayList(set));
        }
    }

    public CloseableIterable query(final String key, final Object query) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    public long count(final String key, final Object value) {
        final Map> keyMap = this.index.get(key);
        if (null == keyMap) {
            return 0;
        } else {
            Set set = keyMap.get(value);
            if (null == set)
                return 0;
            else
                return set.size();
        }
    }

    public void remove(final String key, final Object value, final T element) {
        final Map> keyMap = this.index.get(key);
        if (null != keyMap) {
            Set objects = keyMap.get(value);
            if (null != objects) {
                objects.remove(element);
                if (objects.size() == 0) {
                    keyMap.remove(value);
                }
            }
        }
    }

    public void removeElement(final T element) {
        if (this.indexClass.isAssignableFrom(element.getClass())) {
            for (Map> map : index.values()) {
                for (Set set : map.values()) {
                    set.remove(element);
                }
            }
        }
    }

    public String toString() {
        return StringFactory.indexString(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy