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

org.graylog.plugins.beats.MapUtils Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog.plugins.beats;

import java.util.HashMap;
import java.util.Map;

public final class MapUtils {
    public static Map flatten(Map originalMap, String parentKey, String separator) {
        final Map result = new HashMap<>();
        for (Map.Entry entry : originalMap.entrySet()) {
            final String key = parentKey.isEmpty() ? entry.getKey() : parentKey + separator + entry.getKey();
            final Object value = entry.getValue();
            if (value instanceof Map) {
                @SuppressWarnings("unchecked")
                final Map valueMap = (Map) value;
                result.putAll(flatten(valueMap, key, separator));
            } else {
                result.put(key, value);
            }
        }
        return result;
    }

    public static void renameKey(Map map, String originalKey, String newKey) {
        if (map.containsKey(originalKey)) {
            final Object value = map.remove(originalKey);
            map.put(newKey, value);
        }
    }

    public static Map replaceKeyCharacter(Map map, char oldChar, char newChar) {
        final Map result = new HashMap<>(map.size());
        for (Map.Entry entry : map.entrySet()) {
            final String key = entry.getKey().replace(oldChar, newChar);
            final Object value = entry.getValue();
            result.put(key, value);
        }
        return result;
    }

    private MapUtils() {
        throw new AssertionError("No instances allowed");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy