1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.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
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 }