001/* Copyright (C) 2014 konik.io
002 *
003 * This file is part of the Konik library.
004 *
005 * The Konik library is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * The Konik library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>.
017 */
018package io.konik.zugferd.profile;
019
020import io.konik.util.Strings;
021
022/**
023 * = The known ZUGFeRD profile versions.
024 */
025public enum ProfileVersion {
026
027   /** The release candidate. */
028   RC("rc"),
029
030   /** The release candidate extended. */
031   RCE("rce"),
032
033   /** The version 1.0 */
034   V1P0("1p0");
035
036   private static final String DELIMITER = ":";
037   private final String version;
038
039   private ProfileVersion(String version) {
040      this.version = version;
041   }
042
043   /**
044    * Version.
045    *
046    * @return the string
047    */
048   public String version() {
049      return version;
050   }
051
052   /**
053    * Latest version of the ZUGFeRD Specification.
054    *
055    * @return the string
056    */
057   public static ProfileVersion latestVersion() {
058      return ProfileVersion.values()[ProfileVersion.values().length - 1];
059   }
060
061   /**
062    * Parses the given version string
063    *
064    * @param version the version
065    * @return the profile version or IllegalArgumentException if provided version is not known.
066    */
067   public static ProfileVersion parse(String version) {
068      for (ProfileVersion profileVersion : ProfileVersion.values()) {
069         if (profileVersion.version().equals(version)) { return profileVersion; }
070      }
071      throw new EnumConstantNotPresentException(ProfileVersion.class, version);
072   }
073
074   /**
075    * Extract version from full name.
076    *
077    * @param fullName the full name
078    * @return the version of provided in the full name or empty string
079    */
080   public static ProfileVersion extractVersion(String fullName) {
081      if (Strings.isNullOrEmpty(fullName)) { return null; }
082      String[] tokens = fullName.split(DELIMITER);
083      int versionTokenPosition = tokens.length-2;
084      String version = tokens[versionTokenPosition];
085      return parse(version);
086   }
087
088   @Override
089   public String toString() {
090      return version();
091   }
092}