Skip to content

Instantly share code, notes, and snippets.

@eaorak
Last active December 14, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eaorak/5126436 to your computer and use it in GitHub Desktop.
Save eaorak/5126436 to your computer and use it in GitHub Desktop.
Information shortener, can be used for shorten info like urls.
/**
* Abstract generic info shortener that can be modified
* to generate short ids for desired classes.
*
* @author eaorak
*/
public abstract class InfoShortener<T> {
private static int ALPCNT = 62;
private static String cstr = new String();
private final int threshold;
static {
for (int i = '0'; i <= 'z'; i++) {
if (Character.isAlphabetic(i) || Character.isDigit(i)) {
cstr += (char) i;
}
}
}
/**
* @param threshold Minimum number to start
*/
public InfoShortener(int threshold) {
this.threshold = threshold;
}
/**
* Shorten given string info.
*
* @param str String to shorten
* @return Shortened info string
*/
public String shorten(String str) {
return this.convertToStr(this.getIdFor(str) + this.threshold);
}
/**
* Reverse given shortened info to actual one.
*
* @param str Shortened info string
* @return Actual info string
*/
public T reverse(String str) {
return this.getInfoFor(this.convertToId(str) - this.threshold);
}
protected abstract long getIdFor(String str);
protected abstract T getInfoFor(long id);
protected long convertToId(String str) {
long num = 0;
for (int i = 0; i < str.length(); i++) {
num += Math.pow(ALPCNT, i) * this.cstr.indexOf(str.charAt(i));
}
return num;
}
protected String convertToStr(long id) {
StringBuilder str = new StringBuilder();
while (id > 0) {
long x = id % ALPCNT;
id = (id - x) / ALPCNT;
str.append(cstr.charAt((int) x));
}
return str.toString();
}
}
/**
* Sample class that extends InfoShortener to generate shortened strings
* for Camera serial numbers, then back to Camera objects.
*/
public class SampleGenerator extends InfoShortener<Camera> {
public SampleGenerator() {
super(1000);
}
@Override
protected long getIdFor(String str) {
// Find camera object with serial (str) from database
Camera camera = Camera.find("serial_no = ?", str).first();
if (camera == null) {
throw new RuntimeException("No camera could be found with serial number: " + str);
}
return camera.getId();
}
@Override
protected Camera getInfoFor(long id) {
// Find camera object with given id from database
Camera camera = Camera.findById(id);
if (camera == null) {
throw new RuntimeException("No camera could be found with id: " + id);
}
return camera;
}
// Sample execution
public static void main(String[] args) {
SampleGenerator gen = new SampleGenerator();
String shortened = gen.shorten("29jd37hd39dehbv");
System.out.println(shortened);
// 9G
System.out.println(gen.reverse(shortened));
// Camera [serialNo=29jd37hd39dehbv, model=cm512, status=ACTIVE]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment