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