fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.security.MessageDigest;
  7. import java.nio.charset.StandardCharsets;
  8.  
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13.  
  14. public static class Ring {
  15.  
  16. public static class OrderedMemberRing {
  17.  
  18. public static OrderedMemberRing ofOnlineMembers(HashSet<String> onlineMembers, String ownName, String hashModification) {
  19. HashMap<Integer, String> hashToMember = new HashMap<>();
  20. for(String member : onlineMembers) {
  21. int hash = hashOf(member, hashModification);
  22. if(hashToMember.containsKey(hash)) {
  23. // Hash collision, can't determine next
  24. return null;
  25. }
  26. hashToMember.put(hash, member);
  27. }
  28. if(ownName != null) {
  29. int ownHash = hashOf(ownName, hashModification);
  30. if(!hashToMember.containsKey(ownHash))
  31. hashToMember.put(ownHash, ownName);
  32. }
  33. ArrayList<Integer> orderedHashes = new ArrayList<>(hashToMember.keySet());
  34. Collections.sort(orderedHashes);
  35.  
  36. ArrayList<String> orderedMembers = new ArrayList<>();
  37. for(int hash : orderedHashes)
  38. orderedMembers.add(hashToMember.get(hash));
  39. return new OrderedMemberRing(orderedMembers.toArray(new String[0]), hashModification);
  40. }
  41.  
  42. private final String[] ring;
  43. private final String usedHashModification;
  44.  
  45. public OrderedMemberRing(String[] ring, String usedHashModification) {
  46. this.ring = ring;
  47. this.usedHashModification = usedHashModification;
  48. }
  49.  
  50. public String[] getRing() {
  51. return ring;
  52. }
  53.  
  54. public String getUsedHashModification() {
  55. return usedHashModification;
  56. }
  57.  
  58. /**
  59.   * @return -1 if not found. Else index of member in orderedMemberRing
  60.   */
  61. public int indexOf(String member) {
  62. int memberIndex = -1;
  63. for(int i = 0; i < ring.length; i++) {
  64. if(ring[i].equalsIgnoreCase(member)) {
  65. memberIndex = i;
  66. break;
  67. }
  68. }
  69. return memberIndex;
  70. }
  71.  
  72. public boolean contains(String member) {
  73. return indexOf(member) != -1;
  74. }
  75.  
  76. public String after(String member) {
  77. int memberIndex = indexOf(member);
  78. if(memberIndex == -1) return null; // Not found!
  79. return ring[Math.floorMod(memberIndex + 1, ring.length)];
  80. }
  81.  
  82. public String before(String member) {
  83. int memberIndex = indexOf(member);
  84. if(memberIndex == -1) return null; // Not found!
  85. return ring[Math.floorMod(memberIndex - 1, ring.length)];
  86. }
  87. }
  88.  
  89. public static int hashOf(String userName, String hashModification) {
  90. userName = userName.toLowerCase();
  91. String hashStr = userName + (hashModification != null ? ":" + hashModification : "");
  92. // Create MD5 Hash
  93. try {
  94. MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
  95. byte[] hash = digest.digest(hashStr.getBytes(StandardCharsets.UTF_8));
  96. int h = (hash[0] << 3*8) | (hash[1] << 2*8) | (hash[2] << 8) | (hash[3]);
  97. System.out.println(userName + " : " + h + " : " + hash[3]);
  98. return h;
  99. }catch (Exception ex) {
  100. throw new RuntimeException("WTF, this should never happen! You don't have MD5!");
  101. }
  102. }
  103.  
  104. public static boolean contains(HashSet<String> members, String member) {
  105. for(String maybeMember : members)
  106. if(maybeMember.equalsIgnoreCase(member)) return true;
  107. return false;
  108. }
  109.  
  110. }
  111.  
  112.  
  113. public static void main (String[] args) throws java.lang.Exception
  114. {
  115. HashSet<String> members = new HashSet<>();
  116. members.add("EnderKill98");
  117. members.add("ItzN00bPvP");
  118. members.add("GoobBot1");
  119. members.add("Honbra");
  120. var r = Ring.OrderedMemberRing.ofOnlineMembers(members, "EnderKill98", "rwgOSa");
  121. for(String member : r.getRing()) {
  122. System.out.println(" - " + member);
  123. }
  124. }
  125. }
  126.  
Success #stdin #stdout 0.22s 61388KB
stdin
Standard input is empty
stdout
goobbot1 : -34 : -34
enderkill98 : -78 : -78
itzn00bpvp : -118 : -118
honbra : -105 : -105
enderkill98 : -78 : -78
 - ItzN00bPvP
 - Honbra
 - EnderKill98
 - GoobBot1