Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- madis@madis:~/Documents/Koolivärk/5. semester/tarkvaraprojekt/plumbr-sales$ hg diff
- diff -r 756b68a8bcda src/eu/plumbr/sales/utils/Period.java
- --- a/src/eu/plumbr/sales/utils/Period.java Sat Oct 15 11:54:44 2011 +0300
- +++ b/src/eu/plumbr/sales/utils/Period.java Sat Oct 15 14:43:42 2011 +0300
- @@ -2,35 +2,18 @@
- import java.util.Calendar;
- import java.util.Date;
- -import java.util.logging.Logger;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Period {
- - private static final Logger LOG = Logger.getLogger(Period.class.getName());
- private Date start;
- private Date end;
- - private static final int DEFAULT_MONTHS = 1;
- - private static final Pattern monthsPattern = Pattern.compile("(\\d{1,2}) months?");
- + private static final Pattern monthsPattern = Pattern.compile("(\\d{1,2}) months");
- private static final Pattern datePattern = Pattern.compile(
- "(\\d{4})-(\\d{2})-(\\d{2}):(\\d{4})-(\\d{2})-(\\d{2})");
- - public Period() {
- - startMonthsBack(DEFAULT_MONTHS);
- - }
- -
- - public Period(Date start, Date end) {
- - this.start = start;
- - this.end = end;
- - }
- -
- - /**
- - * Parses two dates of the format yyyy-MM-dd:yyyy-MM-dd or a string of the
- - * format "x month(s)" in which case the end is today and the beginning is
- - * the first day of x months back.
- - */
- public Period(String period) {
- Matcher dateMatcher = datePattern.matcher(period);
- Matcher monthsMatcher = monthsPattern.matcher(period);
- @@ -40,8 +23,7 @@
- } else if (monthsMatcher.matches()) {
- parseMonths(monthsMatcher);
- } else {
- - LOG.warning("Failed to parse " + period + ". Using default.");
- - startMonthsBack(DEFAULT_MONTHS);
- + throw new IllegalArgumentException("Failed to parse " + period);
- }
- }
- @@ -64,31 +46,37 @@
- /** Takes a Matcher that matches <tt>monthsPattern</tt> and parses a period from it */
- private void parseMonths(Matcher matcher) {
- - startMonthsBack(Integer.parseInt(matcher.group(1)));
- - }
- -
- - private void startMonthsBack(int months) {
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- int month = calendar.get(Calendar.MONTH);
- - int day = calendar.get(Calendar.DATE);
- + int day = calendar.get(Calendar.DAY_OF_MONTH);
- calendar.clear();
- calendar.set(year, month, day);
- end = calendar.getTime();
- + int months = Integer.parseInt(matcher.group(1));
- calendar.add(Calendar.MONTH, -months);
- calendar.set(Calendar.DATE, 1);
- start = calendar.getTime();
- }
- + public Period(Date start, Date end) {
- + this.start = start;
- + this.end = end;
- + }
- +
- + // TODO compare end to start
- public Duration getDuration() {
- long diffInSeconds = (end.getTime() - start.getTime()) / 1000;
- -
- - long diff[] = new long[] { 0, 0, 0 };
- - diff[2] = (diffInSeconds = (diffInSeconds / 60)) % 60;
- - diff[1] = (diffInSeconds = (diffInSeconds / 60)) % 24;
- - diff[0] = (diffInSeconds = (diffInSeconds / 24));
- -
- +
- + long diff[] = new long[] { 0, 0, 0 };
- + // minutes
- + diff[2] = (diffInSeconds = (diffInSeconds / 60)) >= 60 ? diffInSeconds % 60 : diffInSeconds;
- + // hours
- + diff[1] = (diffInSeconds = (diffInSeconds / 60)) >= 24 ? diffInSeconds % 24 : diffInSeconds;
- + // days
- + diff[0] = (diffInSeconds = (diffInSeconds / 24));
- +
- Duration duration = new Duration(diff[0], diff[1], diff[2]);
- return duration;
- }
- diff -r 756b68a8bcda tests/eu/plumbr/sales/utils/PeriodTest.java
- --- a/tests/eu/plumbr/sales/utils/PeriodTest.java Sat Oct 15 11:54:44 2011 +0300
- +++ b/tests/eu/plumbr/sales/utils/PeriodTest.java Sat Oct 15 14:43:42 2011 +0300
- @@ -10,8 +10,7 @@
- public class PeriodTest extends TestCase {
- private static final Logger LOG = Logger.getLogger(PeriodTest.class.getName());
- -
- - private Calendar calendar = Calendar.getInstance();
- + Calendar calendar = Calendar.getInstance();
- public void testPeriodStringDates() {
- calendar.clear();
- @@ -24,9 +23,12 @@
- assertEquals(expected, actual);
- }
- + // TODO test parsing of "x months" strings
- + // pay special attention to end of month cases
- + // (e.g. what is March 30 - 1 month?)
- public void testPeriodStringMonths() {
- calendar = Calendar.getInstance();
- -
- +
- // calculate expected end
- int year = calendar.get(Calendar.YEAR);
- int month = calendar.get(Calendar.MONTH);
- @@ -34,44 +36,32 @@
- calendar.clear();
- calendar.set(year, month, day);
- Date end = calendar.getTime();
- -
- +
- // calculate expected start
- calendar.add(Calendar.MONTH, -1);
- calendar.set(Calendar.DATE, 1);
- Date start = calendar.getTime();
- -
- +
- Period expected = new Period(start, end);
- - Period actual = new Period("1 month");
- + Period actual = new Period("1 months");
- +
- assertEquals(expected, actual);
- }
- public void testGetDuration() {
- - long days = 1;
- - long hours = 1;
- - long minutes = 15;
- + long days = 35;
- + long hours = 0;
- + long minutes = 0;
- Duration expected = new Duration(days, hours, minutes);
- -
- - calendar.clear();
- - calendar.set(1989, 1, 1, 12, 30);
- - Date start = calendar.getTime();
- - calendar.set(1989, 1, 2, 13, 45);
- - Date end = calendar.getTime();
- - Duration actual = new Period(start, end).getDuration();
- -
- +
- + Period period = new Period("1989-00-00:1989-01-04");
- + Duration actual = period.getDuration();
- +
- assertEquals(expected, actual);
- }
- public void testEqualsObject() {
- - calendar = Calendar.getInstance();
- - Date start = calendar.getTime();
- - Date end = calendar.getTime();
- - Period first = new Period(start, end);
- - Period second = new Period(start, end);
- - assertTrue(first.equals(second));
- -
- - calendar.set(Calendar.YEAR, 1989);
- - Period notEqual = new Period(calendar.getTime(), end);
- - assertFalse(first.equals(notEqual));
- + fail("Not yet implemented");
- }
- }
Add Comment
Please, Sign In to add comment