RSMSet.java

  1. /**
  2.  *
  3.  * Copyright © 2014 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 org.jivesoftware.smack.packet.Stanza;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. public class RSMSet implements ExtensionElement {

  22.     public static final String ELEMENT = "set";
  23.     public static final String NAMESPACE = "http://jabber.org/protocol/rsm";

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

  32.     public static enum PageDirection {
  33.         before,
  34.         after;
  35.     }

  36.     public RSMSet(int max) {
  37.         this(max, -1);
  38.     }

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

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

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

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

  76.     public String getAfter() {
  77.         return after;
  78.     }

  79.     public String getBefore() {
  80.         return before;
  81.     }

  82.     public int getCount() {
  83.         return count;
  84.     }

  85.     public int getIndex() {
  86.         return index;
  87.     }

  88.     public String getLast() {
  89.         return last;
  90.     }

  91.     public int getMax() {
  92.         return max;
  93.     }

  94.     public String getFirst() {
  95.         return firstString;
  96.     }

  97.     public int getFirstIndex() {
  98.         return firstIndex;
  99.     }

  100.     @Override
  101.     public String getElementName() {
  102.         return ELEMENT;
  103.     }

  104.     @Override
  105.     public String getNamespace() {
  106.         return NAMESPACE;
  107.     }

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

  128.     public static RSMSet from(Stanza packet) {
  129.         return (RSMSet) packet.getExtension(ELEMENT, NAMESPACE);
  130.     }

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

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