@Test public void testLongToPaddedIdentifier() { IdGenerator testGeneratorA = new IdGenerator(IdAlphabets.COLLECTION_ID, true), testGeneratorB = new IdGenerator(IdAlphabets.ROW_NAME, true); SortedSet<Long> sortedInputs = new TreeSet<>(Arrays.asList( Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2, -1L, 0L, 1L, Long.MAX_VALUE - 2, Long.MAX_VALUE - 1, Long.MAX_VALUE)); for (int i = 0; i < 50; i++) { sortedInputs.add(RANDOM.nextLong()); } String lastIdA = null, lastIdB = null; for (long input : sortedInputs) { String testId = testGeneratorA.longToPaddedIdentifier(input); if (lastIdA != null) { assertTrue("IdGenerator.longToPaddedIdentifier generates IDs consistent with " + "input ordering (" + lastIdA + " < " + testId + ")", testId.compareTo(lastIdA) > 0); } lastIdA = testId; testId = testGeneratorB.longToPaddedIdentifier(input); if (lastIdB != null) { assertTrue("IdGenerator.longToPaddedIdentifier generates IDs consistent with " + "input ordering (" + lastIdB + " < " + testId + ")", testId.compareTo(lastIdB) > 0); } lastIdB = testId; } }
public String longToPaddedIdentifier(long x) { StringBuilder s = new StringBuilder(mLongEncodedLength); long unsignedWithSameOrdering = x - Long.MIN_VALUE; PartialDivisionResult signedSubproblem = transformToSignedDivision(unsignedWithSameOrdering); long leftToEncode = signedSubproblem.partialRemainder; for (int placeValue = 0; placeValue < mLongUniformRadixLength - 1; placeValue++) { if (leftToEncode > 0) { s.append(mAlphabet.encodeDigit((int) (leftToEncode % mAlphabet.radix()))); leftToEncode /= mAlphabet.radix(); } else { s.append(mAlphabet.encodeDigit(0)); } } int mostSignificantDigit = signedSubproblem.partialMostSignificantDigit + (int) leftToEncode; if (mLongEncodedLength > mLongUniformRadixLength) { s.append(mAlphabet.encodeDigit(mostSignificantDigit)) .append(mAlphabet.encodeLeadingDigit(0)); } else { s.append(mAlphabet.encodeLeadingDigit(mostSignificantDigit)); } return s.reverse().toString(); }
IdGenerator { public String longToPaddedIdentifier(long x) { StringBuilder s = new StringBuilder(mLongEncodedLength); long unsignedWithSameOrdering = x - Long.MIN_VALUE; PartialDivisionResult signedSubproblem = transformToSignedDivision(unsignedWithSameOrdering); long leftToEncode = signedSubproblem.partialRemainder; for (int placeValue = 0; placeValue < mLongUniformRadixLength - 1; placeValue++) { if (leftToEncode > 0) { s.append(mAlphabet.encodeDigit((int) (leftToEncode % mAlphabet.radix()))); leftToEncode /= mAlphabet.radix(); } else { s.append(mAlphabet.encodeDigit(0)); } } int mostSignificantDigit = signedSubproblem.partialMostSignificantDigit + (int) leftToEncode; if (mLongEncodedLength > mLongUniformRadixLength) { s.append(mAlphabet.encodeDigit(mostSignificantDigit)) .append(mAlphabet.encodeLeadingDigit(0)); } else { s.append(mAlphabet.encodeLeadingDigit(mostSignificantDigit)); } return s.reverse().toString(); } }
IdGenerator { public String longToPaddedIdentifier(long x) { StringBuilder s = new StringBuilder(mLongEncodedLength); long unsignedWithSameOrdering = x - Long.MIN_VALUE; PartialDivisionResult signedSubproblem = transformToSignedDivision(unsignedWithSameOrdering); long leftToEncode = signedSubproblem.partialRemainder; for (int placeValue = 0; placeValue < mLongUniformRadixLength - 1; placeValue++) { if (leftToEncode > 0) { s.append(mAlphabet.encodeDigit((int) (leftToEncode % mAlphabet.radix()))); leftToEncode /= mAlphabet.radix(); } else { s.append(mAlphabet.encodeDigit(0)); } } int mostSignificantDigit = signedSubproblem.partialMostSignificantDigit + (int) leftToEncode; if (mLongEncodedLength > mLongUniformRadixLength) { s.append(mAlphabet.encodeDigit(mostSignificantDigit)) .append(mAlphabet.encodeLeadingDigit(0)); } else { s.append(mAlphabet.encodeLeadingDigit(mostSignificantDigit)); } return s.reverse().toString(); } IdGenerator(IdAlphabet alphabet, boolean perturb); }
IdGenerator { public String longToPaddedIdentifier(long x) { StringBuilder s = new StringBuilder(mLongEncodedLength); long unsignedWithSameOrdering = x - Long.MIN_VALUE; PartialDivisionResult signedSubproblem = transformToSignedDivision(unsignedWithSameOrdering); long leftToEncode = signedSubproblem.partialRemainder; for (int placeValue = 0; placeValue < mLongUniformRadixLength - 1; placeValue++) { if (leftToEncode > 0) { s.append(mAlphabet.encodeDigit((int) (leftToEncode % mAlphabet.radix()))); leftToEncode /= mAlphabet.radix(); } else { s.append(mAlphabet.encodeDigit(0)); } } int mostSignificantDigit = signedSubproblem.partialMostSignificantDigit + (int) leftToEncode; if (mLongEncodedLength > mLongUniformRadixLength) { s.append(mAlphabet.encodeDigit(mostSignificantDigit)) .append(mAlphabet.encodeLeadingDigit(0)); } else { s.append(mAlphabet.encodeLeadingDigit(mostSignificantDigit)); } return s.reverse().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 longToPaddedIdentifier(long x) { StringBuilder s = new StringBuilder(mLongEncodedLength); long unsignedWithSameOrdering = x - Long.MIN_VALUE; PartialDivisionResult signedSubproblem = transformToSignedDivision(unsignedWithSameOrdering); long leftToEncode = signedSubproblem.partialRemainder; for (int placeValue = 0; placeValue < mLongUniformRadixLength - 1; placeValue++) { if (leftToEncode > 0) { s.append(mAlphabet.encodeDigit((int) (leftToEncode % mAlphabet.radix()))); leftToEncode /= mAlphabet.radix(); } else { s.append(mAlphabet.encodeDigit(0)); } } int mostSignificantDigit = signedSubproblem.partialMostSignificantDigit + (int) leftToEncode; if (mLongEncodedLength > mLongUniformRadixLength) { s.append(mAlphabet.encodeDigit(mostSignificantDigit)) .append(mAlphabet.encodeLeadingDigit(0)); } else { s.append(mAlphabet.encodeLeadingDigit(mostSignificantDigit)); } return s.reverse().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);  }