View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package org.apache.accumulo.core.file.rfile.bcfile;
18  
19  import java.util.Comparator;
20  
21  import org.apache.hadoop.io.RawComparator;
22  import org.apache.hadoop.io.WritableComparator;
23  
24  class CompareUtils {
25    /**
26     * Prevent the instantiation of class.
27     */
28    private CompareUtils() {
29      // nothing
30    }
31    
32    /**
33     * A comparator to compare anything that implements {@link RawComparable} using a customized comparator.
34     */
35    public static final class BytesComparator implements Comparator<RawComparable> {
36      private RawComparator<Object> cmp;
37      
38      public BytesComparator(RawComparator<Object> cmp) {
39        this.cmp = cmp;
40      }
41      
42      @Override
43      public int compare(RawComparable o1, RawComparable o2) {
44        return compare(o1.buffer(), o1.offset(), o1.size(), o2.buffer(), o2.offset(), o2.size());
45      }
46      
47      public int compare(byte[] a, int off1, int len1, byte[] b, int off2, int len2) {
48        return cmp.compare(a, off1, len1, b, off2, len2);
49      }
50    }
51    
52    /**
53     * Interface for all objects that has a single integer magnitude.
54     */
55    static interface Scalar {
56      long magnitude();
57    }
58    
59    static final class ScalarLong implements Scalar {
60      private long magnitude;
61      
62      public ScalarLong(long m) {
63        magnitude = m;
64      }
65      
66      public long magnitude() {
67        return magnitude;
68      }
69    }
70    
71    public static final class ScalarComparator implements Comparator<Scalar> {
72      @Override
73      public int compare(Scalar o1, Scalar o2) {
74        long diff = o1.magnitude() - o2.magnitude();
75        if (diff < 0)
76          return -1;
77        if (diff > 0)
78          return 1;
79        return 0;
80      }
81    }
82    
83    public static final class MemcmpRawComparator implements RawComparator<Object> {
84      @Override
85      public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
86        return WritableComparator.compareBytes(b1, s1, l1, b2, s2, l2);
87      }
88      
89      @Override
90      public int compare(Object o1, Object o2) {
91        throw new RuntimeException("Object comparison not supported");
92      }
93    }
94  }