]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/NameMap.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / NameMap.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.layer0.utils;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 import org.simantics.db.Resource;\r
18 \r
19 /**\r
20  * Service that creates unique names for resources and\r
21  * resource paths. \r
22  */\r
23 public class NameMap {\r
24 \r
25     int maxLength;\r
26     Map<Resource, String> cache = new HashMap<Resource, String>();\r
27     int curId = 0;\r
28     \r
29     public NameMap(int maxLength) {\r
30         this.maxLength = maxLength;\r
31     }\r
32     \r
33     protected boolean isValid(String name) {\r
34         return true;\r
35     }\r
36     \r
37     private static final int charA = 65;\r
38     private static final int char0 = 48 - 26;\r
39     \r
40     public void clear() {\r
41         curId = 0;\r
42         cache.clear();\r
43     }\r
44     \r
45     public String getName(Resource resource) {\r
46         String ret = cache.get(resource);\r
47         while(ret == null) {\r
48             int id = curId++;\r
49             byte[] c = new byte[5];\r
50             c[0] = '@';\r
51             for(int i=0;i<4;++i) {\r
52                 int lid = id%36;\r
53                 if(lid < 26)\r
54                     c[4-i] = (byte)(charA + lid);\r
55                 else\r
56                     c[4-i] = (byte)(char0 + lid);\r
57                 id /= 36;\r
58             }                \r
59             ret = new String(c);\r
60             if(isValid(ret))\r
61                 cache.put(resource, ret);\r
62             else\r
63                 ret = null;\r
64         }\r
65         return ret;\r
66     }\r
67     \r
68     public static void main(String[] args) {\r
69                 NameMap m = new NameMap(24);\r
70                 for(int i=0;i<100;++i)\r
71                         System.out.println(m.getName(new Resource() {\r
72 \r
73                                 @Override\r
74                                 public int getThreadHash() {\r
75                                         return hashCode();\r
76                                 }\r
77                                 \r
78                                 @Override\r
79                                 public long getResourceId() {\r
80                                         return 3;\r
81                                 }\r
82 \r
83                                 @Override\r
84                                 public Resource get() {\r
85                                         return this;\r
86                                 }\r
87 \r
88                                 @Override\r
89                                 public boolean isPersistent() {\r
90                                         return false;\r
91                                 }\r
92                                 \r
93                                 @Override\r
94                                 public int compareTo(Resource o) {\r
95                                         // TODO Auto-generated method stub\r
96                                         return 0;\r
97                                 }\r
98 \r
99                                 @Override\r
100                                 public boolean equalsResource(Resource other) {\r
101                                         return equals(other);\r
102                                 }\r
103                                 \r
104                         }));\r
105                         \r
106         }\r
107     \r
108     public String getName(Resource[] resources) {\r
109         String ret = "";\r
110         for(Resource r : resources)\r
111             ret += getName(r);\r
112         return ret;\r
113     }\r
114     \r
115 }\r