Created
July 9, 2018 23:24
-
-
Save nicholasjackson827/eb196f376ca0e624b0c690677b49bf86 to your computer and use it in GitHub Desktop.
Sequential Alphanumeric IDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AlphanumericIdUtil { | |
private static final String VALID_CHARACTERS = "23456789abcdefghjkmnpqrstuvwxyz"; | |
public static void main(String[] args) { | |
System.out.println(getNumberFromId("a8e2fz")); | |
System.out.println(getIdFromNumber(24)); | |
} | |
/** | |
* Go from number (like 12345) to alphanumeric ID (like a8e7z4) | |
* | |
* @param number The number you wish to convert to ID | |
* @return the alphanumeric ID as a string | |
*/ | |
public static String getIdFromNumber(Integer number) { | |
// The magic number we're trying to get down to 0 | |
long magicNumber = number; | |
// Some placeholder values | |
long wholeNum = 0; | |
int remainder = 0; | |
String id = ""; | |
// The value we'll use as our modulo, just stored in a variable because it's simpler | |
int modVal = VALID_CHARACTERS.length(); | |
// Loop until the magic number (AKA the whole number) is not zero (>0 also works) | |
while (magicNumber != 0) { | |
// First, take the magic number, divide it by the modulo value, and store that as our whole | |
// number | |
wholeNum = (long) Math.floor(magicNumber / modVal); | |
// Next, find the remainder of when we take the magic number and divide it by the modulo value | |
remainder = (int) magicNumber % modVal; | |
// Then, take the remainder that we just got, find what position it is in our valid | |
// characters, and store it in our ID (yes, this means the ID is backwards, but we fix that | |
// later) | |
id += VALID_CHARACTERS.charAt(remainder); | |
// Lastly, make the magic number the whole number from above (since we don't need to deal with | |
// the remainder) | |
magicNumber = wholeNum; | |
// Loop again until the whole num is zero! | |
} | |
// One last thing: reverse the string. StringBuilder has a nice reverse method, so that's what | |
// I'm using to reverse the string. | |
return new StringBuilder(id).reverse().toString(); | |
} | |
/** | |
* Go from alphanumeric ID (like a8e7z4) to number (like 12345) | |
* | |
* @param id The string with the alphanumeric ID | |
* @return The number as an int | |
*/ | |
public static Integer getNumberFromId(String id) { | |
// A placeholder for the base 10 number | |
Integer base10Number = 0; | |
// Loop over each character in the input string | |
for (int i = 0; i < id.length(); i++) { | |
// Get the "current" character | |
char c = id.charAt(i); | |
// Find where that character is in the list of valid characters | |
// TODO: What do we do if it's not in that list? | |
int index = VALID_CHARACTERS.indexOf(c); | |
// Check if the character was able to be found | |
if (index == -1) { | |
throw new IllegalArgumentException("Character \"" + c | |
+ "\" is not a valid character. The only valid characters are: " + VALID_CHARACTERS); | |
} | |
// Take the base (31) to the power of the total number of characters, minus i, minus 1, and | |
// mutiply it by the index | |
base10Number += (int) Math.pow(VALID_CHARACTERS.length(), id.length() - i - 1) * index; | |
} | |
return base10Number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment