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

io.fusionauth.domain.util.Normalizer Maven / Gradle / Ivy

There is a newer version: 1.53.0
Show newest version
/*
 * Copyright (c) 2019, FusionAuth, All Rights Reserved
 *
 * Licensed 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 io.fusionauth.domain.util;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * Helper methods for normalizing values.
 *
 * @author Brian Pontarelli
 */
public final class Normalizer {
  /**
   * De-duplicate an array list.
   *
   * @param list the list to de-duplicate
   * @param   the type of the list.
   */
  public static  void deDuplicate(List list) {
    Set set = new HashSet<>();
    list.removeIf(o -> !set.add(o));
  }

  /**
   * Normalize line returns
   *
   * @param str The String to normalize
   * @return The normalized string or null
   */
  public static String lineReturns(String str) {
    if (str == null) {
      return null;
    }

    return str.replaceAll("\\r\\n|\\r", "\n");
  }

  /**
   * Cleans the map by trimming all of the values.
   *
   * @param map The map to clean.
   * @param  the type of map key
   */
  public static  void lineReturnsMap(Map map) {
    map.forEach((key, value) -> {
      if (value != null) {
        map.put(key, value.replaceAll("\\r\\n|\\r", "\n"));
      }
    });
  }

  /**
   * Removes empty values from the list.
   *
   * @param list The list.
   * @param   the type of list.
   */
  public static  void removeEmpty(List list) {
    list.removeIf(Objects::isNull);
  }

  /**
   * Removes keys whose value are null.
   *
   * @param map The map.
   * @param  the type of map key
   * @param  the type of map value
   */
  public static  void removeEmpty(Map map) {
    if (map == null) {
      return;
    }

    map.keySet().removeIf(key -> map.get(key) == null);
  }

  /**
   * Remove line returns from the string
   *
   * @param str the string to remove line returns
   * @return the normalized string w/out line returns or null
   */
  public static String removeLineReturns(String str) {
    if (str == null) {
      return null;
    }

    return str.replaceAll("\\r\\n|\\r|\\n", "");
  }

  /**
   * Lowercase the string values of the collection using the provided collection reference.
   *
   * @param collection the collection
   * @param supplier   a collections supplier
   * @param         the type of collection
   */
  public static > void toLowerCase(Collection collection, Supplier supplier) {
    if (collection.isEmpty()) {
      return;
    }

    Collection lc = collection.stream().map(String::toLowerCase).collect(Collectors.toCollection(supplier));
    collection.clear();
    collection.addAll(lc);
  }

  /**
   * Lowercases the String in a null-safe manner.
   *
   * @param str The String to lowercase.
   * @return The String or null.
   */
  public static String toLowerCase(String str) {
    if (str == null) {
      return null;
    }

    return str.toLowerCase();
  }

  /**
   * Trims the String in a null safe manner.
   *
   * @param str The String to trim.
   * @return The trimmed String or null.
   */
  public static String trim(String str) {
    if (str == null) {
      return null;
    }

    return str.trim();
  }

  /**
   * Cleans the map by trimming all of the values.
   *
   * @param map The map to clean.
   * @param  the type of map key
   */
  public static  void trimMap(Map map) {
    map.forEach((key, value) -> {
      if (value != null) {
        map.put(key, value.trim());
      }
    });
  }

  /**
   * Trims the String in a null safe manner and if the String ends up being empty, then this returns null.
   *
   * @param str The String to trim.
   * @return The trimmed String or null.
   */
  public static String trimToNull(String str) {
    if (str == null) {
      return null;
    }

    str = str.trim();
    if (str.isEmpty()) {
      return null;
    }

    return str;
  }

  /**
   * Truncate the provided time to milliseconds.
   *
   * @param time the time
   * @return a truncated time
   */
  public static ZonedDateTime truncateToMilliseconds(ZonedDateTime time) {
    if (time == null) {
      return null;
    }

    return time.truncatedTo(ChronoUnit.MILLIS);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy