Fix a bug with mutable state.
authorChristos KK Loverdos <loverdos@gmail.com>
Mon, 6 Feb 2012 10:36:06 +0000 (12:36 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Mon, 6 Feb 2012 10:36:06 +0000 (12:36 +0200)
Although DateCalculator is mutable, I was using it as an immutable.

src/main/scala/gr/grnet/aquarium/user/UserStateComputations.scala
src/main/scala/gr/grnet/aquarium/util/date/DateCalculator.scala

index 22b1c25..670c900 100644 (file)
@@ -221,9 +221,8 @@ class UserStateComputations extends Loggable {
                                 accounting: Accounting): Maybe[EndOfBillingState] = Maybe {
 
     val billingMonthStartDate = new DateCalculator(yearOfBillingMonth, billingMonth, 1)
-    val billingMonthStopDate = billingMonthStartDate.endOfThisMonth
+    val billingMonthStopDate = billingMonthStartDate.copy.endOfThisMonth
     logger.debug("billingMonthStartDate = %s".format(billingMonthStartDate))
-    logger.debug("billingMonthStartDate == billingMonthStopDate = %s".format(billingMonthStartDate == billingMonthStopDate))
     logger.debug("billingMonthStopDate  = %s".format(billingMonthStopDate))
 
     val prevBillingMonthStartDate = billingMonthStartDate.previousMonth
index ecdac70..f8be752 100644 (file)
@@ -8,12 +8,12 @@ import java.text.DateFormat
 /**
  * Date calculator.
  *
- * Utility class for date manipulations.
+ * Utility class for date manipulations. Not that this is mutable.
  *
  * @author Christos KK Loverdos <loverdos@gmail.com>
  */
 
-class DateCalculator private(private[this] var dateTime: MutableDateTime) {
+class DateCalculator private(private[this] var dateTime: MutableDateTime) extends Cloneable {
   def this(millis: Long)  = this(new MutableDateTime(millis))
   def this(date: Date)    = this(new MutableDateTime(date))
   def this(cal: Calendar) = this(new MutableDateTime(cal))
@@ -24,6 +24,11 @@ class DateCalculator private(private[this] var dateTime: MutableDateTime) {
   def this(year: Int, monthOfYear: Int) =
     this(year, monthOfYear, 1)
 
+
+  override def clone(): DateCalculator = new DateCalculator(this.dateTime)
+
+  def copy: DateCalculator = clone()
+
   def plusMonths(n: Int): this.type = {
     dateTime.addMonths(n)