Guest User

Untitled

a guest
Jan 22nd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. madis@madis:~/Documents/Koolivärk/5. semester/tarkvaraprojekt/plumbr-sales$ hg diff
  2. diff -r 756b68a8bcda src/eu/plumbr/sales/utils/Period.java
  3. --- a/src/eu/plumbr/sales/utils/Period.java Sat Oct 15 11:54:44 2011 +0300
  4. +++ b/src/eu/plumbr/sales/utils/Period.java Sat Oct 15 14:43:42 2011 +0300
  5. @@ -2,35 +2,18 @@
  6.  
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. -import java.util.logging.Logger;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. public class Period {
  14. - private static final Logger LOG = Logger.getLogger(Period.class.getName());
  15.  
  16. private Date start;
  17. private Date end;
  18.  
  19. - private static final int DEFAULT_MONTHS = 1;
  20. - private static final Pattern monthsPattern = Pattern.compile("(\\d{1,2}) months?");
  21. + private static final Pattern monthsPattern = Pattern.compile("(\\d{1,2}) months");
  22. private static final Pattern datePattern = Pattern.compile(
  23. "(\\d{4})-(\\d{2})-(\\d{2}):(\\d{4})-(\\d{2})-(\\d{2})");
  24.  
  25. - public Period() {
  26. - startMonthsBack(DEFAULT_MONTHS);
  27. - }
  28. -
  29. - public Period(Date start, Date end) {
  30. - this.start = start;
  31. - this.end = end;
  32. - }
  33. -
  34. - /**
  35. - * Parses two dates of the format yyyy-MM-dd:yyyy-MM-dd or a string of the
  36. - * format "x month(s)" in which case the end is today and the beginning is
  37. - * the first day of x months back.
  38. - */
  39. public Period(String period) {
  40. Matcher dateMatcher = datePattern.matcher(period);
  41. Matcher monthsMatcher = monthsPattern.matcher(period);
  42. @@ -40,8 +23,7 @@
  43. } else if (monthsMatcher.matches()) {
  44. parseMonths(monthsMatcher);
  45. } else {
  46. - LOG.warning("Failed to parse " + period + ". Using default.");
  47. - startMonthsBack(DEFAULT_MONTHS);
  48. + throw new IllegalArgumentException("Failed to parse " + period);
  49. }
  50. }
  51.  
  52. @@ -64,31 +46,37 @@
  53.  
  54. /** Takes a Matcher that matches <tt>monthsPattern</tt> and parses a period from it */
  55. private void parseMonths(Matcher matcher) {
  56. - startMonthsBack(Integer.parseInt(matcher.group(1)));
  57. - }
  58. -
  59. - private void startMonthsBack(int months) {
  60. Calendar calendar = Calendar.getInstance();
  61. int year = calendar.get(Calendar.YEAR);
  62. int month = calendar.get(Calendar.MONTH);
  63. - int day = calendar.get(Calendar.DATE);
  64. + int day = calendar.get(Calendar.DAY_OF_MONTH);
  65. calendar.clear();
  66. calendar.set(year, month, day);
  67.  
  68. end = calendar.getTime();
  69. + int months = Integer.parseInt(matcher.group(1));
  70. calendar.add(Calendar.MONTH, -months);
  71. calendar.set(Calendar.DATE, 1);
  72. start = calendar.getTime();
  73. }
  74.  
  75. + public Period(Date start, Date end) {
  76. + this.start = start;
  77. + this.end = end;
  78. + }
  79. +
  80. + // TODO compare end to start
  81. public Duration getDuration() {
  82. long diffInSeconds = (end.getTime() - start.getTime()) / 1000;
  83. -
  84. - long diff[] = new long[] { 0, 0, 0 };
  85. - diff[2] = (diffInSeconds = (diffInSeconds / 60)) % 60;
  86. - diff[1] = (diffInSeconds = (diffInSeconds / 60)) % 24;
  87. - diff[0] = (diffInSeconds = (diffInSeconds / 24));
  88. -
  89. +
  90. + long diff[] = new long[] { 0, 0, 0 };
  91. + // minutes
  92. + diff[2] = (diffInSeconds = (diffInSeconds / 60)) >= 60 ? diffInSeconds % 60 : diffInSeconds;
  93. + // hours
  94. + diff[1] = (diffInSeconds = (diffInSeconds / 60)) >= 24 ? diffInSeconds % 24 : diffInSeconds;
  95. + // days
  96. + diff[0] = (diffInSeconds = (diffInSeconds / 24));
  97. +
  98. Duration duration = new Duration(diff[0], diff[1], diff[2]);
  99. return duration;
  100. }
  101. diff -r 756b68a8bcda tests/eu/plumbr/sales/utils/PeriodTest.java
  102. --- a/tests/eu/plumbr/sales/utils/PeriodTest.java Sat Oct 15 11:54:44 2011 +0300
  103. +++ b/tests/eu/plumbr/sales/utils/PeriodTest.java Sat Oct 15 14:43:42 2011 +0300
  104. @@ -10,8 +10,7 @@
  105.  
  106. public class PeriodTest extends TestCase {
  107. private static final Logger LOG = Logger.getLogger(PeriodTest.class.getName());
  108. -
  109. - private Calendar calendar = Calendar.getInstance();
  110. + Calendar calendar = Calendar.getInstance();
  111.  
  112. public void testPeriodStringDates() {
  113. calendar.clear();
  114. @@ -24,9 +23,12 @@
  115. assertEquals(expected, actual);
  116. }
  117.  
  118. + // TODO test parsing of "x months" strings
  119. + // pay special attention to end of month cases
  120. + // (e.g. what is March 30 - 1 month?)
  121. public void testPeriodStringMonths() {
  122. calendar = Calendar.getInstance();
  123. -
  124. +
  125. // calculate expected end
  126. int year = calendar.get(Calendar.YEAR);
  127. int month = calendar.get(Calendar.MONTH);
  128. @@ -34,44 +36,32 @@
  129. calendar.clear();
  130. calendar.set(year, month, day);
  131. Date end = calendar.getTime();
  132. -
  133. +
  134. // calculate expected start
  135. calendar.add(Calendar.MONTH, -1);
  136. calendar.set(Calendar.DATE, 1);
  137. Date start = calendar.getTime();
  138. -
  139. +
  140. Period expected = new Period(start, end);
  141. - Period actual = new Period("1 month");
  142. + Period actual = new Period("1 months");
  143. +
  144. assertEquals(expected, actual);
  145. }
  146.  
  147. public void testGetDuration() {
  148. - long days = 1;
  149. - long hours = 1;
  150. - long minutes = 15;
  151. + long days = 35;
  152. + long hours = 0;
  153. + long minutes = 0;
  154. Duration expected = new Duration(days, hours, minutes);
  155. -
  156. - calendar.clear();
  157. - calendar.set(1989, 1, 1, 12, 30);
  158. - Date start = calendar.getTime();
  159. - calendar.set(1989, 1, 2, 13, 45);
  160. - Date end = calendar.getTime();
  161. - Duration actual = new Period(start, end).getDuration();
  162. -
  163. +
  164. + Period period = new Period("1989-00-00:1989-01-04");
  165. + Duration actual = period.getDuration();
  166. +
  167. assertEquals(expected, actual);
  168. }
  169.  
  170. public void testEqualsObject() {
  171. - calendar = Calendar.getInstance();
  172. - Date start = calendar.getTime();
  173. - Date end = calendar.getTime();
  174. - Period first = new Period(start, end);
  175. - Period second = new Period(start, end);
  176. - assertTrue(first.equals(second));
  177. -
  178. - calendar.set(Calendar.YEAR, 1989);
  179. - Period notEqual = new Period(calendar.getTime(), end);
  180. - assertFalse(first.equals(notEqual));
  181. + fail("Not yet implemented");
  182. }
  183.  
  184. }
Add Comment
Please, Sign In to add comment