Android Open Source - baracus-framework Lazy Collection






From Project

Back to project page baracus-framework.

License

The source code is released under:

Apache License

If you think the Android project baracus-framework 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 net.mantucon.baracus.orm;
//from  w ww  .ja  va  2 s .com
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: marcus
 * Date: 24.09.12
 * Time: 15:18
 * <p/>
 * Lazy collection implementation. This collection is fitted with and LazyLoader implementation
 * taking care of the load of the data on the first access to this collection.
 */
public class LazyCollection<T> implements List<T> {

    /**
     * Lazy Loader interface. Use this lazy loader in order to load the collection lazily after
     * the first access.
     *
     * @param <T>
     */
    public static interface LazyLoader<T> {
        public List<T> loadReference();
    }


    static enum CollectionState {
        Armed,
        Loaded
    }

    private CollectionState collectionState = CollectionState.Armed;

    final LazyLoader<T> lazyLoader;

    /**
     * Constructor. A LazyCollection must be fitted with a lazy loading helper
     *
     * @param lazyLoader - the component managing the load of the collection on first access.
     */
    public LazyCollection(LazyLoader<T> lazyLoader) {
        this.lazyLoader = lazyLoader;
    }


    private ArrayList<T> referencedData = new ArrayList<T>();

    /**
     * Helper function checking if a lazy load has to be performed.
     * <p/>
     * If a lazy loader is armed, it will call the loadReference function on the
     * lazy loader on first access.
     */
    private void checkReferencedData() {
        synchronized (collectionState) {
            if (collectionState == CollectionState.Armed) {
                collectionState = CollectionState.Loaded;
                referencedData.addAll(lazyLoader.loadReference());
            }
        }
    }


    public boolean add(T object) {
        checkReferencedData();
        return referencedData.add(object);
    }

    public boolean addAll(int index, Collection<? extends T> collection) {
        checkReferencedData();
        return referencedData.addAll(index, collection);
    }

    public List<T> subList(int start, int end) {
        checkReferencedData();
        return referencedData.subList(start, end);
    }

    public T remove(int index) {
        checkReferencedData();
        return referencedData.remove(index);
    }

    public int lastIndexOf(Object object) {
        checkReferencedData();
        return referencedData.lastIndexOf(object);
    }

    public int size() {
        checkReferencedData();
        return referencedData.size();
    }

    public boolean contains(Object object) {
        checkReferencedData();
        return referencedData.contains(object);
    }

    public T get(int index) {
        checkReferencedData();
        return referencedData.get(index);
    }

    public void ensureCapacity(int minimumCapacity) {
        checkReferencedData();
        referencedData.ensureCapacity(minimumCapacity);
    }

    public void add(int index, T object) {
        checkReferencedData();
        referencedData.add(index, object);
    }

    public Iterator<T> iterator() {
        checkReferencedData();
        return referencedData.iterator();
    }

    public ListIterator<T> listIterator(int location) {
        checkReferencedData();
        return referencedData.listIterator(location);
    }

    public boolean containsAll(Collection<?> collection) {
        checkReferencedData();
        return referencedData.containsAll(collection);
    }

    public <T> T[] toArray(T[] contents) {
        checkReferencedData();
        return referencedData.toArray(contents);
    }

    public void clear() {
        checkReferencedData();
        referencedData.clear();
    }

    public boolean addAll(Collection<? extends T> collection) {
        checkReferencedData();
        return referencedData.addAll(collection);
    }

    public ListIterator<T> listIterator() {
        checkReferencedData();
        return referencedData.listIterator();
    }

    public boolean removeAll(Collection<?> collection) {
        checkReferencedData();
        return referencedData.removeAll(collection);
    }

    public void trimToSize() {
        checkReferencedData();
        referencedData.trimToSize();
    }

    public int indexOf(Object object) {
        checkReferencedData();
        return referencedData.indexOf(object);
    }

    public Object[] toArray() {
        checkReferencedData();
        return referencedData.toArray();
    }

    public T set(int index, T object) {
        checkReferencedData();
        return referencedData.set(index, object);
    }

    public boolean retainAll(Collection<?> collection) {
        checkReferencedData();
        return referencedData.retainAll(collection);
    }

    public boolean isEmpty() {
        checkReferencedData();
        return referencedData.isEmpty();
    }

    public boolean remove(Object object) {
        checkReferencedData();
        return referencedData.remove(object);
    }
}




Java Source Code List

net.mantucon.baracus.annotations.Bean.java
net.mantucon.baracus.context.AnnotationScanner.java
net.mantucon.baracus.context.BaracusApplicationContext.java
net.mantucon.baracus.context.BeanContainer.java
net.mantucon.baracus.context.Exceptions.java
net.mantucon.baracus.context.ManagedActivity.java
net.mantucon.baracus.context.ManagedFragment.java
net.mantucon.baracus.dao.BaracusOpenHelper.java
net.mantucon.baracus.dao.BaseDao.java
net.mantucon.baracus.dao.ConfigurationDao.java
net.mantucon.baracus.errorhandling.CustomErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandlingFactory.java
net.mantucon.baracus.errorhandling.ErrorSeverity.java
net.mantucon.baracus.errorhandling.StandardErrorHandler.java
net.mantucon.baracus.errorhandling.TextEditErrorHandler.java
net.mantucon.baracus.lifecycle.ApplicationContextInitializer.java
net.mantucon.baracus.lifecycle.Destroyable.java
net.mantucon.baracus.lifecycle.Initializeable.java
net.mantucon.baracus.migr8.MigrationStep.java
net.mantucon.baracus.migr8.ModelVersion000.java
net.mantucon.baracus.model.ConfigurationParameter.java
net.mantucon.baracus.orm.AbstractModelBase.java
net.mantucon.baracus.orm.FieldList.java
net.mantucon.baracus.orm.Field.java
net.mantucon.baracus.orm.Identifiable.java
net.mantucon.baracus.orm.LazyCollection.java
net.mantucon.baracus.orm.LazyMorphicCollection.java
net.mantucon.baracus.orm.LazyReference.java
net.mantucon.baracus.orm.LegacyModelBase.java
net.mantucon.baracus.orm.ModelBase.java
net.mantucon.baracus.orm.NullReference.java
net.mantucon.baracus.orm.ObjectReference.java
net.mantucon.baracus.orm.OptimisticLockingModelBase.java
net.mantucon.baracus.orm.OptmisticLocking.java
net.mantucon.baracus.orm.ReferenceLoader.java
net.mantucon.baracus.orm.Reference.java
net.mantucon.baracus.orm.Timestamped.java
net.mantucon.baracus.signalling.ConfigurationChangeListener.java
net.mantucon.baracus.signalling.DataChangeAwareComponent.java
net.mantucon.baracus.signalling.DataSetChangeAwareComponent.java
net.mantucon.baracus.signalling.DeleteAwareComponent.java
net.mantucon.baracus.signalling.GenericEventAwareComponent.java
net.mantucon.baracus.signalling.GenericEvent.java
net.mantucon.baracus.ui.ConstrainedEditText.java
net.mantucon.baracus.ui.ConstrainedSpinner.java
net.mantucon.baracus.ui.ErrorView.java
net.mantucon.baracus.ui.Popup.java
net.mantucon.baracus.util.DBBackup.java
net.mantucon.baracus.util.DataUtil.java
net.mantucon.baracus.util.DateUtil.java
net.mantucon.baracus.util.DayDate.java
net.mantucon.baracus.util.Logger.java
net.mantucon.baracus.util.StringUtil.java
net.mantucon.baracus.validation.AbstractValidator.java
net.mantucon.baracus.validation.ConstrainedView.java
net.mantucon.baracus.validation.ValidatableView.java
net.mantucon.baracus.validation.ValidationFactory.java
net.mantucon.baracus.validation.Validator.java
net.mantucon.baracus.validation.builtins.DateFromNow.java
net.mantucon.baracus.validation.builtins.NumberMustBeGreaterThanZero.java
net.mantucon.baracus.validation.builtins.StringIsNumericDouble.java
net.mantucon.baracus.validation.builtins.StringIsNumericInteger.java
net.mantucon.baracus.validation.builtins.StringNotEmpty.java