001/**
002 *
003 * Copyright 2019-2020 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.smack.datatypes;
018
019import org.jivesoftware.smack.util.NumberUtil;
020
021/**
022 * A number representing an unsigned 32-bit integer. Can be used for values with the XML schema type "xs:unsignedInt".
023 */
024public final class UInt32 extends Scalar {
025
026    private static final long serialVersionUID = 1L;
027
028    private final long number;
029
030    public static final long MIN_VALUE_LONG = 0;
031    public static final long MAX_VALUE_LONG = (1L << 32) - 1;
032
033    public static final UInt32 MIN_VALUE = UInt32.from(MAX_VALUE_LONG);
034    public static final UInt32 MAX_VALUE = UInt32.from(MAX_VALUE_LONG);
035
036    private UInt32(long number) {
037        super(NumberUtil.requireUInt32(number));
038        this.number = number;
039    }
040
041    public long nativeRepresentation() {
042        return number;
043    }
044
045    public static UInt32 from(long number) {
046        return new UInt32(number);
047    }
048
049    @Override
050    public int hashCode() {
051        // TODO: Use Long.hashCode(number) once Smack's minimum Android SDK level is 24 or higher.
052        return (int) (number ^ (number >>> 32));
053    }
054
055    @Override
056    public boolean equals(Object other) {
057        if (other instanceof UInt32) {
058            UInt32 otherUint32 = (UInt32) other;
059            return number == otherUint32.number;
060        }
061
062        return super.equals(other);
063    }
064
065    @Override
066    public UInt32 getMinValue() {
067        return MIN_VALUE;
068    }
069
070    @Override
071    public UInt32 getMaxValue() {
072        return MAX_VALUE;
073    }
074
075    @Override
076    public UInt32 incrementedByOne() {
077        long incrementedValue = number < MAX_VALUE_LONG ? number + 1 : 0;
078        return UInt32.from(incrementedValue);
079    }
080}