001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.vocabulary;
017
018import org.apache.commons.lang3.StringUtils;
019
020/**
021 * Enumeration to indicate a part of a canonical scientific name.
022 */
023public enum NamePart {
024
025  GENERIC,
026  INFRAGENERIC,
027  SPECIFIC,
028  INFRASPECIFIC;
029
030  /**
031   * Case insensitive lookup of a NamePart by its name that does not throw an exception but returns null
032   * for a not found NamePart.
033   *
034   * @param namePart case insensitive name of name part
035   *
036   * @return the matching NamePart or null
037   */
038  public static NamePart fromString(String namePart) {
039    if (StringUtils.isNotEmpty(namePart)) {
040      try {
041        return valueOf(namePart.toUpperCase().trim());
042      } catch (IllegalArgumentException e) {
043        // swallow
044      }
045    }
046    return null;
047  }
048
049}