/** * Copyright (C) 2017-2019 Christian Pierre MOMON * * This file is part of (April) Hebdobot. * * Hebdobot is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Hebdobot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Hebdobot. If not, see */ package org.april.hebdobot.bot.stats; import java.time.LocalDateTime; import java.util.Comparator; /** * The Class Stat. */ public class ReviewDataComparator implements Comparator { public enum Sorting { DATE, USERCOUNT, DURATION } private Sorting sorting; /** * Instantiates a new review data comparator. * * @param sorting * the sorting */ public ReviewDataComparator(final Sorting sorting) { this.sorting = sorting; } /* (non-Javadoc) * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ @Override public int compare(final ReviewData alpha, final ReviewData bravo) { int result; result = compare(alpha, bravo, this.sorting); // return result; } /** * Compare. * * @param alpha * the alpha * @param bravo * the bravo * @return the int */ public static int compare(final Integer alpha, final Integer bravo) { int result; // if ((alpha == null) && (bravo == null)) { result = 0; } else if (alpha == null) { result = -1; } else if (bravo == null) { result = +1; } else { result = alpha.compareTo(bravo); } // return result; } public static int compare(final LocalDateTime alpha, final LocalDateTime bravo) { int result; // if ((alpha == null) && (bravo == null)) { result = 0; } else if (alpha == null) { result = -1; } else if (bravo == null) { result = +1; } else { result = alpha.compareTo(bravo); } // return result; } /** * Compare. * * @param alpha * the alpha * @param bravo * the bravo * @return the int */ public static int compare(final Long alpha, final Long bravo) { int result; // if ((alpha == null) && (bravo == null)) { result = 0; } else if (alpha == null) { result = -1; } else if (bravo == null) { result = +1; } else { result = alpha.compareTo(bravo); } // return result; } /** * Compare. * * @param alpha * the alpha * @param bravo * the bravo * @param sorting * the sorting * @return the int */ public static int compare(final ReviewData alpha, final ReviewData bravo, final Sorting sorting) { int result; if (sorting == null) { result = 0; } else { switch (sorting) { case DATE: result = compare(getDate(alpha), getDate(bravo)); break; case USERCOUNT: result = compare(getUserCount(alpha), getUserCount(bravo)); if (result == 0) { result = compare(getDate(alpha), getDate(bravo)); } break; case DURATION: result = compare(getDuration(alpha), getDuration(bravo)); break; default: result = 0; } } // return result; } /** * Gets the date. * * @param source * the source * @return the date */ public static LocalDateTime getDate(final ReviewData source) { LocalDateTime result; if (source == null) { result = null; } else { result = source.getDate(); } // return result; } /** * Gets the duration. * * @param source * the source * @return the duration */ public static Long getDuration(final ReviewData source) { Long result; if (source == null) { result = null; } else { result = source.getDuration(); } // return result; } /** * Gets the user count. * * @param source * the source * @return the user count */ public static Integer getUserCount(final ReviewData source) { Integer result; if (source == null) { result = null; } else { result = source.getUserCount(); } // return result; } }