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