51 /** 52 * Constructor 53 * 54 * @param parent parent name space 55 */ 56 public Namespace(final Namespace parent) { 57 this.parent = parent; 58 this.directory = new HashMap<>(); 59 } 60 61 /** 62 * Return the parent Namespace of this space. 63 * 64 * @return parent name space 65 */ 66 public Namespace getParent() { 67 return parent; 68 } 69 70 /** 71 * Create a uniqueName name in the namespace in the form base$n where n varies. 72 * Also truncates very long names that would otherwise break ASM. 73 * 74 * @param base Base of name. Base will be returned if uniqueName. 75 * @return Generated uniqueName name. 76 */ 77 public String uniqueName(final String base) { 78 final String truncatedBase = base.length() > LARGE_STRING_THRESHOLD ? base.substring(0, LARGE_STRING_THRESHOLD) : base; 79 for (Namespace namespace = this; namespace != null; namespace = namespace.getParent()) { 80 final HashMap<String, Integer> namespaceDirectory = namespace.directory; 81 final Integer counter = namespaceDirectory.get(truncatedBase); 82 83 if (counter != null) { 84 final int count = counter + 1; 85 namespaceDirectory.put(truncatedBase, count); 86 return truncatedBase + '-' + count; 87 } 88 } 89 90 directory.put(truncatedBase, 0); 91 92 return truncatedBase; 93 } 94 95 @Override 96 public String toString() { 97 return directory.toString(); 98 } 99 } | 51 /** 52 * Constructor 53 * 54 * @param parent parent name space 55 */ 56 public Namespace(final Namespace parent) { 57 this.parent = parent; 58 this.directory = new HashMap<>(); 59 } 60 61 /** 62 * Return the parent Namespace of this space. 63 * 64 * @return parent name space 65 */ 66 public Namespace getParent() { 67 return parent; 68 } 69 70 /** 71 * Create a uniqueName name in the namespace in the form base-n where n varies. 72 * Also truncates very long names that would otherwise break ASM. 73 * 74 * @param base Base of name. Base will be returned if uniqueName. 75 * @return Generated uniqueName name. 76 */ 77 public String uniqueName(final String base) { 78 final String truncatedBase = base.length() > LARGE_STRING_THRESHOLD ? base.substring(0, LARGE_STRING_THRESHOLD) : base; 79 for (Namespace namespace = this; namespace != null; namespace = namespace.getParent()) { 80 final HashMap<String, Integer> namespaceDirectory = namespace.directory; 81 final Integer counter = namespaceDirectory.get(truncatedBase); 82 83 if (counter != null) { 84 final int count = counter + 1; 85 namespaceDirectory.put(truncatedBase, count); 86 return truncatedBase + CompilerConstants.ID_FUNCTION_SEPARATOR.symbolName() + count; 87 } 88 } 89 90 directory.put(truncatedBase, 0); 91 92 return truncatedBase; 93 } 94 95 @Override 96 public String toString() { 97 return directory.toString(); 98 } 99 } |