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

pingbu.search.IntIndex Maven / Gradle / Ivy

The newest version!
package pingbu.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数字搜索索引
 *
 * @author pingbu
 */
public class IntIndex implements SearchIndex {

    private final Map mIdToValues = new HashMap<>();
    private final Map> mValueToIds = new HashMap<>();

    @Override
    public void addItem(final int id, final String value) {
        final int intValue = Integer.parseInt(value);
        mIdToValues.put(id, intValue);
        List ids = mValueToIds.get(intValue);
        if (ids == null) {
            ids = new ArrayList<>();
            mValueToIds.put(intValue, ids);
        }
        ids.add(id);
    }

    @Override
    public SearchIndex.Iterator iterate(final String value) {
        if (value.startsWith("range:")) {
            final int p = value.indexOf(',', 7);
            int a = Integer.parseInt(value.substring(7, p));
            int b = Integer
                    .parseInt(value.substring(p + 1, value.length() - 1));
            if (value.charAt(6) == '(')
                ++a;
            if (value.endsWith(")"))
                --b;
            final List iterators = new ArrayList<>();
            for (int intValue = a; intValue <= b; ++intValue) {
                final List ids = mValueToIds.get(intValue);
                if (ids != null)
                    iterators.add(new ListIterator(ids));
            }
            return new MultiIterator(iterators);
        } else {
            final int intValue = Integer.parseInt(value);
            final List ids = mValueToIds.get(intValue);
            return new ListIterator(ids);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy