/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program 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 Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
 */

package org.openlmis.referencedata.util;

import org.springframework.util.MultiValueMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

/**
 * Generic utility functions for {@link UUID}.
 */
public class UuidUtil {

  private static final String ID = "id";

  /**
   * Parses a String into a UUID safely.  Unlike {@link UUID#fromString(String)} however, this
   * method will either return an Optional with the UUID, or it will return an empty Optional
   * in case the String can't be parsed into a UUID.
   *
   * @param uuid see {@link UUID#fromString(String)}.
   * @return An {@link Optional} with either the UUID as parsed from the paramater, or an empty
   *     Optional should it be un-parseable.
   */
  public static Optional<UUID> fromString(String uuid) {
    try {
      return Optional.of(UUID.fromString(uuid));
    } catch (IllegalArgumentException iae) {
      return Optional.empty();
    }
  }

  /**
   * Gets a set of UUIDs from query multi value map.
   */
  public static Set<UUID> getIds(MultiValueMap<String, Object> queryMap) {
    Set<UUID> ids = new HashSet<>();
    queryMap.forEach((key, value) -> {
      if (Objects.equals(key, ID)) {
        value.forEach(id -> ids.add(UUID.fromString((String)id)));
      }
    });

    return ids;
  }
}
@@ -1899,16 +1899,453 @@
 lue map.
+ Returns empty set if no %7B@code id%7D key.%0A   *%0A   * @param queryMap a multi value map that should contain %7B@code id%7D as key%0A   *                 and some UUID string as value.%0A   * @return a set of %7B@link UUID%7D extracted from map where key is %7B@code id%7D.%0A   * @throws ClassCastException when value for key %7B@code id%7D is not %7B@code String%7D%0A   * @throws IllegalArgumentException when value for key %7B@code id%7D is not parsable to %7B@link UUID%7D
 %0A   */%0A 

improve javadoc