Coverage details for base.jdbs.GUIDGenerator

LineHitsSource
1 /*******************************************************************************
2  *InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package base.jdbs;
21  
22 import java.math.BigInteger;
23 import java.security.SecureRandom;
24  
25 import org.apache.log4j.Logger;
26  
270public class GUIDGenerator {
28     
290    private static final transient Logger logger = Logger.getLogger(GUIDGenerator.class.getName());
30     
31     /**The secure random to use when must be build a global unique identifier.**/
32     private SecureRandom secureRandom;
33     /**The GUIDGenerator instance according to the Singleton pattern.**/
340    private static GUIDGenerator instance = null;
35     
36     
37     /**
38      * @return Returns the instance.
39      */
40     public static GUIDGenerator getInstance(){
410        return instance!=null ? instance : (instance = new GUIDGenerator());
42     }
43     
44     /**
45      * The default constructor.
46      */
470    protected GUIDGenerator(){
480        this.secureRandom = new SecureRandom();
490        logger.debug(secureRandom.getProvider().getInfo());
500    }
51     
52     /**
53      * This method returns a alphanumeric key that is unique to a millisecond. The uniqueness
54      * is increased by appending a random number to the key.
55      * @return The unique key.
56      */
57     public String getAlphanumericKey() {
58         // Append current time in millis to a hexadecimal representation
59         // of a random number.
600        String key = "" + System.currentTimeMillis() + Long.toHexString(secureRandom.nextInt());
610        logger.debug("getAlphanumericKey(): "+key);
620        return key;
63     }
64     
65     /**
66      * This method returns a numeric key that is unique to a millisecond. The uniqueness
67      * is increased by appending a random number to the key.
68      * @return The unique key.
69      */
70     public BigInteger getNumericKey() {
71         // Append current time in millis to a hexadecimal representation
72         // of a random number.
730        String firstToken = ""+System.currentTimeMillis();
740        Long secureToken = secureRandom.nextLong();
750        String lastToken = secureToken > 0 ? ""+secureToken : ""+(-1 * secureToken);
760        BigInteger key = new BigInteger(firstToken+lastToken);
770        logger.debug("getNumericKey(): "+key);
780        return key;
79     }
80     
81 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.