Author: elecharny
Date: Wed Aug  1 09:33:26 2012
New Revision: 1367896

URL: http://svn.apache.org/viewvc?rev=1367896&view=rev
Log:
Added the Chare and Byte serializers

Added:
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
    
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ByteSerializerTest.java
    
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/CharSerializerTest.java

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java?rev=1367896&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
 Wed Aug  1 09:33:26 2012
@@ -0,0 +1,51 @@
+/*
+ *  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.mavibot.btree.serializer;
+
+
+
+
+/**
+ * The Byte serializer.
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class ByteSerializer implements ElementSerializer<Byte>
+{
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Byte element )
+    {
+        byte[] bytes = new byte[1];
+        bytes[0] = element.byteValue();
+
+        return bytes;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Byte deserialize( byte[] in )
+    {
+        return in[0];
+    }
+}

Added: 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java?rev=1367896&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
 Wed Aug  1 09:33:26 2012
@@ -0,0 +1,53 @@
+/*
+ *  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.mavibot.btree.serializer;
+
+
+/**
+ * The Character serializer.
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class CharSerializer implements ElementSerializer<Character>
+{
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] serialize( Character element )
+    {
+        byte[] bytes = new byte[2];
+        char value = element.charValue();
+
+        bytes[0] = ( byte ) ( value >>> 8 );
+        bytes[1] = ( byte ) ( value );
+
+        return bytes;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Character deserialize( byte[] in )
+    {
+        return Character.valueOf( ( char ) ( ( in[0] << 8 ) +
+            ( in[1] & 0xFF ) ) );
+    }
+}

Added: 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ByteSerializerTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ByteSerializerTest.java?rev=1367896&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ByteSerializerTest.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ByteSerializerTest.java
 Wed Aug  1 09:33:26 2012
@@ -0,0 +1,80 @@
+/*
+ *  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.mavibot.btree.serializer;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+/**
+ * Test the ByteSerializer class
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class ByteSerializerTest
+{
+    private static ByteSerializer serializer = new ByteSerializer();
+
+
+    @Test
+    public void testByteSerializer()
+    {
+        byte value = 0x00;
+        byte[] result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).byteValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x01;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x01, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).byteValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x7F;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x7F, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).byteValue() );
+
+        // ------------------------------------------------------------------
+        value = ( byte ) 0x80;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x80, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).byteValue() );
+
+        // ------------------------------------------------------------------
+        value = ( byte ) 0xFF;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0xFF, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).byteValue() );
+    }
+}

Added: 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/CharSerializerTest.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/CharSerializerTest.java?rev=1367896&view=auto
==============================================================================
--- 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/CharSerializerTest.java
 (added)
+++ 
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/CharSerializerTest.java
 Wed Aug  1 09:33:26 2012
@@ -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.mavibot.btree.serializer;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+/**
+ * Test the CharSerializer class
+ * 
+ * @author <a href="mailto:[email protected]";>Mavibot labs Project</a>
+ */
+public class CharSerializerTest
+{
+    private static CharSerializer serializer = new CharSerializer();
+
+
+    @Test
+    public void testCharSerializer()
+    {
+        char value = 0x0000;
+        byte[] result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x0001;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x01, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x00FF;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0xFF, result[1] );
+        assertEquals( ( byte ) 0x00, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x0100;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x01, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x7FFF;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0xFF, result[1] );
+        assertEquals( ( byte ) 0x7F, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+
+        // ------------------------------------------------------------------
+        value = 0x8000;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0x00, result[1] );
+        assertEquals( ( byte ) 0x80, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+
+        // ------------------------------------------------------------------
+        value = 0xFFFF;
+        result = serializer.serialize( value );
+
+        assertEquals( ( byte ) 0xFF, result[1] );
+        assertEquals( ( byte ) 0xFF, result[0] );
+
+        assertEquals( value, serializer.deserialize( result ).charValue() );
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to