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

tech.jhipster.lite.shared.collection.domain.JHipsterCollections Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package tech.jhipster.lite.shared.collection.domain;

import static java.util.stream.Collectors.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Stream;

public final class JHipsterCollections {

  private JHipsterCollections() {}

  public static  Collection immutable(Collection collection) {
    if (collection == null) {
      return List.of();
    }

    return Collections.unmodifiableCollection(collection);
  }

  public static  Map immutable(Map map) {
    if (map == null) {
      return Map.of();
    }

    return Collections.unmodifiableMap(map);
  }

  @SafeVarargs
  public static  Collection concat(Collection... collections) {
    return Stream.of(collections).filter(Objects::nonNull).flatMap(Collection::stream).map(item -> (T) item).toList();
  }

  @SafeVarargs
  public static  Map concat(Map... maps) {
    return Collections.unmodifiableMap(
      Stream.of(maps)
        .filter(Objects::nonNull)
        .map(Map::entrySet)
        .flatMap(Collection::stream)
        .collect(toMap(Entry::getKey, Entry::getValue, (value1, value2) -> value2))
    );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy