RSMSet.java

  1. /**
  2.  *
  3.  * Copyright © 2014-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.rsm.packet;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.packet.Stanza;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. public class RSMSet implements ExtensionElement {

  23.     public static final String ELEMENT = "set";
  24.     public static final String NAMESPACE = "http://jabber.org/protocol/rsm";
  25.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  26.     private final String after;
  27.     private final String before;
  28.     private final int count;
  29.     private final int index;
  30.     private final String last;
  31.     private final int max;
  32.     private final String firstString;
  33.     private final int firstIndex;

  34.     public enum PageDirection {
  35.         before,
  36.         after
  37.     }

  38.     public RSMSet(int max) {
  39.         this(max, -1);
  40.     }

  41.     public RSMSet(int max, int index) {
  42.         this(null, null, -1, index, null, max, null, -1);
  43.     }

  44.     public RSMSet(String item, PageDirection pageDirection) {
  45.         this(-1, item, pageDirection);
  46.     }

  47.     public RSMSet(int max, String item, PageDirection pageDirection) {
  48.         switch (pageDirection) {
  49.         case before:
  50.             this.before = item;
  51.             this.after = null;
  52.             break;
  53.         case after:
  54.             this.before = null;
  55.             this.after = item;
  56.             break;
  57.         default:
  58.             throw new AssertionError();
  59.         }
  60.         this.count = -1;
  61.         this.index = -1;
  62.         this.last = null;
  63.         this.max = max;
  64.         this.firstString = null;
  65.         this.firstIndex = -1;
  66.     }

  67.     public RSMSet(String after, String before, int count, int index,
  68.                     String last, int max, String firstString, int firstIndex) {
  69.         this.after = after;
  70.         this.before = before;
  71.         this.count = count;
  72.         this.index = index;
  73.         this.last = last;
  74.         this.max = max;
  75.         this.firstString = firstString;
  76.         this.firstIndex = firstIndex;
  77.     }

  78.     public String getAfter() {
  79.         return after;
  80.     }

  81.     public String getBefore() {
  82.         return before;
  83.     }

  84.     public int getCount() {
  85.         return count;
  86.     }

  87.     public int getIndex() {
  88.         return index;
  89.     }

  90.     public String getLast() {
  91.         return last;
  92.     }

  93.     public int getMax() {
  94.         return max;
  95.     }

  96.     public String getFirst() {
  97.         return firstString;
  98.     }

  99.     public int getFirstIndex() {
  100.         return firstIndex;
  101.     }

  102.     @Override
  103.     public String getElementName() {
  104.         return ELEMENT;
  105.     }

  106.     @Override
  107.     public String getNamespace() {
  108.         return NAMESPACE;
  109.     }

  110.     @Override
  111.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  112.         XmlStringBuilder xml = new XmlStringBuilder(this);
  113.         xml.rightAngleBracket();
  114.         xml.optElement("after", after);
  115.         xml.optElement("before", before);
  116.         xml.optIntElement("count", count);
  117.         if (firstString != null) {
  118.             xml.halfOpenElement("first");
  119.             xml.optIntAttribute("index", firstIndex);
  120.             xml.rightAngleBracket();
  121.             xml.append(firstString);
  122.             xml.closeElement("first");
  123.         }
  124.         xml.optIntElement("index", index);
  125.         xml.optElement("last", last);
  126.         xml.optIntElement("max", max);
  127.         xml.closeElement(this);
  128.         return xml;
  129.     }

  130.     public static RSMSet from(Stanza packet) {
  131.         return (RSMSet) packet.getExtensionElement(ELEMENT, NAMESPACE);
  132.     }

  133.     public static RSMSet newAfter(String after) {
  134.         return new RSMSet(after, PageDirection.after);
  135.     }

  136.     public static RSMSet newBefore(String before) {
  137.         return new RSMSet(before, PageDirection.before);
  138.     }
  139. }