]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/NameMap.java b/bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/NameMap.java
new file mode 100644 (file)
index 0000000..21b7111
--- /dev/null
@@ -0,0 +1,115 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.layer0.utils;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.Resource;\r
+\r
+/**\r
+ * Service that creates unique names for resources and\r
+ * resource paths. \r
+ */\r
+public class NameMap {\r
+\r
+    int maxLength;\r
+    Map<Resource, String> cache = new HashMap<Resource, String>();\r
+    int curId = 0;\r
+    \r
+    public NameMap(int maxLength) {\r
+        this.maxLength = maxLength;\r
+    }\r
+    \r
+    protected boolean isValid(String name) {\r
+        return true;\r
+    }\r
+    \r
+    private static final int charA = 65;\r
+    private static final int char0 = 48 - 26;\r
+    \r
+    public void clear() {\r
+       curId = 0;\r
+       cache.clear();\r
+    }\r
+    \r
+    public String getName(Resource resource) {\r
+        String ret = cache.get(resource);\r
+        while(ret == null) {\r
+            int id = curId++;\r
+            byte[] c = new byte[5];\r
+            c[0] = '@';\r
+            for(int i=0;i<4;++i) {\r
+                int lid = id%36;\r
+                if(lid < 26)\r
+                    c[4-i] = (byte)(charA + lid);\r
+                else\r
+                    c[4-i] = (byte)(char0 + lid);\r
+                id /= 36;\r
+            }                \r
+            ret = new String(c);\r
+            if(isValid(ret))\r
+                cache.put(resource, ret);\r
+            else\r
+                ret = null;\r
+        }\r
+        return ret;\r
+    }\r
+    \r
+    public static void main(String[] args) {\r
+               NameMap m = new NameMap(24);\r
+               for(int i=0;i<100;++i)\r
+                       System.out.println(m.getName(new Resource() {\r
+\r
+                               @Override\r
+                               public int getThreadHash() {\r
+                                       return hashCode();\r
+                               }\r
+                               \r
+                               @Override\r
+                               public long getResourceId() {\r
+                                       return 3;\r
+                               }\r
+\r
+                               @Override\r
+                               public Resource get() {\r
+                                       return this;\r
+                               }\r
+\r
+                               @Override\r
+                               public boolean isPersistent() {\r
+                                       return false;\r
+                               }\r
+                               \r
+                               @Override\r
+                               public int compareTo(Resource o) {\r
+                                       // TODO Auto-generated method stub\r
+                                       return 0;\r
+                               }\r
+\r
+                               @Override\r
+                               public boolean equalsResource(Resource other) {\r
+                                       return equals(other);\r
+                               }\r
+                               \r
+                       }));\r
+                       \r
+       }\r
+    \r
+    public String getName(Resource[] resources) {\r
+        String ret = "";\r
+        for(Resource r : resources)\r
+            ret += getName(r);\r
+        return ret;\r
+    }\r
+    \r
+}\r