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.util;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  
23  /**
24   * Used to describe or modify the lexicographical sort order of a
25   * {@code byte[]}. Default ordering is considered {@code ASCENDING}. The order
26   * of a {@code byte[]} can be inverted, resulting in {@code DESCENDING} order,
27   * by replacing each byte with its 1's compliment.
28   */
29  @InterfaceAudience.Public
30  @InterfaceStability.Evolving
31  public enum Order {
32  
33    ASCENDING {
34      @Override
35      public int cmp(int cmp) { /* noop */ return cmp; }
36  
37      @Override
38      public byte apply(byte val) { /* noop */ return val; }
39  
40      @Override
41      public void apply(byte[] val) { /* noop */ }
42  
43      @Override
44      public void apply(byte[] val, int offset, int length) { /* noop */ }
45  
46      @Override
47      public String toString() { return "ASCENDING"; }
48    },
49  
50    DESCENDING {
51      /**
52       * A {@code byte} value is inverted by taking its 1's Complement, achieved
53       * via {@code xor} with {@code 0xff}.
54       */
55      private static final byte MASK = (byte) 0xff;
56  
57      @Override
58      public int cmp(int cmp) { return -1 * cmp; }
59  
60      @Override
61      public byte apply(byte val) { return (byte) (val ^ MASK); }
62  
63      @Override
64      public void apply(byte[] val) {
65        for (int i = 0; i < val.length; i++) { val[i] ^= MASK; }
66      }
67  
68      @Override
69      public void apply(byte[] val, int offset, int length) {
70        for (int i = 0; i < length; i++) { val[offset + i] ^= MASK; }
71      }
72  
73      @Override
74      public String toString() { return "DESCENDING"; }
75    };
76  
77    /**
78     * Returns the adjusted trichotomous value according to the ordering imposed by this
79     * {@code Order}.
80     */
81    public abstract int cmp(int cmp);
82  
83    /**
84     * Apply order to the byte {@code val}.
85     */
86    public abstract byte apply(byte val);
87  
88    /**
89     * Apply order to the byte array {@code val}.
90     */
91    public abstract void apply(byte[] val);
92  
93    /**
94     * Apply order to a range within the byte array {@code val}.
95     */
96    public abstract void apply(byte[] val, int offset, int length);
97  }