]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/util/TypeUnparsingContext.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / util / TypeUnparsingContext.java
1 package org.simantics.scl.compiler.types.util;
2
3 import gnu.trove.map.hash.THashMap;
4
5 public class TypeUnparsingContext {
6     
7     THashMap<Object, String> names;
8     int nameId;
9     
10     public TypeUnparsingContext() {
11         this.names = new THashMap<Object, String>();
12         this.nameId = 0;
13     }
14     
15     public TypeUnparsingContext(TypeUnparsingContext parent) {
16         this.names = new THashMap<Object, String>(parent.names);
17         this.nameId = parent.nameId;
18     }
19
20     public String getName(Object var) {
21         String name = names.get(var);
22         if(name == null) {
23             name = idToName(nameId++);
24             names.put(var, name);
25         }
26         return name;
27     }
28
29     private static final int alphabetCount = 'z'-'a'+1;
30     
31     private static String idToName(int id) {
32         String name = Character.toString((char)('a' + id % alphabetCount));
33         id /= alphabetCount;
34         if(id > 0)
35             name = idToName(id-1) + name;
36         return name;
37     }
38
39 }