@Test public void testIncrement() { IdGenerator testGenerator = new IdGenerator(IdAlphabets.COLLECTION_ID, true); String[] inputs = new String[]{ "0000", "A", "AZ00", "Az00", "Zz00", "zz00", "zzz0", "zzzz" }; for (String input : inputs) { verifyIncrement(input, testGenerator.increment(input, 2)); } for (int i = 0; i < 50; i++) { String input = testGenerator.longToPaddedIdentifier(RANDOM.nextLong()); verifyIncrement(input, testGenerator.increment(input, RANDOM.nextInt(input.length()))); } }
public String increment(String fullId, int proposedCutoff) { StringBuilder mutable = new StringBuilder(fullId); if (proposedCutoff <= fullId.length()) { for (int i = proposedCutoff - 1; i > 0; i--) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } else { mutable.setCharAt(i, mAlphabet.encodeDigit(0)); } } if (proposedCutoff > 0) { int newValue = mAlphabet.decodeLeadingDigit(mutable.charAt(0)) + 1; if (newValue < mAlphabet.leadingRadix()) { mutable.setCharAt(0, mAlphabet.encodeLeadingDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } } mutable.setLength(0); mutable.append(fullId); for (int i = proposedCutoff; i < fullId.length(); i++) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(i + 1); return mutable.toString(); } } } mutable.append(mAlphabet.encodeDigit(0)); return mutable.toString(); }
IdGenerator { public String increment(String fullId, int proposedCutoff) { StringBuilder mutable = new StringBuilder(fullId); if (proposedCutoff <= fullId.length()) { for (int i = proposedCutoff - 1; i > 0; i--) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } else { mutable.setCharAt(i, mAlphabet.encodeDigit(0)); } } if (proposedCutoff > 0) { int newValue = mAlphabet.decodeLeadingDigit(mutable.charAt(0)) + 1; if (newValue < mAlphabet.leadingRadix()) { mutable.setCharAt(0, mAlphabet.encodeLeadingDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } } mutable.setLength(0); mutable.append(fullId); for (int i = proposedCutoff; i < fullId.length(); i++) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(i + 1); return mutable.toString(); } } } mutable.append(mAlphabet.encodeDigit(0)); return mutable.toString(); } }
IdGenerator { public String increment(String fullId, int proposedCutoff) { StringBuilder mutable = new StringBuilder(fullId); if (proposedCutoff <= fullId.length()) { for (int i = proposedCutoff - 1; i > 0; i--) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } else { mutable.setCharAt(i, mAlphabet.encodeDigit(0)); } } if (proposedCutoff > 0) { int newValue = mAlphabet.decodeLeadingDigit(mutable.charAt(0)) + 1; if (newValue < mAlphabet.leadingRadix()) { mutable.setCharAt(0, mAlphabet.encodeLeadingDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } } mutable.setLength(0); mutable.append(fullId); for (int i = proposedCutoff; i < fullId.length(); i++) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(i + 1); return mutable.toString(); } } } mutable.append(mAlphabet.encodeDigit(0)); return mutable.toString(); } IdGenerator(IdAlphabet alphabet, boolean perturb); }
IdGenerator { public String increment(String fullId, int proposedCutoff) { StringBuilder mutable = new StringBuilder(fullId); if (proposedCutoff <= fullId.length()) { for (int i = proposedCutoff - 1; i > 0; i--) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } else { mutable.setCharAt(i, mAlphabet.encodeDigit(0)); } } if (proposedCutoff > 0) { int newValue = mAlphabet.decodeLeadingDigit(mutable.charAt(0)) + 1; if (newValue < mAlphabet.leadingRadix()) { mutable.setCharAt(0, mAlphabet.encodeLeadingDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } } mutable.setLength(0); mutable.append(fullId); for (int i = proposedCutoff; i < fullId.length(); i++) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(i + 1); return mutable.toString(); } } } mutable.append(mAlphabet.encodeDigit(0)); return mutable.toString(); } IdGenerator(IdAlphabet alphabet, boolean perturb); String longToPaddedIdentifier(long x); String perturb(String baseId); String increment(String fullId, int proposedCutoff); String increment(String previous); String generateTailId(); void registerId(String id); }
IdGenerator { public String increment(String fullId, int proposedCutoff) { StringBuilder mutable = new StringBuilder(fullId); if (proposedCutoff <= fullId.length()) { for (int i = proposedCutoff - 1; i > 0; i--) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } else { mutable.setCharAt(i, mAlphabet.encodeDigit(0)); } } if (proposedCutoff > 0) { int newValue = mAlphabet.decodeLeadingDigit(mutable.charAt(0)) + 1; if (newValue < mAlphabet.leadingRadix()) { mutable.setCharAt(0, mAlphabet.encodeLeadingDigit(newValue)); mutable.setLength(proposedCutoff); return mutable.toString(); } } mutable.setLength(0); mutable.append(fullId); for (int i = proposedCutoff; i < fullId.length(); i++) { int newValue = mAlphabet.decodeDigit(mutable.charAt(i)) + 1; if (newValue < mAlphabet.radix()) { mutable.setCharAt(i, mAlphabet.encodeDigit(newValue)); mutable.setLength(i + 1); return mutable.toString(); } } } mutable.append(mAlphabet.encodeDigit(0)); return mutable.toString(); } IdGenerator(IdAlphabet alphabet, boolean perturb); String longToPaddedIdentifier(long x); String perturb(String baseId); String increment(String fullId, int proposedCutoff); String increment(String previous); String generateTailId(); void registerId(String id);  }