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

org.datacleaner.storage.BerkeleyDbList Maven / Gradle / Ivy

/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Neopost - Customer Information Management
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.storage;

import java.util.AbstractList;
import java.util.List;
import java.util.Map;

/**
 * A map wrapped in the List interface. This is used because the berkeley db.
 * Does not support persistent lists, but only maps. Instead a persistent map is
 * wrapped by this List-implementation.
 */
final class BerkeleyDbList extends AbstractList implements List {

    private final BerkeleyDbMap _wrappedMap;

    public BerkeleyDbList(final BerkeleyDbMap map) {
        super();
        _wrappedMap = map;
    }

    public Map getWrappedMap() {
        return _wrappedMap;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        _wrappedMap.finalize();
    }

    @Override
    public E get(final int index) {
        if (!_wrappedMap.containsKey(index)) {
            throw new IndexOutOfBoundsException();
        }
        return _wrappedMap.get(index);
    }

    @Override
    public int size() {
        return _wrappedMap.size();
    }

    @Override
    public boolean add(final E element) {
        _wrappedMap.put(_wrappedMap.size(), element);
        return true;
    }

    @Override
    public void add(final int index, final E element) {
        if (!_wrappedMap.containsKey(index)) {
            throw new IndexOutOfBoundsException();
        }
        for (int i = _wrappedMap.size(); i > index; i--) {
            _wrappedMap.put(i, _wrappedMap.get(i - 1));
        }
        _wrappedMap.put(index, element);
    }

    @Override
    public E set(final int index, final E element) {
        if (!_wrappedMap.containsKey(index)) {
            throw new IndexOutOfBoundsException();
        }
        return _wrappedMap.put(index, element);
    }

    @Override
    public E remove(final int index) {
        if (!_wrappedMap.containsKey(index)) {
            throw new IndexOutOfBoundsException();
        }
        final E element = _wrappedMap.get(index);
        for (int i = index; i < _wrappedMap.size() - 1; i++) {
            _wrappedMap.put(i, _wrappedMap.get(i + 1));
        }
        _wrappedMap.remove(_wrappedMap.size() - 1);
        return element;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy