1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.layer0.utils;
14 import java.util.HashMap;
17 import org.simantics.db.Resource;
20 * Service that creates unique names for resources and
23 public class NameMap {
26 Map<Resource, String> cache = new HashMap<Resource, String>();
29 public NameMap(int maxLength) {
30 this.maxLength = maxLength;
33 protected boolean isValid(String name) {
37 private static final int charA = 65;
38 private static final int char0 = 48 - 26;
45 public String getName(Resource resource) {
46 String ret = cache.get(resource);
49 byte[] c = new byte[5];
51 for(int i=0;i<4;++i) {
54 c[4-i] = (byte)(charA + lid);
56 c[4-i] = (byte)(char0 + lid);
61 cache.put(resource, ret);
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() {
74 public int getThreadHash() {
79 public long getResourceId() {
84 public Resource get() {
89 public boolean isPersistent() {
94 public int compareTo(Resource o) {
95 // TODO Auto-generated method stub
100 public boolean equalsResource(Resource other) {
101 return equals(other);
108 public String getName(Resource[] resources) {
110 for(Resource r : resources)