InterfaceVersion.java

  1. package fr.sii.ogham.sms.builder.cloudhopper;

  2. import java.util.Arrays;
  3. import java.util.List;

  4. import com.cloudhopper.smpp.SmppConstants;

  5. import fr.sii.ogham.core.convert.StringToEnumConverter.FactoryMethod;

  6. /**
  7.  * Wraps the version for builder
  8.  *
  9.  * @author AurĂ©lien Baudet
  10.  *
  11.  */
  12. @FactoryMethod(name = "of")
  13. public enum InterfaceVersion {
  14.     /**
  15.      * SMPP 3.3 the oldest used version (despite its limitations, it is still
  16.      * widely used); supports GSM only. Generates an immediate response for each
  17.      * message sent.
  18.      */
  19.     VERSION_3_3(SmppConstants.VERSION_3_3, "3.3"),
  20.     /**
  21.      * SMPP 3.4 adds optional Tag-Length-Value (TLV) parameters, support of
  22.      * non-GSM SMS technologies and the transceiver support (single connections
  23.      * that can send and receive messages). The exchange of SMPP request and
  24.      * response PDUs between an ESME Transmitter and SMSC may occur
  25.      * synchronously or asynchronously.
  26.      */
  27.     VERSION_3_4(SmppConstants.VERSION_3_4, "3.4"),
  28.     /**
  29.      * SMPP 5.0 is the latest version of SMPP; adds support for cell
  30.      * broadcasting, smart flow control. As of 2019, it is not widely used.
  31.      */
  32.     VERSION_5_0(SmppConstants.VERSION_5_0, "5.0", "5");

  33.     private final byte value;
  34.     private final List<String> versionNames;

  35.     InterfaceVersion(byte value, String... versionNames) {
  36.         this.value = value;
  37.         this.versionNames = Arrays.asList(versionNames);
  38.     }

  39.     /**
  40.      * Get the version as byte value.
  41.      *
  42.      * @return the byte value for the version
  43.      */
  44.     public byte value() {
  45.         return value;
  46.     }
  47.    
  48.     /**
  49.      * The version as string.
  50.      *
  51.      * @return the version string
  52.      */
  53.     public String getVersionString() {
  54.         return versionNames.get(0);
  55.     }

  56.     private boolean matches(String versionName) {
  57.         for (String v : versionNames) {
  58.             if (v.equals(versionName)) {
  59.                 return true;
  60.             }
  61.         }
  62.         return false;
  63.     }

  64.     /**
  65.      * Get the {@link InterfaceVersion} from the byte value.
  66.      *
  67.      * If value is {@code null}, {@code null} is returned
  68.      *
  69.      * @param value
  70.      *            the byte value that represents the version
  71.      * @return the corresponding interface version
  72.      * @throws IllegalArgumentException
  73.      *             if the value doesn't match any known version
  74.      */
  75.     public static InterfaceVersion fromValue(Byte value) {
  76.         if (value == null) {
  77.             return null;
  78.         }
  79.         for (InterfaceVersion version : values()) {
  80.             if (version.value() == value) {
  81.                 return version;
  82.             }
  83.         }
  84.         throw new IllegalArgumentException("Unknown interface version with value " + value);
  85.     }

  86.     /**
  87.      * Get the {@link InterfaceVersion} from either the enum name (
  88.      * {@link InterfaceVersion#VERSION_3_3},
  89.      * {@link InterfaceVersion#VERSION_3_4},
  90.      * {@link InterfaceVersion#VERSION_5_0}) or from a string that represents
  91.      * the version ("3.3", "3.4", "5.0").
  92.      *
  93.      * If version name is {@code null}, {@code null} is returned
  94.      *
  95.      * @param version
  96.      *            the version as string
  97.      * @return the interface version
  98.      * @throws IllegalArgumentException
  99.      *             if the version name doesn't match any known version
  100.      */
  101.     public static InterfaceVersion of(String version) {
  102.         if (version == null || version.isEmpty()) {
  103.             return null;
  104.         }
  105.         for (InterfaceVersion iv : values()) {
  106.             if (iv.name().equals(version)) {
  107.                 return iv;
  108.             }
  109.             if (iv.matches(version)) {
  110.                 return iv;
  111.             }
  112.         }
  113.         throw new IllegalArgumentException("Unknown interface version named " + version);
  114.     }
  115. }