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     *
028     * @param collection
029     * @param element
030     * @param namespace
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
045     * @param element
046     * @param namespace
047     * @return the extension element
048     */
049    @SuppressWarnings("unchecked")
050    public static <PE extends ExtensionElement> PE extensionElementFrom(Collection<ExtensionElement> collection,
051                    String element, String namespace) {
052        for (ExtensionElement packetExtension : collection) {
053            if ((element == null || packetExtension.getElementName().equals(
054                            element))
055                            && packetExtension.getNamespace().equals(namespace)) {
056                return (PE) packetExtension;
057            }
058        }
059        return null;
060    }
061}