Android Open Source - android-gskbyte-utils Array Hash Map






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.gskbyte.collection;
/*from   ww  w  .j a  v a2s  .co  m*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableList;

public class ArrayHashMap<K,V>
extends HashMap<K, V>
implements Iterable<V>
{

private static final long serialVersionUID = -3578702226046722524L;

private final List<K> keyList;

public ArrayHashMap()
{
    keyList = new ArrayList<K>();
}

public ArrayHashMap(int capacity)
{
    super(capacity);
    keyList = new ArrayList<K>(capacity);
}

public ArrayHashMap(int capacity, float loadFactor)
{
    super(capacity, loadFactor);
    keyList = new ArrayList<K>(capacity);
}

public ArrayHashMap(Map<K,V> map)
{
    super(map.size());
    keyList = new ArrayList<K>(map.size());
    constructorPutAll(map);
}

// TODO add other constructors

public K keyAt(int location)
{ return keyList.get(location); }

public V getAt(int location)
{ return get( keyList.get(location) ); }

// adds at the end of the array
@Override
public V put(K key, V value)
{
    V oldValue = super.put(key, value);
    if(oldValue == null) {
        keyList.add(key);
    }
    return oldValue;
}

@Override
public void putAll(Map<? extends K, ? extends V> map)
{
    Collection<? extends K> keys = null;
    if( (map instanceof ArrayHashMap<?, ?>) ) {
        keys = ((ArrayHashMap<? extends K, ? extends V>) map).keyList();
    } else {
        keys = map.keySet();
    }
    
    // add non-existant entries at the end
    for(K key : keys) {
        if( ! containsKey(key) ) {
            put(key, map.get(key));
        }
    }
}


@Override
public Set< Map.Entry<K, V> > entrySet()
{
    LinkedHashSet< Map.Entry<K,V> > ret = new LinkedHashSet< Map.Entry<K,V>>( size() );
    for(K k : keyList) {
        ret.add( new Entry(k) );
    }
    return ret;
}

@Override
public Set<K> keySet()
{ return new LinkedHashSet<K>(keyList); }

// to provide faster support tu subclasses
protected List<K> unmodifiableKeyList()
{ return Collections.unmodifiableList(keyList); }

public List<K> keyList()
{ return ImmutableList.copyOf(keyList); }

@Override
public Collection<V> values()
{ return valuesList(); }

public List<V> valuesList()
{
    ArrayList<V> values = new ArrayList<V>( size() );
    for(K k : keyList) {
        values.add( get(k) );
    }
    return values;
}

// false if already existing, does nothing
public boolean add(K key, V value)
{
    if( ! containsKey(key) ) {
        keyList.add(key);
        super.put(key, value);
        return true;
    } else {
        return false;
    }
}

// true if added OR changed, false if element found and stays in same position
public boolean add(int index, K key, V value)
{
    if( containsKey(key) ) {
        int existingIndex = keyList.indexOf(key);
        if(existingIndex != index) {
            keyList.set(existingIndex, keyList.set(index, keyList.get(existingIndex)));
            return true;
        } else {
            return false;
        }
    } else {
        keyList.add(index, key);
        super.put(key, value);
        return true;
    }
}

@Override
public void clear()
{
    super.clear();
    keyList.clear();
}

private void constructorPutAll(Map<K,V> origin)
{
    super.putAll(origin);
    if(origin instanceof ArrayHashMap) {
        ArrayHashMap<K,V> a = (ArrayHashMap<K, V>)origin;
        this.keyList.addAll(a.keyList);
    } else {
        this.keyList.addAll(origin.keySet()); // keeps order if linkedhashmap
    }
}

@SuppressWarnings("unchecked")
@Override
public Object clone()
{
    ArrayHashMap<K, V> copy;
    try {
        copy = this.getClass().newInstance();
        copy.constructorPutAll(this);
        return copy;
    } catch (InstantiationException e) {
        // won't happen
    } catch (IllegalAccessException e) {
        // won't happen
    }
    return null;
}

@Override
public boolean equals(Object object)
{
    if(super.equals(object)) {
        if(object instanceof ArrayHashMap) {
            ArrayHashMap<?, ?> a = (ArrayHashMap<?, ?>)object;
            return keyList.equals( a.keyList );
        }
    }
    
    return false;
}

@Override
public V remove(Object key)
{
    V removed = super.remove(key);
    if( removed != null) {
        keyList.remove(key);
    }
    return removed;
}

public V removeAt(int location)
{
    K key = keyList.remove(location);
    V value = super.remove(key);
    return value;
}


// commented out because it should return a VIEW of this map, not a copy!
/*
public ArrayHashMap<K, V> subArrayHashMap(int fromIndex, int toIndex)
{
    ArrayHashMap<K, V> ret = new ArrayHashMap<K, V>(toIndex-fromIndex);
    for(int i=fromIndex; i<toIndex; ++i) {
        K key = keyList.get(i);
        ret.put(key, get(key)); // some kind of putNoCheck() method would accelerate this
    }
    return ret;
}*/

@Override
public Iterator<V> iterator()
{ return valueIterator(); }

public ListIterator<V> listIterator()
{ return valueIterator(); }

public ListIterator<V> listIterator(int index)
{ return valueIterator(index); }

public ListIterator<K> keyIterator()
{ return keyList.listIterator(); }

public ListIterator<K> keyIterator(int index)
{ return keyList.listIterator(index); }

public ListIterator<V> valueIterator()
{ return new ValueIterator(); }

public ListIterator<V> valueIterator(int index)
{ return new ValueIterator(index); }

private final class ValueIterator
implements ListIterator<V>
{
    protected int index = -1;

    protected ValueIterator()
    { this.index = -1; }
    
    protected ValueIterator(int startIndex)
    { this.index = startIndex-1;}
    
    @Override
    public boolean hasNext()
    { return (index < size()-1); }

    @Override
    public V next()
    {
        ++index;
        return getAt(index);
    }

    @Override
    public void remove()
    { removeAt(index); }

    @Override
    public void add(V object)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean hasPrevious()
    { return index>0;}

    @Override
    public int nextIndex()
    { return index+1;}

    @Override
    public V previous()
    {
        --index;
        return getAt(index);
    }

    @Override
    public int previousIndex()
    { return index-1; }

    @Override
    public void set(V object)
    { put(keyList.get(index), object); }
}


private final class Entry
implements Map.Entry<K, V>
{
public final K key;

public Entry(K key)
{ this.key = key; }

public K getKey()
{ return key; }

public V getValue()
{ return get(key); }

public V setValue(V v)
{ return put(key, v); }
}


}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java