Android Open Source - sres Change Tracker






From Project

Back to project page sres.

License

The source code is released under:

Apache License

If you think the Android project sres 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 me.tatarka.sres;
/*from  w w  w .jav a 2s .  c  o  m*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Tracks changes for some set of trackables. Any trackables that want to be tracked must call
 *
 * @{link ChangeTracker#track}. @{link me.tatarka.sres.Property} and it's relatives all call this
 * when you get their value.
 */
public class ChangeTracker {
    private static ThreadLocal<Boolean> isTracking = new ThreadLocal<Boolean>() {
        @Override
        protected Boolean initialValue() {
            return false;
        }
    };
    private static ThreadLocal<List<Trackable>> trackedObservables = new ThreadLocal<List<Trackable>>() {
        @Override
        protected List<Trackable> initialValue() {
            return new ArrayList<>();
        }
    };

    private Map<Trackable.Listener, List<Trackable>> observableMap = new HashMap<Trackable.Listener, List<Trackable>>();

    private static void startTracking() {
        isTracking.set(true);
    }

    private static List<Trackable> stopTracking() {
        isTracking.set(false);
        List<Trackable> properties = new ArrayList<Trackable>(trackedObservables.get());
        trackedObservables.get().clear();
        return properties;
    }

    /**
     * Notifies that the given trackable wants to be tracked. This should be called when a listener
     * is registered on #{addListener}. For example, a @{link Property} calls it on @{link
     * Property#get}.
     *
     * @param trackable the trackable to track
     */
    public static void track(Trackable trackable) {
        if (isTracking.get()) trackedObservables.get().add(trackable);
    }

    /**
     * Clears all registered listeners so they will no longer be called when the trackable changes.
     */
    public void clear() {
        ensureMainThread("clear");

        for (Map.Entry<Trackable.Listener, List<Trackable>> entry : observableMap.entrySet()) {
            for (Trackable trackable : entry.getValue()) trackable.removeListener(entry.getKey());
        }
        observableMap.clear();
    }

    /**
     * Adds the listener. The listener will immediately be called to discover which trackables it
     * tracks. Then it will be called every time any of it's trackables change.
     *
     * @param listener the listener
     */
    public void addListener(Trackable.Listener listener) {
        ensureMainThread("addListener");

        startTracking();
        listener.onChange();
        List<Trackable> newEntry = stopTracking();
        List<Trackable> oldEntry = observableMap.put(listener, newEntry);
        if (oldEntry != null) {
            for (Trackable trackable : oldEntry) trackable.removeListener(listener);
        }
        for (Trackable trackable : newEntry) trackable.addListener(listener);
    }

    /**
     * Removes the listener. It will no longer be called when it's trackables change.
     *
     * @param listener the listener
     */
    public void removeListener(Trackable.Listener listener) {
        ensureMainThread("removeListener");

        List<Trackable> entry = observableMap.remove(listener);
        if (entry != null) {
            for (Trackable trackable : entry) trackable.removeListener(listener);
        }
    }

    private void ensureMainThread(String name) {
        if (!ThreadHandlerProvider.getDefault().isOnMainThread()) {
            throw new IllegalStateException("ChangeTracker" + name + "() must be called on the UI thread");
        }
    }
}




Java Source Code List

me.tatarka.sres.AbstractTrackable.java
me.tatarka.sres.Bindable.java
me.tatarka.sres.ChangeTracker.java
me.tatarka.sres.FileFilter.java
me.tatarka.sres.LayoutGenerator.java
me.tatarka.sres.LayoutParser.java
me.tatarka.sres.LayoutSelectorHelper.java
me.tatarka.sres.LayoutSelector.java
me.tatarka.sres.ListView.java
me.tatarka.sres.Main.java
me.tatarka.sres.ObservableAdapter.java
me.tatarka.sres.ObservableArrayList.java
me.tatarka.sres.ObservableListViewHelper.java
me.tatarka.sres.ObservableList.java
me.tatarka.sres.ObservablePagerAdapter.java
me.tatarka.sres.SResBaseVisitor.java
me.tatarka.sres.SResLexer.java
me.tatarka.sres.SResOutput.java
me.tatarka.sres.SResParser.java
me.tatarka.sres.SResVisitor.java
me.tatarka.sres.SRes.java
me.tatarka.sres.SourceInfo.java
me.tatarka.sres.ThreadHandlerImpl.java
me.tatarka.sres.ThreadHandlerProvider.java
me.tatarka.sres.ThreadHandler.java
me.tatarka.sres.Trackable.java
me.tatarka.sres.ViewPager.java
me.tatarka.sres.ast.Attribute.java
me.tatarka.sres.ast.Binding.java
me.tatarka.sres.ast.Child.java
me.tatarka.sres.ast.Converter.java
me.tatarka.sres.ast.Include.java
me.tatarka.sres.ast.RootView.java
me.tatarka.sres.ast.View.java
me.tatarka.sres.idea.SResFileTypeFactory.java
me.tatarka.sres.idea.SResFileType.java
me.tatarka.sres.idea.SResFile.java
me.tatarka.sres.idea.SResIcons.java
me.tatarka.sres.idea.SResLanguage.java
me.tatarka.sres.idea.SResLexer.java
me.tatarka.sres.idea.SResParserDefinition.java
me.tatarka.sres.idea._SResLexer.java
me.tatarka.sres.idea.psi.SResElementType.java
me.tatarka.sres.idea.psi.SResTokenType.java
me.tatarka.sres.impl.SResCodeLayoutGenerator.java
me.tatarka.sres.impl.SResLayoutParser.java
me.tatarka.sres.impl.SResVisitor.java
me.tatarka.sres.impl.SResXmlLayoutGenerator.java
me.tatarka.sres.impl.WriterCodeWriter.java
me.tatarka.sres.property.BooleanProperty.java
me.tatarka.sres.property.ByteProperty.java
me.tatarka.sres.property.CharProperty.java
me.tatarka.sres.property.DoubleProperty.java
me.tatarka.sres.property.FloatProperty.java
me.tatarka.sres.property.IntProperty.java
me.tatarka.sres.property.LongProperty.java
me.tatarka.sres.property.Property.java
me.tatarka.sres.property.ShortProperty.java
me.tatarka.sres.sample.MainActivity.java
me.tatarka.sres.sample.MyListItemView.java
me.tatarka.sres.sample.MyListItem.java
me.tatarka.sres.sample.MyMainView.java
me.tatarka.sres.sample.MyModel.java
me.tatarka.sres.sample.MyView.java
me.tatarka.sres.util.PathTransformer.java