All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.client.common.ProtocolUtils Maven / Gradle / Ivy

There is a newer version: 8.0.0-alpha2
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.client.common;

import java.util.Arrays;
import java.util.Map;

/**
 * Common utilities used for XPack protocol classes
 */
public final class ProtocolUtils {

    /**
     * Implements equals for a map of string arrays
     *
     * The map of string arrays is used in some XPack protocol classes but does't work with equal.
     */
    public static boolean equals(Map a, Map b) {
        if (a == null) {
            return b == null;
        }
        if (b == null) {
            return false;
        }
        if (a.size() != b.size()) {
            return false;
        }
        for (Map.Entry entry : a.entrySet()) {
            String[] val = entry.getValue();
            String key = entry.getKey();
            if (val == null) {
                if (b.get(key) != null || b.containsKey(key) == false) {
                    return false;
                }
            } else {
                if (Arrays.equals(val, b.get(key)) == false) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * Implements hashCode for map of string arrays
     *
     * The map of string arrays does't work with hashCode.
     */
    public static int hashCode(Map a) {
        int hash = 0;
        for (Map.Entry entry : a.entrySet())
            hash += Arrays.hashCode(entry.getValue());
        return hash;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy