View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.types;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import org.apache.hadoop.hbase.testclassification.SmallTests;
24  import org.apache.hadoop.hbase.util.Order;
25  import org.apache.hadoop.hbase.util.PositionedByteRange;
26  import org.apache.hadoop.hbase.util.SimplePositionedByteRange;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestUnion2 {
32  
33    /**
34     * An example <code>Union</code>
35     */
36    private static class SampleUnion1 extends Union2<Integer, String> {
37  
38      private static final byte IS_INTEGER = 0x00;
39      private static final byte IS_STRING  = 0x01;
40  
41      public SampleUnion1() {
42        super(new RawInteger(), new RawStringTerminated(Order.DESCENDING, "."));
43      }
44  
45      @Override
46      public int skip(PositionedByteRange src) {
47        switch (src.get()) {
48          case IS_INTEGER:
49            return 1 + typeA.skip(src);
50          case IS_STRING:
51            return 1 + typeB.skip(src);
52          default:
53            throw new IllegalArgumentException("Unrecognized encoding format.");
54        }
55      }
56  
57      @Override
58      public Object decode(PositionedByteRange src) {
59        switch (src.get()) {
60          case IS_INTEGER:
61            return typeA.decode(src);
62          case IS_STRING:
63            return typeB.decode(src);
64          default:
65            throw new IllegalArgumentException("Unrecognized encoding format.");
66        }
67      }
68  
69      @Override
70      public int encodedLength(Object val) {
71        Integer i = null;
72        String s = null;
73        try {
74          i = (Integer) val;
75        } catch (ClassCastException e) {}
76        try {
77          s = (String) val;
78        } catch (ClassCastException e) {}
79  
80        if (null != i) return 1 + typeA.encodedLength(i);
81        if (null != s) return 1 + typeB.encodedLength(s);
82        throw new IllegalArgumentException("val is not a valid member of this union.");
83      }
84  
85      @Override
86      public int encode(PositionedByteRange dst, Object val) {
87        Integer i = null;
88        String s = null;
89        try {
90          i = (Integer) val;
91        } catch (ClassCastException e) {}
92        try {
93          s = (String) val;
94        } catch (ClassCastException e) {}
95  
96        if (null != i) {
97          dst.put(IS_INTEGER);
98          return 1 + typeA.encode(dst, i);
99        } else if (null != s) {
100         dst.put(IS_STRING);
101         return 1 + typeB.encode(dst, s);
102       }
103       else
104         throw new IllegalArgumentException("val is not of a supported type.");
105     }
106   }
107 
108   @Test
109   public void testEncodeDecode() {
110     Integer intVal = Integer.valueOf(10);
111     String strVal = "hello";
112     PositionedByteRange buff = new SimplePositionedByteRange(10);
113     SampleUnion1 type = new SampleUnion1();
114 
115     type.encode(buff, intVal);
116     buff.setPosition(0);
117     assertTrue(0 == intVal.compareTo(type.decodeA(buff)));
118     buff.setPosition(0);
119     type.encode(buff, strVal);
120     buff.setPosition(0);
121     assertTrue(0 == strVal.compareTo(type.decodeB(buff)));
122   }
123 
124   @Test
125   public void testSkip() {
126     Integer intVal = Integer.valueOf(10);
127     String strVal = "hello";
128     PositionedByteRange buff = new SimplePositionedByteRange(10);
129     SampleUnion1 type = new SampleUnion1();
130 
131     int len = type.encode(buff, intVal);
132     buff.setPosition(0);
133     assertEquals(len, type.skip(buff));
134     buff.setPosition(0);
135     len = type.encode(buff, strVal);
136     buff.setPosition(0);
137     assertEquals(len, type.skip(buff));
138   }
139 }