import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class Main {

    /**
     * La classe genera identificatori casuali. Ulteriori informazioni
     * {@link org.buscati.knowledge.example}
     * 
     * @author itammb ( Italia Massimiliano Buscati )
     * @version JDK 1.15
     */
    public static class Example_001_JavaDoc {
    
    	private final static Random random = new Random();
    
    	static {
    		ALPHA_NUM = "abcdefghijklmnopqrstuvwxyz0123456789";
    	}
    
    	/**
    	 * @deprecated Spostato nel package matematica
    	 */
    	public final static boolean EVEN = true;
    
    	/**
    	 * @deprecated Spostato nel package matematica
    	 */
    	public final static boolean ODD = false;
    
    	/**
    	 * Sequenza alfanumerica di default
    	 * 
    	 */
    	public final static String ALPHA_NUM;
    
    	/**
    	 * Formato: anno-mese-giorno.ora.minuti.secondi.millisecondi
    	 */
    	public final static String PATTERN_DATETIME = "yyyy-MM-dd.hh.mm.ss.SSS";
    
    	/**
    	 * @return Sequenza temporale alfanumerica univoca
    	 * 
    	 * @throws InterruptedException
    	 * 
    	 * @see Locale
    	 * @see SimpleDateFormat
    	 * @see <a href="http://[Log in to view URL]" target="_blank">buscati.org</a>
    	 * 
    	 * @since JDK 1.15
    	 */
    	public static synchronized String idTimeUnique() throws InterruptedException {
    		Thread.sleep(1);
    
    		return new SimpleDateFormat(PATTERN_DATETIME, Locale.getDefault()).format(new Date(System.currentTimeMillis()))
    				.replace('-', '_').replace(' ', '_').replace('.', '_');
    	}
    
    	/**
    	 * F&#224; la divisione per 2 e ne valuta il resto
    	 * 
    	 * @return {@value #EVEN} se pari. Altrimenti, {@value #ODD} se dispari
    	 * 
    	 * @since JDK 1.15
    	 * 
    	 * @deprecated Spostato nel package matematica
    	 */
    	public static boolean isOdd(int number) {
    		if (number % 2 == 0)
    			return EVEN;
    		else
    			return ODD;
    	}
    
    	/**
    	 * @param userAlphaNum Sequenza alfanumerica utente. Pu&#242; essere
    	 *                     {@code null}
    	 * @return Sequenza alfanumerica casuale
    	 * 
    	 * @see StringBuilder
    	 * @see Random
    	 * @see #ALPHA_NUM
    	 * 
    	 * @since JDK 1.15
    	 */
    	public static String idAlphaNum(String userAlphaNum) {
    		if (userAlphaNum == null)
    			userAlphaNum = ALPHA_NUM;
    
    		int len = userAlphaNum.length();
    
    		StringBuilder sb = new StringBuilder(len);
    
    		for (int i = 0; i < len; i++)
    			sb.append(userAlphaNum.charAt(random.nextInt(userAlphaNum.length())));
    
    		return sb.toString();
    	}
    
    	/**
    	 * @return Numero intero casuale compreso tra un min e max ( incluso ) dove max
    	 *         &gt; min
    	 * 
    	 * @throws IllegalArgumentException se il calcolo ( max - min ) &lt;= 0
    	 * 
    	 * @see StringBuilder
    	 * @see Random
    	 * 
    	 * @since JDK 1.15
    
    	 * <pre>
    	 *Usa {@link Random#nextInt()} non thread-safe
    	 * 
    	 *{@code random.nextInt( ( numberMax - numberMin ) + 1 ) + numberMin;}
    	 * </pre>
    	 */
    	public static int intRandom(int numberMin, int numberMax) throws IllegalArgumentException {
    		return random.nextInt((numberMax - numberMin) + 1) + numberMin;
    	}
    }

   public static void main(String args[]) throws Exception {
        // Unit test - genera un identificatore casuale tra 0..10
		System.out.println( "Estrai un intero casuale: " + new Example_001_JavaDoc().intRandom(0, 10));
   }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: