VariableNameGenerator implements Supplier<Optional<String>> { @Override public Optional<String> get() { try { if (n >= 0 && n < LENGTH) { return Optional.of(String.format("%s%s", CHAR1.charAt(n % CHAR1.length()), CHAR2.charAt(n / CHAR1.length()) ).trim()); } return Optional.empty(); } finally { n += 1; } }  @Override Optional<String> get(); static final String CHAR1; static final String CHAR2; static final int LENGTH; }
@Test public void testNameSequence() { Map<Integer,String> expecteds = new HashMap<>(); expecteds.put(0, "A"); expecteds.put(25, "Z"); expecteds.put(26, "AA"); expecteds.put(51, "ZA"); expecteds.put(52, "AB"); expecteds.put(77, "ZB"); expecteds.put(VariableNameGenerator.LENGTH-1, "Z9"); int lastCheck = expecteds.keySet().stream().max(Integer::compare).get(); VariableNameGenerator gen = new VariableNameGenerator(); for (int i = 0; i <= lastCheck; i++) { String varName = gen.get().orElseThrow(() -> new RuntimeException("Ran out of variable names too early!")); if (expecteds.containsKey(i)) { assertEquals(expecteds.get(i), varName); } } }
@Test public void testSequenceLength() { VariableNameGenerator gen = new VariableNameGenerator(); int count = 0; while (gen.get().isPresent()) count++; assertEquals(VariableNameGenerator.LENGTH, count); }