001/**
002 *
003 * Copyright 2019-2024 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        return Long.hashCode(number);
052    }
053
054    @Override
055    public boolean equals(Object other) {
056        if (other instanceof UInt32) {
057            UInt32 otherUint32 = (UInt32) other;
058            return number == otherUint32.number;
059        }
060
061        return super.equals(other);
062    }
063
064    @Override
065    public UInt32 getMinValue() {
066        return MIN_VALUE;
067    }
068
069    @Override
070    public UInt32 getMaxValue() {
071        return MAX_VALUE;
072    }
073
074    @Override
075    public UInt32 incrementedByOne() {
076        long incrementedValue = number < MAX_VALUE_LONG ? number + 1 : 0;
077        return UInt32.from(incrementedValue);
078    }
079}