KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > naming > NameGenerator


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.naming;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * Generate names for objects using the classname and an integer
26  * counter.
27  */

28 public class NameGenerator extends Hashtable JavaDoc {
29
30     /**
31      * Creates a new NameGenerator
32      */

33     public NameGenerator() {
34     }
35
36     public synchronized String JavaDoc generateName(String JavaDoc className) {
37         Long JavaDoc n = (Long JavaDoc)get(className);
38         if (n==null) {
39             n = new Long JavaDoc(0);
40         }
41         // Just appending the counter to the classname is dangerous
42
// because if you have 2 classes C1 and C11, you'll get name clashes
43
String JavaDoc res =
44             className.substring(className.lastIndexOf('.')+1).toLowerCase()
45             + "#"+ n;
46         put(className,new Long JavaDoc(n.longValue()+1));
47         return res;
48     }
49
50     /**
51      * Parses a name and returns its counter
52      */

53     public static long getCounterFromName(String JavaDoc name) {
54         return Long.parseLong(name.substring(name.indexOf('#')+1));
55     }
56
57     /**
58      * Gets the value of a counter
59      */

60     public long getCounter(String JavaDoc className) {
61         Long JavaDoc counter = (Long JavaDoc)get(className);
62         return counter!=null?counter.longValue():-1;
63     }
64
65     /**
66      * Sets a counter
67      */

68     public void setCounter(String JavaDoc className, long count) {
69         put(className,new Long JavaDoc(count));
70     }
71
72     /**
73      *
74      */

75     public synchronized void update(Map JavaDoc counters) {
76         Iterator JavaDoc i = counters.entrySet().iterator();
77         while(i.hasNext()) {
78             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
79             String JavaDoc name = (String JavaDoc)entry.getKey();
80             Long JavaDoc value = (Long JavaDoc)entry.getValue();
81             long current = getCounter(name);
82             if (current<value.longValue())
83                 put(name,value);
84         }
85     }
86 }
87
88
Popular Tags