Permalink
Browse files

Migrate draft of jsoda package

  • Loading branch information...
williamw520 committed Jan 19, 2012
1 parent 422e2ea commit 89550d0ac7d988ce9eabcba216d299d4622ed76b
View
@@ -1,4 +1,4 @@
build
dist
-lib\*.jar
+lib/*.jar
*.class
View
@@ -40,6 +40,7 @@
<javadoc packagenames="wwutil.db.*,wwutil.jsoda.*"
sourcepath="${src.dir}"
defaultexcludes="yes"
+ classpathref="compile.classpath"
destdir="${javadoc.dir}"
author="true"
version="true"
View
@@ -1,3 +1,6 @@
-UTest library dependency. Copy the following jar files to this directory.
+Library dependency. Copy the following jar files to this directory to enable compilation.
+- AWS Java SDK, aws-java-sdk-1.3.0.jar or above
+- Javax Persistence, ejb3-persistence.jar
+- Apache Commons BeanUtils, commons-beanutils-1.8.3.jar or above
@@ -0,0 +1,109 @@
+
+
+package wwutil.jsoda;
+
+import java.io.*;
+import java.nio.*;
+import java.util.*;
+import java.math.BigInteger;
+
+
+
+/**
+ * Encoder for BaseX
+ */
+public class BaseXUtil
+{
+ // Base62
+ public static final String DIGITS_62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+ // Base58 Base62 minus the 0 O 1 l
+ public static final String DIGITS_58 = "23456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
+
+
+ /**
+ * Encodes a number using BaseX encoding.
+ *
+ * @param number a positive integer
+ * @param alphabets is the set of alphabet for encoding.
+ * @return a BaseX string
+ * @throws IllegalArgumentException if <code>number</code> is negative
+ */
+ public static String encode(BigInteger number, String alphabets) {
+ if (number.compareTo(BigInteger.ZERO) == -1)
+ throw new IllegalArgumentException("Number cannot be negative");
+
+ StringBuilder sb = new StringBuilder();
+ BigInteger base = BigInteger.valueOf(alphabets.length());
+ BigInteger[] divrem = new BigInteger[] {number, BigInteger.ZERO};
+
+ while (divrem[0].compareTo(BigInteger.ZERO) == 1) {
+ divrem = divrem[0].divideAndRemainder(base);
+ sb.append(alphabets.charAt(divrem[1].intValue()));
+ }
+ return (sb.length() == 0) ? alphabets.substring(0, 1) : sb.reverse().toString();
+ }
+
+ /**
+ * Decodes a string using BaseX encoding.
+ *
+ * @param str a BaseX String
+ * @param alphabets is the set of alphabet for encoding.
+ * @return a positive number
+ * @throws IllegalArgumentException if <code>str</code> is empty
+ */
+ public static BigInteger decode(final String str, String alphabets) {
+ if (str.length() == 0)
+ throw new IllegalArgumentException("Str cannot be empty");
+
+ BigInteger result = BigInteger.ZERO;
+ BigInteger base = BigInteger.valueOf(alphabets.length());
+ int digits = str.length();
+ for (int i = 0; i < digits; i++) {
+ int digit = alphabets.indexOf(str.charAt(digits - i - 1));
+ result = result.add(BigInteger.valueOf(digit).multiply(base.pow(i)));
+ }
+ return result;
+ }
+
+ public static String encode58(BigInteger number) {
+ return encode(number, DIGITS_58);
+ }
+
+ public static BigInteger decode58(final String str) {
+ return decode(str, DIGITS_58);
+ }
+
+ public static String encode62(BigInteger number) {
+ return encode(number, DIGITS_62);
+ }
+
+ public static BigInteger decode62(final String str) {
+ return decode(str, DIGITS_62);
+ }
+
+ public static String uuidToBase58(UUID uuid) {
+ ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
+ bb.putLong(uuid.getMostSignificantBits());
+ bb.putLong(uuid.getLeastSignificantBits());
+ BigInteger number = new BigInteger(bb.array()).abs();
+ return encode58(number);
+ }
+
+ public static String uuid16() {
+ UUID uuid = UUID.randomUUID();
+ ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
+ bb.putLong(uuid.getMostSignificantBits());
+ bb.putLong(uuid.getLeastSignificantBits());
+ BigInteger number = new BigInteger(bb.array()).abs();
+ return encode58(number);
+ }
+
+ public static String uuid8() {
+ UUID uuid = UUID.randomUUID();
+ long val = uuid.getMostSignificantBits() ^ uuid.getLeastSignificantBits();
+ BigInteger number = BigInteger.valueOf(val).abs();
+ return encode58(number);
+ }
+
+}
+
@@ -0,0 +1,76 @@
+
+package wwutil.jsoda;
+
+import java.util.*;
+import java.lang.reflect.*;
+
+import org.apache.commons.beanutils.ConvertUtils;
+import com.amazonaws.services.simpledb.util.SimpleDBUtils;
+
+
+
+class DataUtil
+{
+ public static final String NULL_STR = "_null_"; // null encoding
+
+
+ static String getFieldValueStr(Object dataObj, Field field)
+ throws Exception
+ {
+ Object value = field.get(dataObj);
+ return toValueStr(value);
+ }
+
+ static void setFieldValueStr(Object dataObj, Field field, String valueStr)
+ throws Exception
+ {
+ Object value = toValueObj(field.getType(), valueStr);
+ field.set(dataObj, value);
+ }
+
+ static String toValueStr(Object value)
+ throws Exception
+ {
+ if (value == null)
+ return NULL_STR;
+
+ if (value instanceof Integer)
+ return SimpleDBUtils.encodeZeroPadding(((Integer)value).intValue(), 10);
+ if (value instanceof Long)
+ return SimpleDBUtils.encodeZeroPadding(((Long)value).longValue(), 19);
+ if (value instanceof Float)
+ return SimpleDBUtils.encodeZeroPadding(((Float)value).floatValue(), 16);
+ if (value instanceof Date)
+ return SimpleDBUtils.encodeDate((Date)value);
+
+ return value.toString();
+ }
+
+ /**
+ * valueType = Field.getType()
+ */
+ static Object toValueObj(Class valueType, String valueStr)
+ throws Exception
+ {
+ // Set null if input is null, or non-String field having "" or "null" input.
+ if (valueStr == null ||
+ valueStr.equals(NULL_STR) ||
+ (valueType != String.class && valueStr.equals(""))) {
+ return null;
+ }
+
+ if (valueType == Integer.class)
+ return SimpleDBUtils.decodeZeroPaddingInt(valueStr);
+ if (valueType == Long.class)
+ return SimpleDBUtils.decodeZeroPaddingLong(valueStr);
+ if (valueType == Float.class)
+ return SimpleDBUtils.decodeZeroPaddingFloat(valueStr);
+ if (valueType == Double.class)
+ return SimpleDBUtils.decodeZeroPaddingFloat(valueStr);
+ if (valueType == Date.class)
+ return SimpleDBUtils.decodeDate(valueStr);
+
+ return ConvertUtils.convert(valueStr, valueType);
+ }
+
+}
Oops, something went wrong.

0 comments on commit 89550d0

Please sign in to comment.