/*
 * Copyright (c) 2017 Michael D'Arcy and Brianne O'Niel.
 *
 * Licensed 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 com.group13.androidsdk.mycards;

import java.util.Date;

/**
 * A basic implementation of the <code>DatePattern</code> interface, using a simple pattern of
 * start and end dates. Any dates between the start and end dates are considered matches, and all
 * others are not matches.
 */
class SimpleDatePattern implements DatePattern {

    private Date start;
    private Date end;
    private long repeatInterval = -1;
    private int numRepeats = 0;

    public SimpleDatePattern(Date start, Date end, long repeatInterval, int numRepeats) {
        this.start = start;
        this.end = end;
        this.repeatInterval = repeatInterval;
        this.numRepeats = numRepeats;
    }

    @Override
    public boolean dateMatches(Date d) {
        if (d == null || start == null || end == null) {
            return false;
        }

        long neededRepeats = 0;
        if (0 < repeatInterval) {
            neededRepeats = (d.getTime() - start.getTime()) / repeatInterval;
        }
        if (numRepeats < neededRepeats) {
            neededRepeats = numRepeats;
        }

        long trueStart = start.getTime() + neededRepeats * repeatInterval;
        long trueEnd = end.getTime() + neededRepeats * repeatInterval;

        return (trueStart <= d.getTime() && d.getTime() <= trueEnd);
    }

    /**
     * Gets the next matching date after the given <code>Date</code>. If the <code>Date</code>
     * given is null, the current date/time is used instead.
     *
     * @param startDate the date to start from when searching for the next match.
     * @return a <code>Date</code> indicating the next matching date
     */
    public Date nextMatch(Date startDate) {
        if (startDate == null) {
            startDate = new Date();
        }
        if (this.dateMatches(startDate)) {
            return startDate;
        }

        long neededRepeats;
        if ((startDate.getTime() - start.getTime()) <= 0) {
            neededRepeats = 0;
        } else if (0 < repeatInterval) {
            neededRepeats = (startDate.getTime() - start.getTime()) / repeatInterval + 1;
        } else {
            return null;
        }

        if(numRepeats < neededRepeats) {
            return null;
        }

        return new Date(this.start.getTime() + repeatInterval * neededRepeats);
    }
}

@@ -1297,24 +1297,184 @@
  numRepeats;
+%0A%0A        if(this.end.getTime() %3C this.start.getTime()) %7B%0A            Date tmp = this.end;%0A            this.end = start;%0A            this.start = tmp;%0A        %7D
 %0A    %7D%0A%0A    
@@ -2084,24 +2084,969 @@
 nd);%0A    %7D%0A%0A
+    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) %7B%0A            return true;%0A        %7D%0A        if (o == null %7C%7C getClass() != o.getClass()) %7B%0A            return false;%0A        %7D%0A%0A        SimpleDatePattern that = (SimpleDatePattern) o;%0A%0A        if (repeatInterval != that.repeatInterval) %7B%0A            return false;%0A        %7D%0A        if (numRepeats != that.numRepeats) %7B%0A            return false;%0A        %7D%0A        if (start != null ? !start.equals(that.start) : that.start != null) %7B%0A            return false;%0A        %7D%0A        return end != null ? end.equals(that.end) : that.end == null;%0A%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        int result = start != null ? start.hashCode() : 0;%0A        result = 31 * result + (end != null ? end.hashCode() : 0);%0A        result = 31 * result + (int) (repeatInterval %5E (repeatInterval %3E%3E%3E 32));%0A        result = 31 * result + numRepeats;%0A        return result;%0A    %7D%0A%0A
     /**%0A    
@@ -4030,16 +4030,17 @@
 peats);%0A
-
     %7D%0A
+%0A
 %7D%0A

Add equals() and hashcode() to SimpleDatePattern