/*
 * Copyright 2010-2013 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.subscription.alignment;

import org.joda.time.DateTime;

import org.killbill.billing.catalog.api.Duration;

public class BaseAligner {

    protected DateTime addDuration(final DateTime input, final Duration duration) {
        return addOrRemoveDuration(input, duration, true);
    }

    protected DateTime removeDuration(final DateTime input, final Duration duration) {
        return addOrRemoveDuration(input, duration, false);
    }

    private DateTime addOrRemoveDuration(final DateTime input, final Duration duration, boolean add) {
        DateTime result = input;
        switch (duration.getUnit()) {
            case DAYS:
                result = add ? result.plusDays(duration.getNumber()) : result.minusDays(duration.getNumber());
                ;
                break;

            case MONTHS:
                result = add ? result.plusMonths(duration.getNumber()) : result.minusMonths(duration.getNumber());
                break;

            case YEARS:
                result = add ? result.plusYears(duration.getNumber()) : result.minusYears(duration.getNumber());
                break;
            case UNLIMITED:
            default:
                throw new RuntimeException("Trying to move to unlimited time period");
        }
        return result;
    }
}

@@ -21,9 +21,9 @@
 -201
-3
+4
  Nin
@@ -36,16 +36,115 @@
 .%0A *
-%0A * Ning
+ Copyright 2014-2016 Groupon, Inc%0A * Copyright 2014-2016 The Billing Project, LLC%0A *%0A * The Billing Project
  lic
@@ -342,16 +342,17 @@
  *%0A *   
+ 
 http://w
@@ -801,17 +801,16 @@
 teTime;%0A
-%0A
 import o
@@ -1271,16 +1271,22 @@
 uration,
+ final
  boolean
@@ -1305,140 +1305,31 @@
     
-DateTime result = input;%0A        switch (duration.getUnit()) %7B%0A            case DAYS:%0A                result =
+return
  add ? 
-resul
+inpu
 t.plus
-Days
 (dur
@@ -1338,382 +1338,37 @@
 ion.
-getNumber()) : result.minusDays(duration.getNumber());%0A                ;%0A                break;%0A%0A            case MONTHS:%0A                result = add ? result.plusMonths(duration.getNumber()) : result.minusMonths(duration.getNumber());%0A                break;%0A%0A            case YEARS:%0A                result = add ? result.plusYears(duration.getNumber()) : resul
+toJodaPeriod()) : inpu
 t.minus
-Years
 (dur
@@ -1377,212 +1377,23 @@
 ion.
-getNumber());%0A                break;%0A            case UNLIMITED:%0A            default:%0A                throw new RuntimeException(%22Trying to move to unlimited time period%22);%0A        %7D%0A        return result
+toJodaPeriod())
 ;%0A  

simplify date computation in BaseAligner