001/**
002 *
003 * Copyright © 2014-2020 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.packet;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.packet.Stanza;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025public class RSMSet implements ExtensionElement {
026
027    public static final String ELEMENT = "set";
028    public static final String NAMESPACE = "http://jabber.org/protocol/rsm";
029    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
030
031    private final String after;
032    private final String before;
033    private final int count;
034    private final int index;
035    private final String last;
036    private final int max;
037    private final String firstString;
038    private final int firstIndex;
039
040    public enum PageDirection {
041        before,
042        after
043    }
044
045    public RSMSet(int max) {
046        this(max, -1);
047    }
048
049    public RSMSet(int max, int index) {
050        this(null, null, -1, index, null, max, null, -1);
051    }
052
053    public RSMSet(String item, PageDirection pageDirection) {
054        this(-1, item, pageDirection);
055    }
056
057    public RSMSet(int max, String item, PageDirection pageDirection) {
058        switch (pageDirection) {
059        case before:
060            this.before = item;
061            this.after = null;
062            break;
063        case after:
064            this.before = null;
065            this.after = item;
066            break;
067        default:
068            throw new AssertionError();
069        }
070        this.count = -1;
071        this.index = -1;
072        this.last = null;
073        this.max = max;
074        this.firstString = null;
075        this.firstIndex = -1;
076    }
077
078    public RSMSet(String after, String before, int count, int index,
079                    String last, int max, String firstString, int firstIndex) {
080        this.after = after;
081        this.before = before;
082        this.count = count;
083        this.index = index;
084        this.last = last;
085        this.max = max;
086        this.firstString = firstString;
087        this.firstIndex = firstIndex;
088    }
089
090    public String getAfter() {
091        return after;
092    }
093
094    public String getBefore() {
095        return before;
096    }
097
098    public int getCount() {
099        return count;
100    }
101
102    public int getIndex() {
103        return index;
104    }
105
106    public String getLast() {
107        return last;
108    }
109
110    public int getMax() {
111        return max;
112    }
113
114    public String getFirst() {
115        return firstString;
116    }
117
118    public int getFirstIndex() {
119        return firstIndex;
120    }
121
122    @Override
123    public String getElementName() {
124        return ELEMENT;
125    }
126
127    @Override
128    public String getNamespace() {
129        return NAMESPACE;
130    }
131
132    @Override
133    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
134        XmlStringBuilder xml = new XmlStringBuilder(this);
135        xml.rightAngleBracket();
136        xml.optElement("after", after);
137        xml.optElement("before", before);
138        xml.optIntElement("count", count);
139        if (firstString != null) {
140            xml.halfOpenElement("first");
141            xml.optIntAttribute("index", firstIndex);
142            xml.rightAngleBracket();
143            xml.append(firstString);
144            xml.closeElement("first");
145        }
146        xml.optIntElement("index", index);
147        xml.optElement("last", last);
148        xml.optIntElement("max", max);
149        xml.closeElement(this);
150        return xml;
151    }
152
153    public static RSMSet from(Stanza packet) {
154        return (RSMSet) packet.getExtensionElement(ELEMENT, NAMESPACE);
155    }
156
157    public static RSMSet newAfter(String after) {
158        return new RSMSet(after, PageDirection.after);
159    }
160
161    public static RSMSet newBefore(String before) {
162        return new RSMSet(before, PageDirection.before);
163    }
164}