001/**
002 *
003 * Copyright © 2016-2021 Florian Schmaus and Frank Matheron
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.mam.element;
018
019/**
020 * MAM versions supported by Smack.
021 *
022 * @since 4.5.0
023 */
024public enum MamVersion {
025    // Note that the order in which the enum values are defined, is also the order in which we attempt to find a
026    // supported version. The versions should therefore be listed in order of newest to oldest, so that Smack prefers
027    // using a newer version over an older version.
028    MAM2("urn:xmpp:mam:2") {
029        @Override
030        public MamElementFactory newElementFactory() {
031            return new MamV2ElementFactory();
032        }
033    },
034    MAM1("urn:xmpp:mam:1") {
035        @Override
036        public MamElementFactory newElementFactory() {
037            return new MamV1ElementFactory();
038        }
039    };
040
041    private final String namespace;
042
043    MamVersion(String namespace) {
044        this.namespace = namespace;
045    }
046
047    /**
048     * Each MAM version is identified by its namespace. Returns the namespace for this MAM version.
049     * @return the namespace of the MAM version
050     */
051    public String getNamespace() {
052        return namespace;
053    }
054
055    /**
056     * Creates a new factory that creates IQ's and extension objects for this MAM version.
057     * @return the factory
058     */
059    public abstract MamElementFactory newElementFactory();
060
061    public static MamVersion fromNamespace(String namespace) {
062        for (MamVersion v : MamVersion.values()) {
063            if (v.namespace.equals(namespace)) {
064                return v;
065            }
066        }
067        throw new IllegalArgumentException("Unsupported namespace: " + namespace);
068    }
069}