blob: e597b48f375edcbf7c09d98da13020eecb042b0c [file] [log] [blame]
/*
* Copyright 2017 Kuelap, Inc.
*
* 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 io.mifos.individuallending.internal.service;
import io.mifos.individuallending.api.v1.domain.product.ChargeProportionalDesignator;
import io.mifos.portfolio.api.v1.domain.ChargeDefinition;
import java.util.Comparator;
import java.util.Optional;
/**
* @author Myrle Krantz
*/
class ScheduledChargeComparator implements Comparator<ScheduledCharge>
{
@Override
public int compare(ScheduledCharge o1, ScheduledCharge o2) {
return compareScheduledCharges(o1, o2);
}
static int compareScheduledCharges(ScheduledCharge o1, ScheduledCharge o2) {
int ret = o1.getScheduledAction().when.compareTo(o2.getScheduledAction().when);
if (ret != 0)
return ret;
ret = o1.getScheduledAction().action.compareTo(o2.getScheduledAction().action);
if (ret != 0)
return ret;
ret = proportionalityApplicationOrder(o1.getChargeDefinition(), o2.getChargeDefinition());
if (ret != 0)
return ret;
return o1.getChargeDefinition().getIdentifier().compareTo(o2.getChargeDefinition().getIdentifier());
}
static int proportionalityApplicationOrder(final ChargeDefinition o1, final ChargeDefinition o2) {
final Optional<ChargeProportionalDesignator> aProportionalToDesignator
= ChargeProportionalDesignator.fromString(o1.getProportionalTo());
final Optional<ChargeProportionalDesignator> bProportionalToDesignator
= ChargeProportionalDesignator.fromString(o2.getProportionalTo());
if (aProportionalToDesignator.isPresent() && bProportionalToDesignator.isPresent())
return Integer.compare(
aProportionalToDesignator.get().getOrderOfApplication(),
bProportionalToDesignator.get().getOrderOfApplication());
else if (aProportionalToDesignator.isPresent())
return 1;
else if (bProportionalToDesignator.isPresent())
return -1;
else
return 0;
}
}