Android Open Source - SurgeTracker Aggregate Collection






From Project

Back to project page SurgeTracker.

License

The source code is released under:

MIT License

If you think the Android project SurgeTracker 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.bklimt.surgetracker.model;
/*from   w w  w  .  j a  v  a 2 s .  c  om*/
import com.bklimt.surgetracker.backbone.Collection;
import com.bklimt.surgetracker.backbone.CollectionListener;
import com.bklimt.surgetracker.backbone.Visitor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

/**
 * Created by klimt on 7/2/14.
 */
public class AggregateCollection extends Collection<Aggregate> {
    private SurgeCollection surges;

    public AggregateCollection(SurgeCollection newSurges) {
        surges = newSurges;

        surges.addListener(new CollectionListener<Collection<Surge>, Surge>() {
            @Override
            public void onAdd(Collection<Surge> collection, Surge item, int position) {
                recompute();
            }

            @Override
            public void onRemove(Collection<Surge> collection, Surge item) {
                recompute();
            }

            @Override
            public void onChanged(Surge model, String key, Object oldValue, Object newValue) {
                recompute();
            }
        });

        recompute();
    }

    public void recompute() {
        synchronized (lock) {
            clear();
            if (surges.size() == 0) {
                return;
            }

            Surge latest = surges.get(0);
            Aggregate aggregate = new Aggregate(Collections.singletonList(latest), latest.getStart());
            add(aggregate);

            final Date since = new Date();
            while (true) {
                since.setTime(since.getTime() - (2 * 60 * 60 * 1000));

                final ArrayList<Surge> surgesSince = new ArrayList<Surge>();
                surges.each(new Visitor<Surge>() {
                    @Override
                    public void visit(Surge surge) {
                        if (since.before(surge.getStart())) {
                            surgesSince.add(surge);
                        }
                    }
                });
                if (surgesSince.size() == 0) {
                    continue;
                }

                final Date sinceCopy = new Date();
                sinceCopy.setTime(since.getTime());
                add(new Aggregate(surgesSince, sinceCopy));

                if (surgesSince.size() == surges.size()) {
                    break;
                }
            }
        }
    }
}




Java Source Code List

com.bklimt.surgetracker.ApplicationTest.java
com.bklimt.surgetracker.SurgeTrackerApplication.java
com.bklimt.surgetracker.TimerThread.java
com.bklimt.surgetracker.backbone.CollectionListener.java
com.bklimt.surgetracker.backbone.Collection.java
com.bklimt.surgetracker.backbone.ModelListener.java
com.bklimt.surgetracker.backbone.Model.java
com.bklimt.surgetracker.backbone.Visitor.java
com.bklimt.surgetracker.model.AggregateCollection.java
com.bklimt.surgetracker.model.Aggregate.java
com.bklimt.surgetracker.model.RootViewModel.java
com.bklimt.surgetracker.model.SurgeCollection.java
com.bklimt.surgetracker.model.SurgeParseObject.java
com.bklimt.surgetracker.model.Surge.java
com.bklimt.surgetracker.view.AggregateListAdapter.java
com.bklimt.surgetracker.view.AggregateListFragment.java
com.bklimt.surgetracker.view.MainActivity.java
com.bklimt.surgetracker.view.SurgeDeletionDialogFragment.java
com.bklimt.surgetracker.view.SurgeDurationDialogFragment.java
com.bklimt.surgetracker.view.SurgeGraphFragment.java
com.bklimt.surgetracker.view.SurgeListAdapter.java
com.bklimt.surgetracker.view.SurgeListFragment.java
com.bklimt.surgetracker.view.SurgeStartDialogFragment.java