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 org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  import org.apache.hadoop.hbase.util.Order;
23  import org.apache.hadoop.hbase.util.PositionedByteRange;
24  
25  /**
26   * The {@code Union} family of {@link DataType}s encode one of a fixed
27   * set of {@code Object}s. They provide convenience methods which handle
28   * type casting on your behalf.
29   */
30  @SuppressWarnings("unchecked")
31  @InterfaceAudience.Public
32  @InterfaceStability.Evolving
33  public abstract class Union2<A, B> implements DataType<Object> {
34  
35    protected final DataType<A> typeA;
36    protected final DataType<B> typeB;
37  
38    /**
39     * Create an instance of {@code Union2} over the set of specified
40     * types.
41     */
42    public Union2(DataType<A> typeA, DataType<B> typeB) {
43      this.typeA = typeA;
44      this.typeB = typeB;
45    }
46  
47    @Override
48    public boolean isOrderPreserving() {
49      return typeA.isOrderPreserving() && typeB.isOrderPreserving();
50    }
51  
52    @Override
53    public Order getOrder() { return null; }
54  
55    @Override
56    public boolean isNullable() {
57      return typeA.isNullable() && typeB.isNullable();
58    }
59  
60    @Override
61    public boolean isSkippable() {
62      return typeA.isSkippable() && typeB.isSkippable();
63    }
64  
65    @Override
66    public Class<Object> encodedClass() {
67      throw new UnsupportedOperationException(
68        "Union types do not expose a definitive encoded class.");
69    }
70  
71    /**
72     * Read an instance of the first type parameter from buffer {@code src}.
73     */
74    public A decodeA(PositionedByteRange src) {
75      return (A) decode(src);
76    }
77  
78    /**
79     * Read an instance of the second type parameter from buffer {@code src}.
80     */
81    public B decodeB(PositionedByteRange src) {
82      return (B) decode(src);
83    }
84  }