001/**
002 *
003 * Copyright © 2014 Florian Schmaus
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.smack.util;
018
019import java.util.Collection;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022
023public class PacketUtil {
024
025    /**
026     * Get a extension element from a collection.
027     * @param collection
028     * @param element
029     * @param namespace
030     * @param <PE>
031     * @return the extension element
032     * @deprecated use {@link #extensionElementFrom(Collection, String, String)} instead.
033     */
034    @Deprecated
035    public static <PE extends ExtensionElement> PE packetExtensionfromCollection(
036            Collection<ExtensionElement> collection, String element,
037            String namespace) {
038        return extensionElementFrom(collection, element, namespace);
039    }
040
041    /**
042     * Get a extension element from a collection.
043     *
044     * @param collection Collection of ExtensionElements.
045     * @param element name of the targeted ExtensionElement.
046     * @param namespace namespace of the targeted ExtensionElement.
047     * @param <PE> Type of the ExtensionElement
048     *
049     * @return the extension element
050     * @deprecated use {@link #extensionElementFrom(Collection, String, String)} instead
051     */
052    @Deprecated
053    public static <PE extends ExtensionElement> PE packetExtensionFromCollection(
054                    Collection<ExtensionElement> collection, String element,
055                    String namespace) {
056        return extensionElementFrom(collection, element, namespace);
057    }
058
059    /**
060     * Get a extension element from a collection.
061     *
062     * @param collection Collection of ExtensionElements.
063     * @param element name of the targeted ExtensionElement.
064     * @param namespace namespace of the targeted ExtensionElement.
065     * @param <PE> Type of the ExtensionElement
066     *
067     * @return the extension element
068     */
069    @SuppressWarnings("unchecked")
070    public static <PE extends ExtensionElement> PE extensionElementFrom(Collection<ExtensionElement> collection,
071                    String element, String namespace) {
072        for (ExtensionElement packetExtension : collection) {
073            if ((element == null || packetExtension.getElementName().equals(
074                            element))
075                            && packetExtension.getNamespace().equals(namespace)) {
076                return (PE) packetExtension;
077            }
078        }
079        return null;
080    }
081}