Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package com.archmage.toolbox;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  6. public abstract class Unique {
  7.  
  8. private final int id;
  9.  
  10. private static HashMap<String, ArrayList<Integer>> register = new HashMap<>();
  11. private static final int START_ID = 1;
  12.  
  13. public Unique() {
  14. String idClass = getIdCode();
  15. if(!register.containsKey(idClass)) {
  16. register.put(idClass, new ArrayList<>());
  17. }
  18. for(int i = START_ID; i < Integer.MAX_VALUE; ++i) {
  19. if(!register.get(idClass).contains(Integer.valueOf(i))) {
  20. register.get(idClass).add((id = i));
  21. return;
  22. }
  23. }
  24. // this should never happen
  25. throw new UnsupportedOperationException("no suitable id was available!");
  26. }
  27.  
  28. protected abstract String getIdCode();
  29.  
  30. public final String getId() {
  31. return getIdCode() + (id + "");
  32. }
  33.  
  34. /**
  35. * Receives an "id" argument and parses it into an array of valid ids.
  36. *
  37. * Expected format is [idClass][rangeStart]-[rangeEnd]
  38. *
  39. * e.g. for idClass "d", the following inputs are valid:
  40. *
  41. * "d1" = returns "d1"
  42. * "d4-6" = returns ["d4", "d5", "d6"]
  43. * "all" = returns all ids for idClass "d"
  44. *
  45. * If a larger rangeEnd than highest id is specified, it will return all
  46. * from rangeStart to the highest id.
  47. *
  48. * It is assumed that idClass does not contain spaces, numerals or hyphens.
  49. *
  50. * @param input
  51. * @return
  52. */
  53. public static String[] getIdsFromRange(String input) {
  54. if(input == null) return null;
  55. if((input = input.trim()).isEmpty()) return null;
  56. String idClass = input.replaceAll("[0-9]", "").replaceAll("\\-", "");
  57. if(idClass.length() == 0) return null;
  58. if(!register.containsKey(idClass)) return null;
  59. ArrayList<String> output = new ArrayList<>();
  60. int[] integers;
  61. try {
  62. String[] temp = input.replaceAll(idClass, "").split("\\-");
  63. if(temp.length == 0) return null;
  64.  
  65. int low = Integer.valueOf(temp[0]);
  66. int high = Integer.valueOf(temp[temp.length - 1]);
  67.  
  68. integers = new int[high - low + 1];
  69. for(int i = low; i <= high; ++i) {
  70. integers[i - low] = i;
  71. }
  72.  
  73. // was I high when I wrote this?!
  74. // integers = new int[temp.length];
  75. // for(int i = 0; i < temp.length; ++i)
  76. // integers[i] = Integer.valueOf(temp[i]);
  77. }
  78. catch(NumberFormatException nfe) {
  79. return null;
  80. }
  81. for(int i = integers[0]; i <= integers[integers.length - 1]; ++i) {
  82. if(register.get(idClass).contains(i)) {
  83. output.add(idClass + i);
  84. }
  85. else break;
  86. }
  87.  
  88. return output.toArray(new String[0]);
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement