Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
RangeDivisionInfo.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.timeline.utils;
20 
21 import com.google.common.collect.ImmutableList;
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.annotation.concurrent.Immutable;
25 import org.joda.time.DateTime;
26 import org.joda.time.DateTimeFieldType;
27 import org.joda.time.Days;
28 import org.joda.time.Hours;
29 import org.joda.time.Interval;
30 import org.joda.time.Minutes;
31 import org.joda.time.Months;
32 import org.joda.time.Seconds;
33 import org.joda.time.Years;
34 import org.joda.time.format.DateTimeFormat;
35 import org.joda.time.format.DateTimeFormatter;
36 import org.joda.time.format.ISODateTimeFormat;
39 
47 @Immutable
48 public class RangeDivisionInfo {
49 
53  private final TimeUnits blockSize;
54 
58  private final int numberOfBlocks;
59 
64  private final DateTimeFormatter tickFormatter;
65 
70  private final long lowerBound;
71 
76  private final long upperBound;
77 
81  private final Interval timeRange;
82  private ImmutableList<Interval> intervals;
83 
84  public Interval getTimeRange() {
85  return timeRange;
86  }
87 
88  private RangeDivisionInfo(Interval timeRange, int periodsInRange, TimeUnits periodSize, DateTimeFormatter tickformatter, long lowerBound, long upperBound) {
89  this.numberOfBlocks = periodsInRange;
90  this.blockSize = periodSize;
91  this.tickFormatter = tickformatter;
92 
93  this.lowerBound = lowerBound;
94  this.upperBound = upperBound;
95  this.timeRange = timeRange;
96  }
97 
108  public static RangeDivisionInfo getRangeDivisionInfo(Interval timeRange) {
109  //Check from largest to smallest unit
110 
111  //TODO: make this more generic... reduce code duplication -jm
112  DateTimeFieldType timeUnit;
113  final DateTime startWithZone = timeRange.getStart().withZone(TimeLineController.getJodaTimeZone());
114  final DateTime endWithZone = timeRange.getEnd().withZone(TimeLineController.getJodaTimeZone());
115 
116  if (Years.yearsIn(timeRange).isGreaterThan(Years.THREE)) {
117  timeUnit = DateTimeFieldType.year();
118  long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis();
119  long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis();
120  return new RangeDivisionInfo(timeRange, Years.yearsIn(timeRange).get(timeUnit.getDurationType()) + 1, TimeUnits.YEARS, ISODateTimeFormat.year(), lower, upper);
121  } else if (Months.monthsIn(timeRange).isGreaterThan(Months.THREE)) {
122  timeUnit = DateTimeFieldType.monthOfYear();
123  long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis();
124  long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis();
125  return new RangeDivisionInfo(timeRange, Months.monthsIn(timeRange).getMonths() + 1, TimeUnits.MONTHS, DateTimeFormat.forPattern("YYYY'-'MMMM"), lower, upper); // NON-NLS
126  } else if (Days.daysIn(timeRange).isGreaterThan(Days.THREE)) {
127  timeUnit = DateTimeFieldType.dayOfMonth();
128  long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis();
129  long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis();
130  return new RangeDivisionInfo(timeRange, Days.daysIn(timeRange).getDays() + 1, TimeUnits.DAYS, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd"), lower, upper); // NON-NLS
131  } else if (Hours.hoursIn(timeRange).isGreaterThan(Hours.THREE)) {
132  timeUnit = DateTimeFieldType.hourOfDay();
133  long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis();
134  long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis();
135  return new RangeDivisionInfo(timeRange, Hours.hoursIn(timeRange).getHours() + 1, TimeUnits.HOURS, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd HH"), lower, upper); // NON-NLS
136  } else if (Minutes.minutesIn(timeRange).isGreaterThan(Minutes.THREE)) {
137  timeUnit = DateTimeFieldType.minuteOfHour();
138  long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis();
139  long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis();
140  return new RangeDivisionInfo(timeRange, Minutes.minutesIn(timeRange).getMinutes() + 1, TimeUnits.MINUTES, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd HH':'mm"), lower, upper); // NON-NLS
141  } else {
142  timeUnit = DateTimeFieldType.secondOfMinute();
143  long lower = startWithZone.property(timeUnit).roundFloorCopy().getMillis();
144  long upper = endWithZone.property(timeUnit).roundCeilingCopy().getMillis();
145  return new RangeDivisionInfo(timeRange, Seconds.secondsIn(timeRange).getSeconds() + 1, TimeUnits.SECONDS, DateTimeFormat.forPattern("YYYY'-'MMMM'-'dd HH':'mm':'ss"), lower, upper); // NON-NLS
146  }
147  }
148 
149  public DateTimeFormatter getTickFormatter() {
150  return tickFormatter;
151  }
152 
153  public int getPeriodsInRange() {
154  return numberOfBlocks;
155  }
156 
158  return blockSize;
159  }
160 
161  public long getUpperBound() {
162  return upperBound;
163  }
164 
165  public long getLowerBound() {
166  return lowerBound;
167  }
168 
169  @SuppressWarnings("ReturnOfCollectionOrArrayField")
170  synchronized public List<Interval> getIntervals() {
171  if (intervals == null) {
172  ArrayList<Interval> tempList = new ArrayList<>();
173  //extend range to block bounderies (ie day, month, year)
174  final Interval range = new Interval(new DateTime(lowerBound, TimeLineController.getJodaTimeZone()), new DateTime(upperBound, TimeLineController.getJodaTimeZone()));
175 
176  DateTime start = range.getStart();
177  while (range.contains(start)) {
178  //increment for next iteration
179  DateTime end = start.plus(getPeriodSize().getPeriod());
180  final Interval interval = new Interval(start, end);
181  tempList.add(interval);
182  start = end;
183  }
184  intervals = ImmutableList.copyOf(tempList);
185  }
186  return intervals;
187  }
188 
189  public String formatForTick(Interval interval) {
190  return interval.getStart().toString(tickFormatter);
191  }
192 }
static RangeDivisionInfo getRangeDivisionInfo(Interval timeRange)
RangeDivisionInfo(Interval timeRange, int periodsInRange, TimeUnits periodSize, DateTimeFormatter tickformatter, long lowerBound, long upperBound)

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.