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

Alachisoft.NCache.Common.DataStructures.SortedMap Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.DataStructures;


import Alachisoft.NCache.Common.SearchAlgorithms.BinarySearch;

import java.util.*;

/**
 * @author Basit Anwer
 */
public class SortedMap implements Map {
    Comparator comp;
    //Maintains key order
    private LinkedList _linkList = new LinkedList();
    //HashMap for fastest fetch
    private HashMap> _hashMap;

    //private List _valueList = new ArrayList();
    public SortedMap(Comparator comp) {
        this.comp = comp;
        _hashMap = new HashMap>();
    }


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

    @Override
    public boolean isEmpty() {
        return _linkList.isEmpty();
    }

    @Override
    public boolean containsKey(Object o) {
        return _hashMap.containsKey(o);
    }

    @Override
    public boolean containsValue(Object o) {
        return _hashMap.containsValue(o);
    }

    @Override
    @Deprecated
    public V get(Object o) {
        throw new UnsupportedOperationException();
    }

    public List getValue(Object o) {
//         ArrayList arr = new ArrayList();
//         if (_hashMap.get(o) != null)
//            {
//                List intList=_hashMap.get(o);
//                for (int j = 0; j < intList.size(); j++)
//                {
//                    arr.add(intList.get(j));
//                }
//            }
//        return arr;
        return _hashMap.get(o);
    }

    @Override
    @Deprecated
    public V put(K k, V v) {
        throw new UnsupportedOperationException();
    }

    public List putValue(K k, V v) {
        List valueList = new ArrayList();
        sortKey(k);
        if (_hashMap.containsKey(k)) {
            valueList = _hashMap.get(k);
            valueList.add(v);
            return _hashMap.put(k, valueList);
        } else {
            valueList.add(v);
            return _hashMap.put(k, valueList);
        }
    }

    @Override
    @Deprecated
    public V remove(Object o) {
        throw new UnsupportedOperationException();
    }

    public List removeVal(Object o) {
        _linkList.remove(o);
        return _hashMap.remove(o);
    }

    @Override
    public void putAll(Map map) {
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            putValue(pair.getKey(), pair.getValue());
        }
    }

    @Override
    public void clear() {
        _linkList.clear();
        _hashMap.clear();
    }

    @Override
    public Set keySet() {
        LinkedHashSet link = new LinkedHashSet(_linkList);
        return link;
    }

    @Override
    public Collection values() {
        ArrayList arr = new ArrayList();
        for (int i = 0; i < _linkList.size(); i++) {
            if (_hashMap.get(_linkList.get(i)) != null) {
                List intList = _hashMap.get(_linkList.get(i));
                for (int j = 0; j < intList.size(); j++) {
                    arr.add(intList.get(j));
                }
            }
//            else
//               arr.add(_hashMap.get(_linkList.get(i)));
        }
        return arr;
    }

    @Override
    public Set> entrySet() {
        LinkedHashSet> link = new LinkedHashSet>();

        for (int i = 0; i < _linkList.size(); i++) {
            HashMap.SimpleEntry simple = new AbstractMap.SimpleEntry(_linkList.get(i), _hashMap.get(_linkList.get(i)));
            link.add(simple);
        }
        return link;
    }


    private void sortKey(Object o) {

        BinarySearch.insertItem(_linkList, o, comp);

        //
        //        if (comp != null)
        //        {
        //        }
        //        else
        //        {
        //            throw new IllegalArgumentException("Comparator is NUll");
        //        }
        //
        //        if(size() == 0)
        //        {
        //            _linkList.add((K)o);
        //            return;
        //        }
        //
        //        boolean insert = false;
        //        int itemNumber = _linkList.size() / 2;
        //        int less = 0;
        //        int greater = size()-1;
        //
        //        while (!insert)
        //        {
        //
        //            K key = _linkList.get(itemNumber);
        //            if (comp.compare((K) o, key) < 0) //Less than
        //            {
        //                if ((itemNumber - less) == 1)
        //                {
        //                    greater = itemNumber = less;
        //                    continue;
        //                }
        //                else if(itemNumber == less)
        //                {
        //                    _linkList.add(itemNumber, (K) o);
        //                    insert = true;
        //                    break;
        //                }
        //
        //                greater = itemNumber;
        //                itemNumber = (itemNumber - less) / 2 + less;
        //            }
        //            else if (comp.compare((K) o, key) > 0) //Greater than
        //            {
        //                if ((greater - itemNumber) == 1)
        //                {
        //                    less = itemNumber = greater;
        //                    continue;
        //                }
        //                else if(itemNumber == greater)
        //                {
        //                    _linkList.add(itemNumber+1, (K) o);
        //                    insert = true;
        //                    break;
        //                }
        //
        //                less = itemNumber;
        //                itemNumber = (greater - itemNumber) / 2 + itemNumber;
        //            }
        //            else
        //            {
        //                throw new IllegalArgumentException("Duplicate Key found");
        //            }
        //        }
        //

    }

    public V getByIndex(int index) {
        if (index >= size())
            throw new IndexOutOfBoundsException();
//         ArrayList arr = new ArrayList();
//         if (_hashMap.get(_linkList.get(index)) != null)
//            {
//                List intList=_hashMap.get(_linkList.get(index));
//                for (int j = 0; j < intList.size(); j++)
//                {
//                    arr.add(intList.get(j));
//                }
//            }
//        return arr;

        int currentIndex = 0;
        V value = null;
        ArrayList tempList = new ArrayList();
        Iterator ie = _linkList.iterator();
        while (ie.hasNext()) {
            Object tmpValue = ie.next();
            if (!tempList.contains(tmpValue))
                tempList.add(tmpValue);
        }
        ie = tempList.iterator();
        while (ie.hasNext()) {
            //for(int i =0; i< size(); i++){
            List list = _hashMap.get(ie.next());

            if (list != null && list.size() > 1) {
                for (int j = 0; j < list.size(); j++) {
                    value = list.get(j);
                    if (currentIndex == index)
                        return value;
                    currentIndex++;
                }
            } else {
                if (list != null && list.size() == 1)
                    value = list.get(0);
                if (currentIndex == index)
                    return value;

                currentIndex++;
            }


        }

        return null;


        //return _hashMap.get(_linkList.get(index));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy