开发者

Does a Java ranking class exist, increase/decrease, first/last rank

I'm in the process of creating a ranking class but was wondering if there is already any existing implementations.

Functionality needed (these will move the other ranks up/down appropriately) :

  • increase/decrease rank
  • move to first/last rank
  • move to rank (x)
  • check for any gaps in the rank, is so move the ranks around to get rid of the gaps. i.e. 1,2,4,5 will be updated to 1,2,3,4
  • check duplicate ranks. If there are duplicate ranks, set one of them down a rank (probably the oldest object) and move the other objects down the ranks.
  • Object getObjectAtRank(x)
  • Int getObjectRank(object)

Are there any exisiting classes that I can extend or am I better off doing it from scratch?

Possibly with a Map using but there would be problems with duplicate ranks etc.

Thanks!

Edit some additional info

The objects I am ranking are Jira Issues, these will have a rank field which I will read in so if an 开发者_高级运维issue is deleted there could be a gap in the ranking.


If you don't have any other constraints, I think ArrayList is sufficient for your needs:

  • increase/decrease rank: Swap neighboring elements.
  • move to first/last rank: Prepend/append using add().
  • move to rank (x): Insert using add().
  • check for any gaps in the rank: No need.
  • check duplicate ranks: No need.

You cannot get the rank of an object in O(1) time though.


How about a simple ArrayList? You could use the index as rank. The best part is you don't need to check for gaps or duplicate ranks.

void increaseRank(Object o) {
  int currentRank = list.indexOf(o);
  list.remove(o);
  list.add(o,currentRank - 1);
}


I am not aware of any full implementation of what you want, but Guava's Range class gets close, especially when used together with a DiscreteDomain

(unfortunately it's in version 10, which hasn't been released yet)


On re-reading the Question: you want to re-rank positions. Range does not seem to support that.

I think you should probably build your functionality around a simple Arraylist:

increase/decrease rank
move to first/last rank
move to rank (x)

All of these can be achieved with Collections.swap(List, offset1, offset2)

check for any gaps in the rank, is so move the ranks around to get rid of the gaps. i.e. 1,2,4,5 will be updated to 1,2,3,4

Irrelevant. Ranks are defined by List offset.

check duplicate ranks. If there are duplicate ranks, set one of them down a rank (probably the oldest object) and move the other objects down the ranks.

ditto. There are no dupes, one element comes first

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜