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

  35.     /**
  36.      * Calculates the delta of the last known stanza handled count and the new
  37.      * reported stanza handled count while considering that the new value may be
  38.      * wrapped after 2^32-1.
  39.      *
  40.      * @param reportedHandledCount TODO javadoc me please
  41.      * @param lastKnownHandledCount TODO javadoc me please
  42.      * @return the delta
  43.      */
  44.     public static long calculateDelta(long reportedHandledCount, long lastKnownHandledCount) {
  45.         if (lastKnownHandledCount > reportedHandledCount) {
  46.             throw new IllegalStateException("Illegal Stream Management State: Last known handled count (" + lastKnownHandledCount + ") is greater than reported handled count (" + reportedHandledCount + ')');
  47.         }
  48.         return (reportedHandledCount - lastKnownHandledCount) & MASK_32_BIT;
  49.     }
  50. }