SMUtils.java

  1. /**
  2.  *
  3.  * Copyright © 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.sm;

  18. import java.math.BigInteger;

  19. public class SMUtils {

  20.     private static long MASK_32_BIT = BigInteger.ONE.shiftLeft(32).subtract(BigInteger.ONE).longValue();

  21.     /**
  22.      * Quoting XEP-198 4.:
  23.      * "In the unlikely case that the number of stanzas handled during a stream management session exceeds the number
  24.      * of digits that can be represented by the unsignedInt datatype as specified in XML Schema Part 2 [10]
  25.      * (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)."
  26.      *
  27.      * @param height
  28.      * @return the incremented height
  29.      */
  30.     public static long incrementHeight(long height) {
  31.         return ++height & MASK_32_BIT;
  32.     }

  33.     /**
  34.      * Calculates the delta of the last known stanza handled count and the new
  35.      * reported stanza handled count while considering that the new value may be
  36.      * wrapped after 2^32-1.
  37.      *
  38.      * @param reportedHandledCount
  39.      * @param lastKnownHandledCount
  40.      * @return the delta
  41.      */
  42.     public static long calculateDelta(long reportedHandledCount, long lastKnownHandledCount) {
  43.         return (reportedHandledCount - lastKnownHandledCount) & MASK_32_BIT;
  44.     }
  45. }