Android Open Source - mobilib Mbl Weak Array List






From Project

Back to project page mobilib.

License

The source code is released under:

MIT License

If you think the Android project mobilib 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 com.datdo.mobilib.event;
// w  w  w.jav  a2 s  .co  m
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

class MblWeakArrayList<T> {
    private List<WeakReference<T>> mData = new Vector<WeakReference<T>>();

    public MblWeakArrayList() {}

    public MblWeakArrayList(MblWeakArrayList<T> other) {
        mData.clear();
        mData.addAll(other.mData);
    }

    public void add(T item) {
        if (item == null) return;

        synchronized (mData) {
            if (!__contains(item)) {
                mData.add(new WeakReference<T>(item));
            }
        }
    }

    public void remove(T item) {
        if (item == null) return;

        synchronized (mData) {
            int index = __indexOf(item);
            if (index >= 0) {
                mData.remove(index);
            }
        }
    }

    public boolean contains(T item) {
        synchronized (mData) {
            return __contains(item);
        }
    }

    public boolean isEmpty() {
        synchronized (mData) {
            __flush();
            return mData.isEmpty();
        }
    }

    public void iterateWithCallback(MblWeakArrayListCallback<T> cb) {
        if (cb == null) return;

        synchronized (mData) {
            __flush();
            for (WeakReference<T> ref : mData) {
                T item = ref.get();
                if (item != null) cb.onInterate(item);
            }
        }
    }

    @Deprecated
    public Iterator<T> iterate() {
        return null; // not yet used
    }

    public static interface MblWeakArrayListCallback<T> {
        public void onInterate(T item);
    }

    private boolean __contains(T item) {
        return __indexOf(item) >= 0;
    }

    private int __indexOf(T item) {
        if (item == null) return -1;

        int i = 0;
        for (WeakReference<T> anItem : mData) {
            if (anItem.get() == item) {
                return i;
            }
            i++;
        }

        return -1;
    }

    private void __flush() {
        List<WeakReference<T>> needToRemove = new ArrayList<WeakReference<T>>();
        for (WeakReference<T> anItem : mData) {
            if (anItem.get() == null) {
                needToRemove.add(anItem);
            }
        }
        mData.removeAll(needToRemove);
    }
}




Java Source Code List

com.datdo.mobilib.api.DBBase.java
com.datdo.mobilib.api.DBHelper.java
com.datdo.mobilib.api.MblApi.java
com.datdo.mobilib.api.MblCache.java
com.datdo.mobilib.api.MblException.java
com.datdo.mobilib.api.MblSSLCertificateUtils.java
com.datdo.mobilib.base.MblActivityPlugin.java
com.datdo.mobilib.base.MblBaseActionBarActivity.java
com.datdo.mobilib.base.MblBaseActivity.java
com.datdo.mobilib.base.MblBaseAdapter.java
com.datdo.mobilib.base.MblBaseApplication.java
com.datdo.mobilib.base.MblBaseFragmentActivity.java
com.datdo.mobilib.base.MblDecorView.java
com.datdo.mobilib.base.MblNetworkStatusChangedReceiver.java
com.datdo.mobilib.event.MblCommonEvents.java
com.datdo.mobilib.event.MblEventCenter.java
com.datdo.mobilib.event.MblEventListener.java
com.datdo.mobilib.event.MblStrongEventListener.java
com.datdo.mobilib.event.MblWeakArrayList.java
com.datdo.mobilib.imageinput.MblAutoResizeSquareImageView.java
com.datdo.mobilib.imageinput.MblDataInputActivity.java
com.datdo.mobilib.imageinput.MblImageFolderScanner.java
com.datdo.mobilib.imageinput.MblImageInput.java
com.datdo.mobilib.imageinput.MblImagePickingScanEngine.java
com.datdo.mobilib.imageinput.MblPickImageActivity.java
com.datdo.mobilib.imageinput.MblPickImageGridViewAdapter.java
com.datdo.mobilib.imageinput.MblTakeImageActivity.java
com.datdo.mobilib.util.MblAsyncTask.java
com.datdo.mobilib.util.MblImageLoader.java
com.datdo.mobilib.util.MblLinkMovementMethod.java
com.datdo.mobilib.util.MblUrlRecognizer.java
com.datdo.mobilib.util.MblUtils.java
com.datdo.mobilib.widget.MblHorizontalViewPager.java
com.datdo.mobilib.widget.MblListViewWithScrollableItems.java
com.datdo.mobilib.widget.MblSequenceImage.java
com.datdo.mobilib.widget.MblSideMenuEnabledLayout.java
com.datdo.mobilib.widget.MblTouchImageView.java