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