MamVersion.java

  1. /**
  2.  *
  3.  * Copyright © 2016-2021 Florian Schmaus and Frank Matheron
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.mam.element;

  18. /**
  19.  * MAM versions supported by Smack.
  20.  *
  21.  * @since 4.5.0
  22.  */
  23. public enum MamVersion {
  24.     // Note that the order in which the enum values are defined, is also the order in which we attempt to find a
  25.     // supported version. The versions should therefore be listed in order of newest to oldest, so that Smack prefers
  26.     // using a newer version over an older version.
  27.     MAM2("urn:xmpp:mam:2") {
  28.         @Override
  29.         public MamElementFactory newElementFactory() {
  30.             return new MamV2ElementFactory();
  31.         }
  32.     },
  33.     MAM1("urn:xmpp:mam:1") {
  34.         @Override
  35.         public MamElementFactory newElementFactory() {
  36.             return new MamV1ElementFactory();
  37.         }
  38.     };

  39.     private final String namespace;

  40.     MamVersion(String namespace) {
  41.         this.namespace = namespace;
  42.     }

  43.     /**
  44.      * Each MAM version is identified by its namespace. Returns the namespace for this MAM version.
  45.      * @return the namespace of the MAM version
  46.      */
  47.     public String getNamespace() {
  48.         return namespace;
  49.     }

  50.     /**
  51.      * Creates a new factory that creates IQ's and extension objects for this MAM version.
  52.      * @return the factory
  53.      */
  54.     public abstract MamElementFactory newElementFactory();

  55.     public static MamVersion fromNamespace(String namespace) {
  56.         for (MamVersion v : MamVersion.values()) {
  57.             if (v.namespace.equals(namespace)) {
  58.                 return v;
  59.             }
  60.         }
  61.         throw new IllegalArgumentException("Unsupported namespace: " + namespace);
  62.     }
  63. }