Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/18221#discussion_r120677421
  
    --- Diff: 
common/kvstore/src/main/java/org/apache/spark/kvstore/ArrayWrappers.java ---
    @@ -0,0 +1,214 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.kvstore;
    +
    +import java.util.Arrays;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.base.Objects;
    +
    +/**
    + * A factory for array wrappers so that arrays can be used as a key in a 
map, sorted or not.
    + *
    + * The comparator implementation makes two assumptions:
    + * - All elements are instances of Comparable
    + * - When comparing two arrays, they both contain elements of the same 
type in corresponding
    + *   indices.
    + *
    + * Otherwise, ClassCastExceptions may occur. The equality method can 
compare any two arrays.
    + *
    + * This class is not efficient and is mostly meant to compare really small 
arrays, like those
    + * generally used as indices and keys in a KVStore.
    + */
    +class ArrayWrappers {
    +
    +  @SuppressWarnings("unchecked")
    +  public static Comparable<Object> forArray(Object a) {
    +    Preconditions.checkArgument(a.getClass().isArray());
    +    Comparable<?> ret;
    +    if (a instanceof int[]) {
    +      ret = new ComparableIntArray((int[]) a);
    +    } else if (a instanceof long[]) {
    +      ret = new ComparableLongArray((long[]) a);
    +    } else if (a instanceof byte[]) {
    +      ret = new ComparableByteArray((byte[]) a);
    +    } else {
    +      
Preconditions.checkArgument(!a.getClass().getComponentType().isPrimitive());
    +      ret = new ComparableObjectArray((Object[]) a);
    +    }
    +    return (Comparable<Object>) ret;
    +  }
    +
    +  private static class ComparableIntArray implements 
Comparable<ComparableIntArray> {
    +
    +    private final int[] array;
    +
    +    ComparableIntArray(int[] array) {
    +      this.array = array;
    +    }
    +
    +    @Override
    +    public boolean equals(Object other) {
    +      if (!(other instanceof ComparableIntArray)) {
    +        return false;
    +      }
    +      return Arrays.equals(array, ((ComparableIntArray) other).array);
    +    }
    +
    +    @Override
    +    public int hashCode() {
    +      int code = 0;
    +      for (int i = 0; i < array.length; i++) {
    +        code += array[i];
    --- End diff --
    
    Sure, that's easy enough.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to