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

com.github.palindromicity.syslog.util.StructuredDataUtil Maven / Gradle / Ivy

package com.github.palindromicity.syslog.util;

import com.github.palindromicity.syslog.KeyProvider;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import org.jspecify.nullness.NullMarked;
import org.jspecify.nullness.Nullable;

/**
 * This utility class takes a 'flattened' syslog map and un-flattens it.
 */
@NullMarked
public class StructuredDataUtil {

  /**
   * Unflattens a flat map such that it contains any nested maps required.
   *
   * @param flattenedMap the map to unflatten
   * @param keyProvider  the key provider to use
   * @return Map
   */
  @SuppressWarnings("unchecked")
  public static Map unFlattenStructuredData(
      Map flattenedMap,
      KeyProvider keyProvider) {
    Validate.notNull(keyProvider, "keyProvider");
    Validate.notNull(flattenedMap, "flattenedMap");
    boolean hasStructuredData = false;
    final Map returnMap = new HashMap<>();
    for (String key : flattenedMap.keySet()) {
      if (key.startsWith(keyProvider.getStructuredBase())) {
        hasStructuredData = true;
        break;
      }
    }

    if (!hasStructuredData) {
      return returnMap;
    }
    flattenedMap.forEach((key, value) -> {
      Matcher matcher = keyProvider.getStructuredElementIdParamNamePattern().matcher(key);
      if (matcher.matches()) {
        String id = matcher.group(1);
        String name = matcher.group(2);
        returnMap.putIfAbsent(id, new HashMap<>());
        Map idMap = (Map) returnMap.get(id);
        idMap.put(name, value);
      }
    });
    return returnMap;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy