001/**
002 *
003 * Copyright © 2014 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.sm;
018
019import java.math.BigInteger;
020
021public class SMUtils {
022
023    private static long MASK_32_BIT = BigInteger.ONE.shiftLeft(32).subtract(BigInteger.ONE).longValue();
024
025    /**
026     * Quoting XEP-198 4.:
027     * "In the unlikely case that the number of stanzas handled during a stream management session exceeds the number
028     * of digits that can be represented by the unsignedInt datatype as specified in XML Schema Part 2 [10]
029     * (i.e., 2^32), the value of 'h' SHALL be reset from 2^32-1 back to zero (rather than being incremented to 2^32)."
030     * 
031     * @param height
032     * @return the incremented height
033     */
034    public static long incrementHeight(long height) {
035        return ++height & MASK_32_BIT;
036    }
037
038    /**
039     * Calculates the delta of the last known stanza handled count and the new
040     * reported stanza handled count while considering that the new value may be
041     * wrapped after 2^32-1.
042     * 
043     * @param reportedHandledCount
044     * @param lastKnownHandledCount
045     * @return the delta
046     */
047    public static long calculateDelta(long reportedHandledCount, long lastKnownHandledCount) {
048        return (reportedHandledCount - lastKnownHandledCount) & MASK_32_BIT;
049    }
050}