You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2010/11/22 06:51:11 UTC

svn commit: r1037608 - in /wicket/trunk/wicket-util/src: main/java/org/apache/wicket/util/LongEncoder.java test/java/org/apache/wicket/util/LongEncoderTest.java

Author: ivaynberg
Date: Mon Nov 22 05:51:11 2010
New Revision: 1037608

URL: http://svn.apache.org/viewvc?rev=1037608&view=rev
Log:
long value encoder util

Added:
    wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/LongEncoder.java   (with props)
    wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/LongEncoderTest.java   (with props)

Added: wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/LongEncoder.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/LongEncoder.java?rev=1037608&view=auto
==============================================================================
--- wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/LongEncoder.java (added)
+++ wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/LongEncoder.java Mon Nov 22 05:51:11 2010
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.util;
+
+import org.apache.wicket.util.string.PrependingStringBuffer;
+
+/**
+ * Encodes long values into the specified alphabet. Encoding is useful when long values need to be
+ * represented in their string form and shorter values are preferred; by using alphabets of length
+ * greater than ten shorter values can be obtained. For example, to encode values into their
+ * hexadecimal representations the {@code 0123456789ABCDEF} can be used. Long values can be
+ * shortened even further by using longer alphabets.
+ * 
+ * @author igor
+ */
+public class LongEncoder
+{
+	/**
+	 * default alphabet that should be safe to use in most circumstances, while still providing good
+	 * shortening of long values
+	 */
+	public static String DEFAULT = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+	private LongEncoder()
+	{
+
+	}
+
+	/**
+	 * Encodes the value into the default alphabet: {@value #DEFAULT}
+	 * 
+	 * @param value
+	 * @return encoded value
+	 */
+	public static String encode(long value)
+	{
+		return encode(value, DEFAULT);
+	}
+
+	/**
+	 * Decodes value using the default alphabet: {@value #DEFAULT}
+	 * 
+	 * @param value
+	 * @return decoded value
+	 */
+	public static long decode(String value)
+	{
+		return decode(value, DEFAULT);
+	}
+
+	/**
+	 * Encodes value into the specified alphabet
+	 * 
+	 * @param value
+	 * @param alphabet
+	 * @return encoded value
+	 */
+	public static String encode(long value, String alphabet)
+	{
+		final int len = alphabet.length();
+		PrependingStringBuffer buff = new PrependingStringBuffer();
+		do
+		{
+			int mod = (int)(value % len);
+			buff.prepend(alphabet.charAt(mod));
+			value = value / len;
+		}
+		while (value > 0);
+		return buff.toString();
+	}
+
+	/**
+	 * Decodes value using the specified alphabet
+	 * 
+	 * @param value
+	 * @param alphabet
+	 * @return decoded value
+	 */
+	public static long decode(String value, String alphabet)
+	{
+		final int factor = alphabet.length();
+		long num = 0;
+		for (int i = 0, len = value.length(); i < len; i++)
+		{
+			num = num * factor + alphabet.indexOf(value.charAt(i));
+		}
+		return num;
+	}
+}

Propchange: wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/LongEncoder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/LongEncoderTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/LongEncoderTest.java?rev=1037608&view=auto
==============================================================================
--- wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/LongEncoderTest.java (added)
+++ wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/LongEncoderTest.java Mon Nov 22 05:51:11 2010
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests {@link LongEncoder}
+ * 
+ * @author igor
+ */
+public class LongEncoderTest
+{
+	/**
+	 * Tests the default alphabet included with the encoder
+	 */
+	@Test
+	public void defaultAlphabet()
+	{
+		Set<String> encoded = new HashSet<String>();
+		for (int i = 0; i < 10000; i++)
+		{
+			String enc = LongEncoder.encode(i);
+			Assert.assertFalse("uniqueness: " + i, encoded.contains(enc));
+			encoded.add(enc);
+			Assert.assertEquals("decoding: " + i, i, LongEncoder.decode(enc));
+		}
+	}
+}

Propchange: wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/LongEncoderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain