X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.layer0.utils%2Fsrc%2Forg%2Fsimantics%2Flayer0%2Futils%2FNameMap.java;h=431f07574a1b2c5f9175073000875195ab8198ab;hb=452670c58399d8054872655841ebb6e66d9c6b6e;hp=21b7111a8bf5ed0bc863fab0d555fbd11155f5cd;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git 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 index 21b7111a8..431f07574 100644 --- 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 @@ -1,115 +1,115 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.layer0.utils; - -import java.util.HashMap; -import java.util.Map; - -import org.simantics.db.Resource; - -/** - * Service that creates unique names for resources and - * resource paths. - */ -public class NameMap { - - int maxLength; - Map cache = new HashMap(); - int curId = 0; - - public NameMap(int maxLength) { - this.maxLength = maxLength; - } - - protected boolean isValid(String name) { - return true; - } - - private static final int charA = 65; - private static final int char0 = 48 - 26; - - public void clear() { - curId = 0; - cache.clear(); - } - - public String getName(Resource resource) { - String ret = cache.get(resource); - while(ret == null) { - int id = curId++; - byte[] c = new byte[5]; - c[0] = '@'; - for(int i=0;i<4;++i) { - int lid = id%36; - if(lid < 26) - c[4-i] = (byte)(charA + lid); - else - c[4-i] = (byte)(char0 + lid); - id /= 36; - } - ret = new String(c); - if(isValid(ret)) - cache.put(resource, ret); - else - ret = null; - } - return ret; - } - - public static void main(String[] args) { - NameMap m = new NameMap(24); - for(int i=0;i<100;++i) - System.out.println(m.getName(new Resource() { - - @Override - public int getThreadHash() { - return hashCode(); - } - - @Override - public long getResourceId() { - return 3; - } - - @Override - public Resource get() { - return this; - } - - @Override - public boolean isPersistent() { - return false; - } - - @Override - public int compareTo(Resource o) { - // TODO Auto-generated method stub - return 0; - } - - @Override - public boolean equalsResource(Resource other) { - return equals(other); - } - - })); - - } - - public String getName(Resource[] resources) { - String ret = ""; - for(Resource r : resources) - ret += getName(r); - return ret; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.layer0.utils; + +import java.util.HashMap; +import java.util.Map; + +import org.simantics.db.Resource; + +/** + * Service that creates unique names for resources and + * resource paths. + */ +public class NameMap { + + int maxLength; + Map cache = new HashMap(); + int curId = 0; + + public NameMap(int maxLength) { + this.maxLength = maxLength; + } + + protected boolean isValid(String name) { + return true; + } + + private static final int charA = 65; + private static final int char0 = 48 - 26; + + public void clear() { + curId = 0; + cache.clear(); + } + + public String getName(Resource resource) { + String ret = cache.get(resource); + while(ret == null) { + int id = curId++; + byte[] c = new byte[5]; + c[0] = '@'; + for(int i=0;i<4;++i) { + int lid = id%36; + if(lid < 26) + c[4-i] = (byte)(charA + lid); + else + c[4-i] = (byte)(char0 + lid); + id /= 36; + } + ret = new String(c); + if(isValid(ret)) + cache.put(resource, ret); + else + ret = null; + } + return ret; + } + + public static void main(String[] args) { + NameMap m = new NameMap(24); + for(int i=0;i<100;++i) + System.out.println(m.getName(new Resource() { + + @Override + public int getThreadHash() { + return hashCode(); + } + + @Override + public long getResourceId() { + return 3; + } + + @Override + public Resource get() { + return this; + } + + @Override + public boolean isPersistent() { + return false; + } + + @Override + public int compareTo(Resource o) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public boolean equalsResource(Resource other) { + return equals(other); + } + + })); + + } + + public String getName(Resource[] resources) { + String ret = ""; + for(Resource r : resources) + ret += getName(r); + return ret; + } + +}