0

I'm working on a location-aware application (Android 4.2+) where I store locations to create routes.
The Route class is basically a wrapper for Stack with a few extra capabilities, such as having a JSON version of the route and a unique ID.

Here is my Route class

import android.location.Location;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Stack;

public class Route {

    private final static String TAG = "Route Class";
    private boolean locked;
    private String routeID;
    private Stack route;
    private JSONObject jsonRoute;


    public Route(final String routeID){
        Log.v(TAG, "Initializing new Route with ID "+routeID);

        this.routeID = routeID;
        route = new Stack<Location>();

        jsonRoute = new JSONObject();
        // Initialize JSON Route with Route ID
        try {
            jsonRoute.put("Route", routeID);
        } catch(JSONException e) {
            e.printStackTrace();
        }

        locked = false;
    }

    public void add(final Location location) {
        Log.v(TAG, "Adding a new location to Route "+routeID);
        route.push(location);

        //JSON
        JSONArray array = new JSONArray();
        JSONObject temp = new JSONObject();
        try {
            temp.put("Latitude", location.getLatitude());
            temp.put("Longitude", location.getLongitude());
            temp.put("Time", location.getTime());
            temp.put("Speed", location.getSpeed());

            array.put(temp);
            // Start with Location 0
            jsonRoute.put("Location"+(route.size()-1), array);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public Location removeLast() {
        Log.v(TAG, "Removing last location");
        jsonRoute.remove("Location"+(route.size()-1));
        return (Location) route.pop();
    }

    public Location getLast() {
        return (Location) route.peek();
    }

    public int size() {
        return route.size();
    }

    public boolean empty() {
        return route.empty();
    }

    public String getRouteID() {
        return routeID;
    }

    public Iterator iterator() {
        return route.iterator();
    }

    public void lock() {
        locked = !locked;
    }

    public boolean locked() {
        return locked;
    }
}

My problem is with the ID; I want to be able to create and remove routes, store them in the phone and upload them to a RESTful service. So far, I've thought about this solution for creating IDs for views, but what I want is:

  • An ID that is unique throughout the life of the application and future instances of it.
  • A meaningful ID that will provide extra information about the route.
  • An ID that allows me to compare with other IDs (a time hierarchy, similar to LIFO)

I was thinking I could create an ID using the current date, such as 20141013 plus n numbers appended to determine the route number of that day, long enough for it not to mess with other results. However, this would still create a problem if I close the application and open it again, since it'll start appending from 1 again. I basically want to be able to sort them through the stored data I'll have both in the phone and in the API, and load old routes if needed.

Has anybody run into a similar problem? I'm open to any recommendations, not only for the ID itself, but the whole Route class.

Thanks in advance!

Community
  • 1
  • 1
pvaldes
  • 1
  • 3
  • Why about the current date/time in millis? `System.currentTimeMillis()` – Joffrey Oct 13 '14 at 05:53
  • That's what I was actually thinking about when I mentioned the ID using the current date, but it wouldn't be a meaningful ID unless I change that long value to a date in the form of YYYYMMDD. – pvaldes Oct 13 '14 at 05:55

0 Answers0