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.smackx.rsm;
018
019import java.util.Collection;
020import java.util.LinkedList;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.ExtensionElement;
024import org.jivesoftware.smack.util.PacketUtil;
025
026import org.jivesoftware.smackx.rsm.packet.RSMSet;
027import org.jivesoftware.smackx.rsm.packet.RSMSet.PageDirection;
028
029public class RSMManager {
030
031    Collection<ExtensionElement> page(int max) {
032        List<ExtensionElement> packetExtensions = new LinkedList<>();
033        packetExtensions.add(new RSMSet(max));
034        return packetExtensions;
035    }
036
037    Collection<ExtensionElement> continuePage(int max, Collection<ExtensionElement> returnedExtensions) {
038        return continuePage(max, returnedExtensions, null);
039    }
040
041    Collection<ExtensionElement> continuePage(int max,
042                    Collection<ExtensionElement> returnedExtensions,
043                    Collection<ExtensionElement> additionalExtensions) {
044        if (returnedExtensions == null) {
045            throw new IllegalArgumentException("returnedExtensions must no be null");
046        }
047        if (additionalExtensions == null) {
048            additionalExtensions = new LinkedList<>();
049        }
050        RSMSet resultRsmSet = PacketUtil.extensionElementFrom(returnedExtensions, RSMSet.ELEMENT, RSMSet.NAMESPACE);
051        if (resultRsmSet == null) {
052            throw new IllegalArgumentException("returnedExtensions did not contain a RSMset");
053        }
054        RSMSet continuePageRsmSet = new RSMSet(max, resultRsmSet.getLast(), PageDirection.after);
055        additionalExtensions.add(continuePageRsmSet);
056        return additionalExtensions;
057    }
058}