Android Open Source - FB Notify List






From Project

Back to project page FB.

License

The source code is released under:

Copyright (c) 2014, hydraulic All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Re...

If you think the Android project FB 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.hydra.mainthreadlist;
/* ww w .ja  v a 2  s  .  c  o  m*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

import android.os.Looper;

public class NotifyList<E> extends ArrayList<E> {
  
  private static final long serialVersionUID = 6933912252105668830L;
  
  private transient ArrayList<NotifyListObserver<E>> mObservers;
  
  private transient AtomicBoolean mIsHandle;
  
  public NotifyList() {
    super();
    
    init();
  }

  public NotifyList(Collection<? extends E> collection) {
    super(collection); 
    
    init();
  }

  public NotifyList(int capacity) {
    super(capacity);
    
    init();
  }
  
  private void init() {
    mIsHandle = new AtomicBoolean(false);
    
    mObservers = new ArrayList<NotifyListObserver<E>>();
  }

  @Override
  public boolean add(E object) {
    checkThread("add");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean suc = super.add(object);
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return suc;
      }
    }
  }
  
  @Override
  public void add(int index, E object) {
    checkThread("add");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        super.add(index, object);
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return;
      }
    }
  }
  
  @Override
  public E get(int index) {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        E e = super.get(index);
        mIsHandle.getAndSet(false);
        
        return e;
      }
    }
  }
  
  @Override
  public int size() {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        int i = super.size();
        mIsHandle.getAndSet(false);
        
        return i;
      }
    }
  }
  
  @Override
  public boolean isEmpty() {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean isEmpty = super.isEmpty();
        mIsHandle.getAndSet(false);
        
        return isEmpty;
      }
    }
  }
  
  @Override
  public boolean contains(Object object) {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean contains = super.contains(object);
        mIsHandle.getAndSet(false);
        
        return contains;
      }
    }
  }
  
  @Override
  public boolean containsAll(Collection<?> collection) {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean contains = super.containsAll(collection);
        mIsHandle.getAndSet(false);
        
        return contains;
      }
    }
  }

  @Override
  public Object[] toArray() {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        Object[] arrayE = super.toArray();
        mIsHandle.getAndSet(false);
        
        return arrayE;
      }
    }
  }
  
  @Override
  public <T> T[] toArray(T[] contents) {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        T[] arrayT = super.toArray(contents);
        mIsHandle.getAndSet(false);
        
        return arrayT;
      }
    }
  }
  
  @Override
  public E remove(int index) {
    checkThread("remove");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        
        E e = super.remove(index);
        
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return e;
      }
    }
  }
  
  @Override
  public boolean remove(Object object) {
    checkThread("remove");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean suc = super.remove(object);
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return suc;
      }
    }
  }
  
  @Override
  public int indexOf(Object object) {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        int index = super.indexOf(object);
        mIsHandle.getAndSet(false);
        
        return index;
      }
    }
  }
  
  @Override
  public int lastIndexOf(Object object) {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        int lastIndex = super.lastIndexOf(object);
        mIsHandle.getAndSet(false);
        
        return lastIndex;
      }
    }
  }
  
  @Override
  public Iterator<E> iterator() {
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        Iterator<E> ite = super.iterator();
        mIsHandle.getAndSet(false);
        
        return ite;
      }
    }
  }
  
  @Override
  public void clear() {
    checkThread("clear");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        super.clear();
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return;
      }
    }
  }
  
  @Override
  public boolean addAll(Collection<? extends E> collection) {
    checkThread("addAll");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean suc = super.addAll(collection);
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return suc;
      }
    }
  }
  
  @Override
  public boolean addAll(int index, Collection<? extends E> collection) {
    checkThread("addAll");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        boolean suc = super.addAll(index, collection);
        mIsHandle.getAndSet(false);
        
        notifyListChanged();
        
        return suc;
      }
    }
  }
  
  @Override
  public E set(int index, E object) {
    checkThread("set");
    
    for(;;) {
      if(mIsHandle.compareAndSet(false, true)) {
        try {
          E e = super.set(index, object);
          mIsHandle.getAndSet(false);
          
          notifyListChanged();
          
          return e;
        } catch (IndexOutOfBoundsException e) {
          mIsHandle.getAndSet(false);
          
              throw new IndexOutOfBoundsException(e.toString());
        }
      }
    }
  }
  
  private void checkThread(String action) {
    if(Looper.getMainLooper() != Looper.myLooper()) {
      throw new IllegalStateException("don't " + action + " data in List which UI " +
          "ListView may be use in a Non-MainThread ID:" 
          + Thread.currentThread().getId() + " name:" + getClass());
    }
  }

  private void notifyListChanged() {
    for(NotifyListObserver<E> observer : mObservers) {
      observer.notifyListChanged();
    }
  }
  
  public void registerObserver(NotifyListObserver<E> observer) {
        if(observer != null) {
          mObservers.add(observer);
          
            observer.setList(this);
        }
    }

    public void unregisterObserver(NotifyListObserver<E> observer) {
      mObservers.remove(observer);
    }
    
    public void unregisterAll() {
      mObservers.clear();
    }
    
  private void readObject(ObjectInputStream stream) throws IOException,
      ClassNotFoundException {
    stream.defaultReadObject();
    
    init();
  }
}




Java Source Code List

com.hydra.mainthreadlist.MainActivity.java
com.hydra.mainthreadlist.NotifyListObserver.java
com.hydra.mainthreadlist.NotifyList.java