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","rc"),
029
030   /** The release candidate extended. */
031   RCE("rce","rce"),
032
033   /** The version 1.0 */
034   V1P0("1p0","1.0");
035
036   private static final String DELIMITER = ":";
037   private final String version;
038   private final String versionAlt;
039   
040   private ProfileVersion(String version, String versionAlt) {
041      this.version = version;
042      this.versionAlt = versionAlt;
043   }
044
045   /**
046    * Version.
047    *
048    * @return the version string
049    */
050   public String version() {
051      return version;
052   }
053   
054   /**
055    * alternative representation of the version with the .(dot).
056    *
057    * @return the alternative representation of the version
058    */
059   public String versionAlt() {
060      return versionAlt;
061   }
062
063   /**
064    * Latest version of the ZUGFeRD Specification.
065    *
066    * @return the string
067    */
068   public static ProfileVersion latestVersion() {
069      return ProfileVersion.values()[ProfileVersion.values().length - 1];
070   }
071
072   /**
073    * Parses the given version string
074    *
075    * @param version the version
076    * @return the profile version or IllegalArgumentException if provided version is not known.
077    */
078   public static ProfileVersion parse(String version) {
079      for (ProfileVersion profileVersion : ProfileVersion.values()) {
080         if (profileVersion.versionAlt().equals(version) ||
081               profileVersion.version().equals(version)) { return profileVersion; }
082      }
083      throw new EnumConstantNotPresentException(ProfileVersion.class, version);
084   }
085
086   /**
087    * Extract version from full name.
088    *
089    * @param fullName the full name
090    * @return the version of provided in the full name or empty string
091    */
092   public static ProfileVersion extractVersion(String fullName) {
093      if (Strings.isNullOrEmpty(fullName)) { return null; }
094      String[] tokens = fullName.split(DELIMITER);
095      int versionTokenPosition = tokens.length-2;
096      String version = tokens[versionTokenPosition];
097      return parse(version);
098   }
099
100   @Override
101   public String toString() {
102      return version();
103   }
104}