1 7 8 package org.jdesktop.dataset; 9 import java.util.Map ; 10 import java.util.WeakHashMap ; 11 12 18 class NameGenerator { 19 private String prefix; 20 private Map <Object ,String > usedNames = new WeakHashMap <Object ,String >(); 21 22 23 public NameGenerator(String prefix) { 24 this.prefix = prefix; 25 } 26 27 33 public String generateName(Object obj) { 34 for (int i=0; i<Integer.MAX_VALUE; i++) { 35 String name = prefix + (i+1); 36 if (!usedNames.values().contains(name)) { 37 usedNames.put(obj, name); 38 return name; 39 } 40 } 41 assert false : "Apparently I ran out of available names, this should never happen"; 42 return null; 43 } 44 } 45 | Popular Tags |