001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.aspects.naming;
019    
020    import java.util.Hashtable;
021    import java.util.Iterator;
022    import java.util.Map;
023    
024    /**
025     * Generate names for objects using the classname and an integer
026     * counter.
027     */
028    public class NameGenerator extends Hashtable {
029    
030        /**
031         * Creates a new NameGenerator
032         */
033        public NameGenerator() {
034        }
035    
036        public synchronized String generateName(String className) {
037            Long n = (Long)get(className);
038            if (n==null) {
039                n = new Long(0);
040            }
041            // Just appending the counter to the classname is dangerous
042            // because if you have 2 classes C1 and C11, you'll get name clashes
043            String res = 
044                className.substring(className.lastIndexOf('.')+1).toLowerCase() 
045                + "#"+ n;
046            put(className,new Long(n.longValue()+1));
047            return res;
048        }
049    
050        /**
051         * Parses a name and returns its counter
052         */
053        public static long getCounterFromName(String name) {
054            return Long.parseLong(name.substring(name.indexOf('#')+1));
055        }
056    
057        /**
058         * Gets the value of a counter
059         */
060        public long getCounter(String className) {
061            Long counter = (Long)get(className);
062            return counter!=null?counter.longValue():-1;
063        }
064    
065        /**
066         * Sets a counter
067         */
068        public void setCounter(String className, long count) {
069            put(className,new Long(count));
070        }
071    
072        /**
073         * 
074         */
075        public synchronized void update(Map counters) {
076            Iterator i = counters.entrySet().iterator();
077            while(i.hasNext()) {
078                Map.Entry entry = (Map.Entry)i.next();
079                String name = (String)entry.getKey();
080                Long value = (Long)entry.getValue();
081                long current = getCounter(name);
082                if (current<value.longValue())
083                    put(name,value);
084            }
085        }
086    }
087