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 16-bit integer. Can be used for values with the XML schema type "xs:unsingedShort".
023 */
024public final class UInt16 extends Scalar implements Comparable<UInt16> {
025
026    private static final long serialVersionUID = 1L;
027
028    private final int number;
029
030    public static final int MIN_VALUE_INT = 0;
031    public static final int MAX_VALUE_INT = (1 << 16) - 1;
032
033    public static final UInt16 MIN_VALUE = UInt16.from(MIN_VALUE_INT);
034    public static final UInt16 MAX_VALUE = UInt16.from(MAX_VALUE_INT);
035
036    private UInt16(int number) {
037        super(NumberUtil.requireUShort16(number));
038        this.number = number;
039    }
040
041    public int nativeRepresentation() {
042        return number;
043    }
044
045    public static UInt16 from(int number) {
046        return new UInt16(number);
047    }
048
049    @Override
050    public int hashCode() {
051        return number;
052    }
053
054    @Override
055    public boolean equals(Object other) {
056        if (other instanceof UInt16) {
057            UInt16 otherUint16 = (UInt16) other;
058            return number == otherUint16.number;
059        }
060
061        return super.equals(other);
062    }
063
064    @Override
065    public int compareTo(UInt16 o) {
066        return Integer.compare(number, o.number);
067    }
068
069    @Override
070    public UInt16 getMinValue() {
071        return MIN_VALUE;
072    }
073
074    @Override
075    public UInt16 getMaxValue() {
076        return MAX_VALUE;
077    }
078
079    @Override
080    public UInt16 incrementedByOne() {
081        int incrementedValue = number < MAX_VALUE_INT ? number + 1 : 0;
082        return UInt16.from(incrementedValue);
083    }
084}