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

com.lambdazen.bitsy.index.BitsyIndex Maven / Gradle / Ivy

package com.lambdazen.bitsy.index;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.lambdazen.bitsy.ads.set.CompactSet;

public abstract class BitsyIndex {
    Map index;
    
    public BitsyIndex() {
        this.index = new ConcurrentHashMap();
    }
    
    public abstract Object getValue(T bean);
    public abstract T copy(T bean);

    public void load(Iterator initialContents) {
        while (initialContents.hasNext()) {
            T elem = initialContents.next();
            add(elem);
        }
    }
    
    public List get(Object value) {
        Object idxValue = index.get(value);

        if (idxValue == null) {
            return Collections.emptyList();
        } else {
            Object[] objs = CompactSet.getElements(idxValue);
            List ans = new ArrayList(objs.length);
            int len = objs.length;
            for (int i=0; i < len; i++) {
                // Always check for nulls on getElements() because reads don't acquire locks
                if (objs[i] != null) {
                    ans.add(copy((T)objs[i]));   
                }
            }
    
            return ans;
        }
    }
    
    public void add(T bean) {
        Object value = getValue(bean);
        if (value == null) {
            // Nothing to do
            return;
        }

        // No need to synchronize, because there is a read-write lock
        Object origSet = index.get(value);
        Object newSet = CompactSet.add(origSet, bean);

        if (origSet != newSet) {
            index.put(value, newSet);
        }
    }
    
    public void remove(T bean) {
        Object value = getValue(bean);
        if (value == null) {
            // Nothing to do
            return;
        }

        // No need to synchronize, because there is a read-write lock
        Object origSet = index.get(value);
        Object newSet = CompactSet.remove(origSet, bean);

        if (origSet != newSet) {
            if (newSet == null) {
                index.remove(value);
            } else {
                index.put(value, newSet);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy