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

com.hazelcast.collection.list.ListContainer Maven / Gradle / Ivy

There is a newer version: 5.0-BETA-1
Show newest version
/*
 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.collection.list;

import com.hazelcast.collection.CollectionContainer;
import com.hazelcast.collection.CollectionItem;
import com.hazelcast.config.ListConfig;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.NodeEngine;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

public class ListContainer extends CollectionContainer {

    private static final int INITIAL_CAPACITY = 1000;
    private List itemList;
    private ListConfig config;

    public ListContainer() {
    }

    public ListContainer(String name, NodeEngine nodeEngine) {
        super(name, nodeEngine);
    }

    @Override
    public ListConfig getConfig() {
        if (config == null) {
            config = nodeEngine.getConfig().findListConfig(name);
        }
        return config;
    }

    protected CollectionItem add(int index, Data value) {
        final CollectionItem item = new CollectionItem(nextId(), value);
        if (index < 0) {
            return getCollection().add(item) ? item : null;
        } else {
            getCollection().add(index, item);
            return item;
        }
    }

    protected CollectionItem get(int index) {
        return getCollection().get(index);
    }

    protected CollectionItem set(int index, long itemId, Data value) {
        return getCollection().set(index, new CollectionItem(itemId, value));
    }

    protected void setBackup(long oldItemId, long itemId, Data value) {
        getMap().remove(oldItemId);
        getMap().put(itemId, new CollectionItem(itemId, value));

    }

    protected CollectionItem remove(int index) {
        return getCollection().remove(index);
    }

    protected int indexOf(boolean last, Data value) {
        final List list = getCollection();
        if (last) {
            int index = list.size();
            final ListIterator iterator = list.listIterator(index);
            while (iterator.hasPrevious()) {
                final CollectionItem item = iterator.previous();
                index--;
                if (value.equals(item.getValue())) {
                    return index;
                }
            }
        } else {
            int index = -1;
            for (CollectionItem item : list) {
                index++;
                if (value.equals(item.getValue())) {
                    return index;
                }
            }
        }
        return -1;
    }

    protected Map addAll(int index, List valueList) {
        final int size = valueList.size();
        final Map map = new HashMap(size);
        List list = new ArrayList(size);
        for (Data value : valueList) {
            final long itemId = nextId();
            list.add(new CollectionItem(itemId, value));
            map.put(itemId, value);
        }
        getCollection().addAll(index, list);

        return map;
    }

    protected List sub(int from, int to) {
        final List list;
        if (from == -1 && to == -1) {
            list = getCollection();
        } else {
            list = getCollection().subList(from, to);
        }
        final ArrayList sub = new ArrayList(list.size());
        for (CollectionItem item : list) {
            sub.add((Data) item.getValue());
        }
        return sub;
    }

    @Override
    public List getCollection() {
        if (itemList == null) {
            if (itemMap != null && !itemMap.isEmpty()) {
                itemList = new ArrayList(itemMap.values());
                Collections.sort(itemList);
                itemMap.clear();
            } else {
                itemList = new ArrayList(INITIAL_CAPACITY);
            }
            itemMap = null;
        }
        return itemList;
    }

    @Override
    protected Map getMap() {
        if (itemMap == null) {
            if (itemList != null && !itemList.isEmpty()) {
                itemMap = new HashMap(itemList.size());
                for (CollectionItem item : itemList) {
                    itemMap.put(item.getItemId(), item);
                }
                itemList.clear();
            } else {
                itemMap = new HashMap(INITIAL_CAPACITY);
            }
            itemList = null;
        }
        return itemMap;
    }

    @Override
    protected void onDestroy() {
        if (itemList != null) {
            itemList.clear();
        }
        if (itemMap != null) {
            itemMap.clear();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy