patternjavaMinor
Searching database, filtering results, adding longs
Viewed 0 times
searchingaddingdatabaseresultslongsfiltering
Problem
My needs outpace my abilities. This code (mostly) works, but it is really ugly, doesn't always add correctly, and is in desperate need of some help refactoring. I can't wrap my head around what needs done with it.
I have a SQLite table
Time should be the difference between
In the code below,
Here is the basic logic:
-
This method searches a db for entries matching the criteria "anything that ended in
-
The next loop goes through and attempts to throw away a list entry if the next one has the same
I have a SQLite table
WorkLog which contains rows of int, long, long, long, and long. They are _id, unused, dayStartTimestamp, activityStartTime, and activityStopTime. Timestamps are stored as epoch time longs. It is possible to have multiple activities on the same day. I am trying to calculate the total time working in a time period defined by lookBackHours.Time should be the difference between
dayStartTime and activityStopTime + 15 minutes for each day. Since there can be many rows with the same dayStartTime, I want to discard entries with duplicate dayStartTimes, only keeping the row with the longest time between activityStartTime and activityStopTime. I am assuming rows in the db/cursor are sorted by dayStartTime and activityStopTime.In the code below,
activityStartTime is represented by TrackerContract.WorkLog.BLOCK_OUT and activityStopTime is rep by TrackerContract.WorkLog.BLOCK_IN. currentTimeMilliMinutes() can be assumed to be the same as System.currentTimeMillis(), and unrelated to the problem of kludgey, buggy code. It simply takes a System.currentTimeMillis() call, constructs a Calendar, then returns a millisecond time constructed from the calendar's Year, Month, Date, Hour, and Minute (stripping seconds and milliseconds from the time).Here is the basic logic:
-
This method searches a db for entries matching the criteria "anything that ended in
lookbackHours+15 minutes, or more recently". It puts them into a List of long[2] arrays, storing the dayStartTime and activityStopTime in position 0 and 1 in the array.-
The next loop goes through and attempts to throw away a list entry if the next one has the same
dayStartTime and a higher (more recent) `activityStoSolution
Your code is a bit too messy to understand what you are doing - or why you are doing it, but here are some pointers that can get you started:
-
Your method is way too long. Break it up into a couple of sub-methods.
-
Using a
-
Your indentation is a bit off. All the code in your method should be indented one step.
-
In your for-loop, you can use:
To get rid of the
As I said, this is just a start. I am still not sure about what it actually is that you are doing, but this will be more clear once you divide your method into a couple of sub-methods.
-
Your method is way too long. Break it up into a couple of sub-methods.
-
Using a
List and using the indexes 0 and 1 for that long[] like you are doing tells me that you should instead use a class to represent your values in the long[]. This will make it a lot clearer about what the 0 index actually is. (Assuming you use proper naming for it)-
Your indentation is a bit off. All the code in your method should be indented one step.
-
In your for-loop, you can use:
for (int i = 0; i = length)
continue;To get rid of the
else statement entirely.As I said, this is just a start. I am still not sure about what it actually is that you are doing, but this will be more clear once you divide your method into a couple of sub-methods.
Code Snippets
for (int i = 0; i < length; i++) {
if ((i+1) >= length)
continue;Context
StackExchange Code Review Q#37787, answer score: 5
Revisions (0)
No revisions yet.