/*
 * Copyright 2017 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-api
 *
 * Report Portal 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.
 *
 * Report Portal 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 Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.commons;

import com.google.common.base.Preconditions;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.function.Function;
import java.util.function.Predicate;

import static com.google.common.base.Strings.isNullOrEmpty;

/**
 * Some useful utils for working with entities<br>
 * For example: usernames, project names, tags, etc.
 *
 * @author Andrei Varabyeu
 */
public class EntityUtils {

	private static final String OLD_SEPARATOR = ",";
	private static final String NEW_SEPARATOR = "_";

	private EntityUtils() {

	}

	public static final Function<Date, LocalDateTime> TO_LOCAL_DATE_TIME = date -> {
		Preconditions.checkNotNull(date, "Provided value shouldn't be null");
		return LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC);
	};

	public static final Function<LocalDateTime, Date> TO_DATE = localDateTime -> {
		Preconditions.checkNotNull(localDateTime, "Provided value shouldn't be null");
		return Date.from(localDateTime.atZone(ZoneOffset.UTC).toInstant());
	};

	/**
	 * Remove leading and trailing spaces from list of string
	 */
	public static final Function<String, String> TRIM_FUNCTION = String::trim;
	public static final Predicate<String> NOT_EMPTY = s -> !isNullOrEmpty(s);

	/**
	 * Convert declined symbols on allowed for WS and UI
	 */
	public static final Function<String, String> REPLACE_SEPARATOR = s -> s.replace(OLD_SEPARATOR, NEW_SEPARATOR);

	/**
	 * Normalize any ID for database ID fields, for example
	 *
	 * @param id ID to normalize
	 * @return String
	 */

	public static String normalizeId(String id) {
		return Preconditions.checkNotNull(id, "Provided value shouldn't be null").toLowerCase();
	}

}

@@ -954,16 +954,43 @@
 l.Date;%0A
+import java.util.Optional;%0A
 import j
@@ -1501,90 +1501,46 @@
  -%3E 
-%7B%0A%09%09Preconditions.checkNotNull(date, %22Provided value shouldn't be null%22);%0A%09%09return
+Optional.ofNullable(date)%0A%09%09%09.map(d -%3E
  Loc
@@ -1561,19 +1561,16 @@
 nstant(d
-ate
 .toInsta
@@ -1590,20 +1590,34 @@
 set.UTC)
-;%0A%09%7D
+)%0A%09%09%09.orElse(null)
 ;%0A%0A%09publ
@@ -1693,123 +1693,67 @@
  -%3E 
-%7B%0A%09%09Preconditions.checkNotNull(localDateTime, %22Provided value shouldn't be null%22);%0A%09%09return Date.from(localDateTime
+Optional.ofNullable(localDateTime)%0A%09%09%09.map(l -%3E Date.from(l
 .atZ
@@ -1784,20 +1784,34 @@
 stant())
-;%0A%09%7D
+)%0A%09%09%09.orElse(null)
 ;%0A%0A%09/**%0A

fix potential npe