001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.types; 019 020import static org.junit.Assert.assertEquals; 021 022import org.apache.hadoop.hbase.HBaseClassTestRule; 023import org.apache.hadoop.hbase.testclassification.MiscTests; 024import org.apache.hadoop.hbase.testclassification.SmallTests; 025import org.apache.hadoop.hbase.util.Order; 026import org.apache.hadoop.hbase.util.PositionedByteRange; 027import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032@Category({ MiscTests.class, SmallTests.class }) 033public class TestUnion2 { 034 @ClassRule 035 public static final HBaseClassTestRule CLASS_RULE = 036 HBaseClassTestRule.forClass(TestUnion2.class); 037 038 /** 039 * An example <code>Union</code> 040 */ 041 private static class SampleUnion1 extends Union2<Integer, String> { 042 private static final byte IS_INTEGER = 0x00; 043 private static final byte IS_STRING = 0x01; 044 045 public SampleUnion1() { 046 super(new RawInteger(), new RawStringTerminated(Order.DESCENDING, ".")); 047 } 048 049 @Override 050 public int skip(PositionedByteRange src) { 051 switch (src.get()) { 052 case IS_INTEGER: 053 return 1 + typeA.skip(src); 054 case IS_STRING: 055 return 1 + typeB.skip(src); 056 default: 057 throw new IllegalArgumentException("Unrecognized encoding format."); 058 } 059 } 060 061 @Override 062 public Object decode(PositionedByteRange src) { 063 switch (src.get()) { 064 case IS_INTEGER: 065 return typeA.decode(src); 066 case IS_STRING: 067 return typeB.decode(src); 068 default: 069 throw new IllegalArgumentException("Unrecognized encoding format."); 070 } 071 } 072 073 @Override 074 public int encodedLength(Object val) { 075 Integer i = null; 076 String s = null; 077 try { 078 i = (Integer) val; 079 } catch (ClassCastException ignored) {} 080 try { 081 s = (String) val; 082 } catch (ClassCastException ignored) {} 083 084 if (null != i) { 085 return 1 + typeA.encodedLength(i); 086 } 087 088 if (null != s) { 089 return 1 + typeB.encodedLength(s); 090 } 091 092 throw new IllegalArgumentException("val is not a valid member of this union."); 093 } 094 095 @Override 096 public int encode(PositionedByteRange dst, Object val) { 097 Integer i = null; 098 String s = null; 099 try { 100 i = (Integer) val; 101 } catch (ClassCastException ignored) {} 102 try { 103 s = (String) val; 104 } catch (ClassCastException ignored) {} 105 106 if (null != i) { 107 dst.put(IS_INTEGER); 108 return 1 + typeA.encode(dst, i); 109 } else if (null != s) { 110 dst.put(IS_STRING); 111 return 1 + typeB.encode(dst, s); 112 } else { 113 throw new IllegalArgumentException("val is not of a supported type."); 114 } 115 } 116 } 117 118 @Test 119 public void testEncodeDecode() { 120 Integer intVal = 10; 121 String strVal = "hello"; 122 PositionedByteRange buff = new SimplePositionedMutableByteRange(10); 123 SampleUnion1 type = new SampleUnion1(); 124 125 type.encode(buff, intVal); 126 buff.setPosition(0); 127 assertEquals(0, intVal.compareTo(type.decodeA(buff))); 128 buff.setPosition(0); 129 type.encode(buff, strVal); 130 buff.setPosition(0); 131 assertEquals(0, strVal.compareTo(type.decodeB(buff))); 132 } 133 134 @Test 135 public void testSkip() { 136 Integer intVal = 10; 137 String strVal = "hello"; 138 PositionedByteRange buff = new SimplePositionedMutableByteRange(10); 139 SampleUnion1 type = new SampleUnion1(); 140 141 int len = type.encode(buff, intVal); 142 buff.setPosition(0); 143 assertEquals(len, type.skip(buff)); 144 buff.setPosition(0); 145 len = type.encode(buff, strVal); 146 buff.setPosition(0); 147 assertEquals(len, type.skip(buff)); 148 } 149}