TaskUtils { public static String generateTaskId() { int keepTrying = 0; while ( ++keepTrying < TaskUtils.MAX_ATTEMPTS ) { String id = RandomStringUtils.randomAlphanumeric( TaskUtils.KEY_LENGTH ).toUpperCase(); if ( TaskUtils.usedIds.isEmpty() || !TaskUtils.usedIds.contains( id ) ) { TaskUtils.usedIds.add( id ); return id; } } throw new IllegalStateException( "Failed to find a unique task id in " + TaskUtils.MAX_ATTEMPTS + " iterations" ); }  static String generateTaskId();  }
@Test public final void testGenerateTaskId() { Collection<String> seen = new HashSet<>(); for ( int i = 0; i < 1000; i++ ) { String id = TaskUtils.generateTaskId(); assertTrue( StringUtils.isNotBlank( id ) ); assertTrue( !seen.contains( id ) ); seen.add( id ); } }