com.ideaaedi.commonds.rank.DefaultRankSupporter Maven / Gradle / Ivy
The newest version!
package com.ideaaedi.commonds.rank;
import java.util.List;
/**
* 排名器
*
* @author JustryDeng
* @since 1.0.0
*/
public class DefaultRankSupporter> implements RankSupporter {
@Override
public List natural(List nonNullSortedList) {
int rankStartValue = rankStartValue();
for (int i = 0, size = nonNullSortedList.size(); i < size; i++) {
nonNullSortedList.get(i).fillRank(i + rankStartValue);
}
return nonNullSortedList;
}
@Override
public List indexPostponedIfSame(List nonNullSortedList) {
// 第一名先排 rankStartValue
int rankStartValue = rankStartValue();
T pre = nonNullSortedList.get(0);
pre.fillRank(rankStartValue);
// 处理剩下的
for (int i = 1, size = nonNullSortedList.size(); i < size; i++) {
T curr = nonNullSortedList.get(i);
if (pre.compareTo(curr) == 0) {
curr.fillRank(pre.obtainRank());
} else {
curr.fillRank(i + rankStartValue);
}
pre = curr;
}
return nonNullSortedList;
}
@Override
public List rankPostponedIfSame(List nonNullSortedList) {
// 第一名先排rankStartValue
int rankStartValue = rankStartValue();
T pre = nonNullSortedList.get(0);
pre.fillRank(rankStartValue);
// 处理剩下的
for (int i = 1, size = nonNullSortedList.size(); i < size; i++) {
T curr = nonNullSortedList.get(i);
int preRank = pre.obtainRank();
if (pre.compareTo(curr) == 0) {
curr.fillRank(preRank);
} else {
curr.fillRank(preRank + 1);
}
pre = curr;
}
return nonNullSortedList;
}
}