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 ASCENDING {
33 @Override
34 public int cmp(int cmp) {
35 /* noop */ return cmp;
36 }
37
38 @Override
39 public byte apply(byte val) {
40 /* noop */ return val;
41 }
42
43 @Override
44 public void apply(byte[] val) {
45 /* noop */
46 }
47
48 @Override
49 public void apply(byte[] val, int offset, int length) {
50 /* noop */
51 }
52
53 @Override
54 public String toString() {
55 return "ASCENDING";
56 }
57 },
58
59 DESCENDING {
60 /**
61 * A {@code byte} value is inverted by taking its 1's Complement, achieved
62 * via {@code xor} with {@code 0xff}.
63 */
64 private static final byte MASK = (byte) 0xff;
65
66 @Override
67 public int cmp(int cmp) {
68 return -1 * cmp;
69 }
70
71 @Override
72 public byte apply(byte val) {
73 return (byte) (val ^ MASK);
74 }
75
76 @Override
77 public void apply(byte[] val) {
78 for (int i = 0; i < val.length; i++) {
79 val[i] ^= MASK;
80 }
81 }
82
83 @Override
84 public void apply(byte[] val, int offset, int length) {
85 for (int i = 0; i < length; i++) {
86 val[offset + i] ^= MASK;
87 }
88 }
89
90 @Override
91 public String toString() {
92 return "DESCENDING";
93 }
94 };
95
96 /**
97 * Returns the adjusted trichotomous value according to the ordering imposed by this
98 * {@code Order}.
99 */
100 public abstract int cmp(int cmp);
101
102 /**
103 * Apply order to the byte {@code val}.
104 */
105 public abstract byte apply(byte val);
106
107 /**
108 * Apply order to the byte array {@code val}.
109 */
110 public abstract void apply(byte[] val);
111
112 /**
113 * Apply order to a range within the byte array {@code val}.
114 */
115 public abstract void apply(byte[] val, int offset, int length);
116 }