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
22 import org.apache.hadoop.hbase.testclassification.SmallTests;
23 import org.apache.hadoop.hbase.util.Order;
24 import org.apache.hadoop.hbase.util.PositionedByteRange;
25 import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange;
26 import org.junit.Test;
27 import org.junit.experimental.categories.Category;
28
29 @Category(SmallTests.class)
30 public class TestUnion2 {
31
32
33
34 private static class SampleUnion1 extends Union2<Integer, String> {
35 private static final byte IS_INTEGER = 0x00;
36 private static final byte IS_STRING = 0x01;
37
38 public SampleUnion1() {
39 super(new RawInteger(), new RawStringTerminated(Order.DESCENDING, "."));
40 }
41
42 @Override
43 public int skip(PositionedByteRange src) {
44 switch (src.get()) {
45 case IS_INTEGER:
46 return 1 + typeA.skip(src);
47 case IS_STRING:
48 return 1 + typeB.skip(src);
49 default:
50 throw new IllegalArgumentException("Unrecognized encoding format.");
51 }
52 }
53
54 @Override
55 public Object decode(PositionedByteRange src) {
56 switch (src.get()) {
57 case IS_INTEGER:
58 return typeA.decode(src);
59 case IS_STRING:
60 return typeB.decode(src);
61 default:
62 throw new IllegalArgumentException("Unrecognized encoding format.");
63 }
64 }
65
66 @Override
67 public int encodedLength(Object val) {
68 Integer i = null;
69 String s = null;
70 try {
71 i = (Integer) val;
72 } catch (ClassCastException ignored) {}
73 try {
74 s = (String) val;
75 } catch (ClassCastException ignored) {}
76
77 if (null != i) {
78 return 1 + typeA.encodedLength(i);
79 }
80
81 if (null != s) {
82 return 1 + typeB.encodedLength(s);
83 }
84
85 throw new IllegalArgumentException("val is not a valid member of this union.");
86 }
87
88 @Override
89 public int encode(PositionedByteRange dst, Object val) {
90 Integer i = null;
91 String s = null;
92 try {
93 i = (Integer) val;
94 } catch (ClassCastException ignored) {}
95 try {
96 s = (String) val;
97 } catch (ClassCastException ignored) {}
98
99 if (null != i) {
100 dst.put(IS_INTEGER);
101 return 1 + typeA.encode(dst, i);
102 } else if (null != s) {
103 dst.put(IS_STRING);
104 return 1 + typeB.encode(dst, s);
105 } else {
106 throw new IllegalArgumentException("val is not of a supported type.");
107 }
108 }
109 }
110
111 @Test
112 public void testEncodeDecode() {
113 Integer intVal = 10;
114 String strVal = "hello";
115 PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
116 SampleUnion1 type = new SampleUnion1();
117
118 type.encode(buff, intVal);
119 buff.setPosition(0);
120 assertEquals(0, intVal.compareTo(type.decodeA(buff)));
121 buff.setPosition(0);
122 type.encode(buff, strVal);
123 buff.setPosition(0);
124 assertEquals(0, strVal.compareTo(type.decodeB(buff)));
125 }
126
127 @Test
128 public void testSkip() {
129 Integer intVal = 10;
130 String strVal = "hello";
131 PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
132 SampleUnion1 type = new SampleUnion1();
133
134 int len = type.encode(buff, intVal);
135 buff.setPosition(0);
136 assertEquals(len, type.skip(buff));
137 buff.setPosition(0);
138 len = type.encode(buff, strVal);
139 buff.setPosition(0);
140 assertEquals(len, type.skip(buff));
141 }
142 }