mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 09:09:38 +02:00
[ibb] Use UInt16 for 'seq' and fix its handling
Fixes a off-by-one error when incrementing 'seq'. Thanks to Kim Alvefur <zash@zash.se> for spotting this.
This commit is contained in:
parent
02c9058c3d
commit
a4bb5bfda8
17 changed files with 297 additions and 81 deletions
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2019 Florian Schmaus
|
||||
* Copyright 2019-2020 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,7 +16,9 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.datatypes;
|
||||
|
||||
public abstract class Scalar extends java.lang.Number {
|
||||
import org.jivesoftware.smack.util.DefaultCharSequence;
|
||||
|
||||
public abstract class Scalar extends java.lang.Number implements DefaultCharSequence {
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -83,4 +85,11 @@ public abstract class Scalar extends java.lang.Number {
|
|||
public final String toString() {
|
||||
return number.toString();
|
||||
}
|
||||
|
||||
public abstract Scalar getMinValue();
|
||||
|
||||
public abstract Scalar getMaxValue();
|
||||
|
||||
public abstract Scalar incrementedByOne();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2019 Florian Schmaus
|
||||
* Copyright 2019-2020 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,12 +21,18 @@ import org.jivesoftware.smack.util.NumberUtil;
|
|||
/**
|
||||
* A number representing an unsigned 16-bit integer. Can be used for values with the XML schema type "xs:unsingedShort".
|
||||
*/
|
||||
public final class UInt16 extends Scalar {
|
||||
public final class UInt16 extends Scalar implements Comparable<UInt16> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final int number;
|
||||
|
||||
public static final int MIN_VALUE_INT = 0;
|
||||
public static final int MAX_VALUE_INT = (1 << 16) - 1;
|
||||
|
||||
public static final UInt16 MIN_VALUE = UInt16.from(MIN_VALUE_INT);
|
||||
public static final UInt16 MAX_VALUE = UInt16.from(MAX_VALUE_INT);
|
||||
|
||||
private UInt16(int number) {
|
||||
super(NumberUtil.requireUShort16(number));
|
||||
this.number = number;
|
||||
|
@ -54,4 +60,25 @@ public final class UInt16 extends Scalar {
|
|||
|
||||
return super.equals(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(UInt16 o) {
|
||||
return Integer.compare(number, o.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UInt16 getMinValue() {
|
||||
return MIN_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UInt16 getMaxValue() {
|
||||
return MAX_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UInt16 incrementedByOne() {
|
||||
int incrementedValue = number < MAX_VALUE_INT ? number + 1 : 0;
|
||||
return UInt16.from(incrementedValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2019 Florian Schmaus
|
||||
* Copyright 2019-2020 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -27,6 +27,12 @@ public final class UInt32 extends Scalar {
|
|||
|
||||
private final long number;
|
||||
|
||||
public static final long MIN_VALUE_LONG = 0;
|
||||
public static final long MAX_VALUE_LONG = (1L << 32) - 1;
|
||||
|
||||
public static final UInt32 MIN_VALUE = UInt32.from(MAX_VALUE_LONG);
|
||||
public static final UInt32 MAX_VALUE = UInt32.from(MAX_VALUE_LONG);
|
||||
|
||||
private UInt32(long number) {
|
||||
super(NumberUtil.requireUInt32(number));
|
||||
this.number = number;
|
||||
|
@ -55,4 +61,20 @@ public final class UInt32 extends Scalar {
|
|||
|
||||
return super.equals(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UInt32 getMinValue() {
|
||||
return MIN_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UInt32 getMaxValue() {
|
||||
return MAX_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UInt32 incrementedByOne() {
|
||||
long incrementedValue = number < MAX_VALUE_LONG ? number + 1 : 0;
|
||||
return UInt32.from(incrementedValue);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,4 +55,28 @@ public class SmackParsingException extends Exception {
|
|||
super(uriSyntaxException);
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequiredValueMissingException extends SmackParsingException {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public RequiredValueMissingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class RequiredAttributeMissingException extends RequiredValueMissingException {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public RequiredAttributeMissingException(String attributeName) {
|
||||
super("The required attribute '" + attributeName + "' is missing (or has the empty String as value)");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util;
|
||||
|
||||
public interface DefaultCharSequence extends CharSequence {
|
||||
|
||||
@Override
|
||||
default int length() {
|
||||
return toString().length();
|
||||
}
|
||||
|
||||
@Override
|
||||
default char charAt(int index) {
|
||||
return toString().charAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
default CharSequence subSequence(int start, int end) {
|
||||
return toString().subSequence(start, end);
|
||||
}
|
||||
|
||||
}
|
|
@ -29,6 +29,7 @@ import org.jivesoftware.smack.datatypes.UInt16;
|
|||
import org.jivesoftware.smack.datatypes.UInt32;
|
||||
import org.jivesoftware.smack.packet.XmlEnvironment;
|
||||
import org.jivesoftware.smack.parsing.SmackParsingException;
|
||||
import org.jivesoftware.smack.parsing.SmackParsingException.RequiredAttributeMissingException;
|
||||
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
|
||||
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
|
||||
import org.jivesoftware.smack.xml.XmlPullParser;
|
||||
|
@ -231,6 +232,14 @@ public class ParserUtils {
|
|||
return UInt16.from(integer);
|
||||
}
|
||||
|
||||
public static UInt16 getRequiredUInt16Attribute(XmlPullParser parser, String name) throws RequiredAttributeMissingException {
|
||||
UInt16 uint16 = getUInt16Attribute(parser, name);
|
||||
if (uint16 == null) {
|
||||
throw new SmackParsingException.RequiredAttributeMissingException(name);
|
||||
}
|
||||
return uint16;
|
||||
}
|
||||
|
||||
public static int getIntegerFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
String intString = parser.nextText();
|
||||
return Integer.valueOf(intString);
|
||||
|
|
|
@ -362,6 +362,20 @@ public class XmlStringBuilder implements Appendable, CharSequence, Element {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #optAttribute(String, CharSequence)}, but with a different method name. This method can be used if
|
||||
* the provided attribute value argument type causes ambiguity in method overloading. For example if the type is a
|
||||
* subclass of Number and CharSequence.
|
||||
*
|
||||
* @param name the name of the attribute.
|
||||
* @param value the value of the attribute.
|
||||
* @return a reference to this object.
|
||||
* @since 4.5
|
||||
*/
|
||||
public XmlStringBuilder optAttributeCs(String name, CharSequence value) {
|
||||
return optAttribute(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given attribute if {@code value => 0}.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.datatypes;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class UInt16Test {
|
||||
|
||||
@Test
|
||||
public void testMaxValue() {
|
||||
assertEquals(65535, UInt16.MAX_VALUE_INT);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.datatypes;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class UInt32Test {
|
||||
|
||||
@Test
|
||||
public void testMaxValue() {
|
||||
assertEquals(4294967295L, UInt32.MAX_VALUE_LONG);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue