You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2012/08/01 11:53:28 UTC
svn commit: r1367900 - in /labs/mavibot/trunk/mavibot/src:
main/java/org/apache/mavibot/btree/serializer/
test/java/org/apache/mavibot/btree/serializer/
Author: elecharny
Date: Wed Aug 1 09:53:27 2012
New Revision: 1367900
URL: http://svn.apache.org/viewvc?rev=1367900&view=rev
Log:
Added the short and boolean serializers
Added:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/BooleanSerializerTest.java
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ShortSerializerTest.java
Added: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java?rev=1367900&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java (added)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java Wed Aug 1 09:53:27 2012
@@ -0,0 +1,49 @@
+/*
+ * 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 Boolean serializer.
+ *
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class BooleanSerializer implements ElementSerializer<Boolean>
+{
+ /**
+ * {@inheritDoc}
+ */
+ public byte[] serialize( Boolean element )
+ {
+ byte[] bytes = new byte[1];
+ bytes[0] = element.booleanValue() ? ( byte ) 0x01 : ( byte ) 0x00;
+
+ return bytes;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public Boolean deserialize( byte[] in )
+ {
+ return in[0] == 0x01;
+ }
+}
Added: labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java?rev=1367900&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java (added)
+++ labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java Wed Aug 1 09:53:27 2012
@@ -0,0 +1,52 @@
+/*
+ * 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 Short serializer.
+ *
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class ShortSerializer implements ElementSerializer<Short>
+{
+ /**
+ * {@inheritDoc}
+ */
+ public byte[] serialize( Short element )
+ {
+ byte[] bytes = new byte[2];
+ short value = element.shortValue();
+
+ bytes[0] = ( byte ) ( value >>> 8 );
+ bytes[1] = ( byte ) ( value );
+
+ return bytes;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public Short deserialize( byte[] in )
+ {
+ return ( short ) ( ( in[0] << 8 ) + ( in[1] & 0xFF ) );
+ }
+}
Added: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/BooleanSerializerTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/BooleanSerializerTest.java?rev=1367900&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/BooleanSerializerTest.java (added)
+++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/BooleanSerializerTest.java Wed Aug 1 09:53:27 2012
@@ -0,0 +1,56 @@
+/*
+ * 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 BooleanSerializer class
+ *
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class BooleanSerializerTest
+{
+ private static BooleanSerializer serializer = new BooleanSerializer();
+
+
+ @Test
+ public void testBooleanSerializer()
+ {
+ boolean value = true;
+ byte[] result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0x01, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).booleanValue() );
+
+ // ------------------------------------------------------------------
+ value = false;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0x00, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).booleanValue() );
+ }
+}
Added: labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ShortSerializerTest.java
URL: http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ShortSerializerTest.java?rev=1367900&view=auto
==============================================================================
--- labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ShortSerializerTest.java (added)
+++ labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/ShortSerializerTest.java Wed Aug 1 09:53:27 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 ShortSerializer class
+ *
+ * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
+ */
+public class ShortSerializerTest
+{
+ private static ShortSerializer serializer = new ShortSerializer();
+
+
+ @Test
+ public void testShortSerializer()
+ {
+ short value = 0x0000;
+ byte[] result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0x00, result[1] );
+ assertEquals( ( byte ) 0x00, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+
+ // ------------------------------------------------------------------
+ value = 0x0001;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0x01, result[1] );
+ assertEquals( ( byte ) 0x00, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+
+ // ------------------------------------------------------------------
+ value = 0x00FF;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0xFF, result[1] );
+ assertEquals( ( byte ) 0x00, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+
+ // ------------------------------------------------------------------
+ value = 0x0100;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0x00, result[1] );
+ assertEquals( ( byte ) 0x01, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+
+ // ------------------------------------------------------------------
+ value = 0x7FFF;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0xFF, result[1] );
+ assertEquals( ( byte ) 0x7F, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+
+ // ------------------------------------------------------------------
+ value = ( short ) 0x8000;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0x00, result[1] );
+ assertEquals( ( byte ) 0x80, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+
+ // ------------------------------------------------------------------
+ value = ( short ) 0xFFFF;
+ result = serializer.serialize( value );
+
+ assertEquals( ( byte ) 0xFF, result[1] );
+ assertEquals( ( byte ) 0xFF, result[0] );
+
+ assertEquals( value, serializer.deserialize( result ).shortValue() );
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org