1 // Copyright (c) 2003-2004 Brian Wellington (bwelling (at) xbill.org) 2 3 package org.xbill.DNS; 4 5 /** 6 * Helper functions for doing serial arithmetic. These should be used when 7 * setting/checking SOA serial numbers. SOA serial number arithmetic is 8 * defined in RFC 1982. 9 * 10 * @author Brian Wellington 11 */ 12 13 public final class Serial { 14 15 private static final long MAX32 = 0xFFFFFFFFL; 16 17 private 18 Serial() { 19 } 20 21 /** 22 * Compares two numbers using serial arithmetic. The numbers are assumed 23 * to be 32 bit unsigned integers stored in longs. 24 * @param serial1 The first integer 25 * @param serial2 The second integer 26 * @return 0 if the 2 numbers are equal, a positive number if serial1 is greater 27 * than serial2, and a negative number if serial2 is greater than serial1. 28 * @throws IllegalArgumentException serial1 or serial2 is out of range 29 */ 30 public static int 31 compare(long serial1, long serial2) { 32 if (serial1 < 0 || serial1 > MAX32) 33 throw new IllegalArgumentException(serial1 + " out of range"); 34 if (serial2 < 0 || serial2 > MAX32) 35 throw new IllegalArgumentException(serial2 + " out of range"); 36 long diff = serial1 - serial2; 37 if (diff >= MAX32) 38 diff -= (MAX32 + 1); 39 else if (diff < -MAX32) 40 diff += (MAX32 + 1); 41 return (int)diff; 42 } 43 44 /** 45 * Increments a serial number. The number is assumed to be a 32 bit unsigned 46 * integer stored in a long. This basically adds 1 and resets the value to 47 * 0 if it is 2^32. 48 * @param serial The serial number 49 * @return The incremented serial number 50 * @throws IllegalArgumentException serial is out of range 51 */ 52 public static long 53 increment(long serial) { 54 if (serial < 0 || serial > MAX32) 55 throw new IllegalArgumentException(serial + " out of range"); 56 if (serial == MAX32) 57 return 0; 58 return serial + 1; 59 } 60 61 } 62