1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 17:19:39 +02:00

Introduce UInt(16|32) datatypes

This commit is contained in:
Florian Schmaus 2019-06-10 21:39:36 +02:00
parent c0183775fe
commit ce70308099
16 changed files with 262 additions and 49 deletions

View file

@ -0,0 +1,70 @@
/**
*
* Copyright 2019 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;
public abstract class Scalar extends java.lang.Number {
/**
*
*/
private static final long serialVersionUID = 1L;
private final java.lang.Number number;
protected Scalar(java.lang.Number number) {
this.number = number;
}
public final Number number() {
return number;
}
@Override
public final int intValue() {
return number.intValue();
}
@Override
public final long longValue() {
return number.longValue();
}
@Override
public final float floatValue() {
return number.floatValue();
}
@Override
public final double doubleValue() {
return number.doubleValue();
}
@Override
public final int hashCode() {
return number.hashCode();
}
@Override
public final boolean equals(Object other) {
return number.equals(other);
}
@Override
public final String toString() {
return number.toString();
}
}

View file

@ -0,0 +1,42 @@
/**
*
* Copyright 2019 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 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 {
private static final long serialVersionUID = 1L;
private final int number;
private UInt16(int number) {
super(NumberUtil.requireUShort16(number));
this.number = number;
}
public int nativeRepresentation() {
return number;
}
public static UInt16 from(int number) {
return new UInt16(number);
}
}

View file

@ -0,0 +1,42 @@
/**
*
* Copyright 2019 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 org.jivesoftware.smack.util.NumberUtil;
/**
* A number representing an unsigned 32-bit integer. Can be used for values with the XML schema type "xs:unsignedInt".
*/
public final class UInt32 extends Scalar {
private static final long serialVersionUID = 1L;
private final long number;
private UInt32(long number) {
super(NumberUtil.requireUInt32(number));
this.number = number;
}
public long nativeRepresentation() {
return number;
}
public static UInt32 from(long number) {
return new UInt32(number);
}
}

View file

@ -0,0 +1,21 @@
/**
*
* Copyright 2019 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.
*/
/**
* Custom datatypes for Integers.
*/
package org.jivesoftware.smack.datatypes;

View file

@ -35,13 +35,14 @@ public class NumberUtil {
*
* @param value
*/
public static void requireUInt32(long value) {
public static long requireUInt32(long value) {
if (value < 0) {
throw new IllegalArgumentException("unsigned 32-bit integers can't be negative");
}
if (value > ((1L << 32) - 1)) {
throw new IllegalArgumentException("unsigned 32-bit integers can't be greater then 2^32 - 1");
}
return value;
}
/**

View file

@ -25,6 +25,8 @@ import java.util.Locale;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.datatypes.UInt16;
import org.jivesoftware.smack.datatypes.UInt32;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
@ -204,6 +206,14 @@ public class ParserUtils {
}
}
public static UInt16 getUInt16Attribute(XmlPullParser parser, String name) {
Integer integer = getIntegerAttribute(parser, name);
if (integer == null) {
return null;
}
return UInt16.from(integer);
}
public static int getIntegerFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
String intString = parser.nextText();
return Integer.valueOf(intString);
@ -226,6 +236,14 @@ public class ParserUtils {
}
}
public static UInt32 getUInt32Attribute(XmlPullParser parser, String name) {
Long l = getLongAttribute(parser, name);
if (l == null) {
return null;
}
return UInt32.from(l);
}
public static double getDoubleFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
String doubleString = parser.nextText();
return Double.valueOf(doubleString);