01: // Copyright (c) 2003-2004 Brian Wellington (bwelling@xbill.org)
02:
03: package org.xbill.DNS;
04:
05: /**
06: * Helper functions for doing serial arithmetic. These should be used when
07: * setting/checking SOA serial numbers. SOA serial number arithmetic is
08: * defined in RFC 1982.
09: *
10: * @author Brian Wellington
11: */
12:
13: public final class Serial {
14:
15: private static final long MAX32 = 0xFFFFFFFFL;
16:
17: private Serial() {
18: }
19:
20: /**
21: * Compares two numbers using serial arithmetic. The numbers are assumed
22: * to be 32 bit unsigned integers stored in longs.
23: * @param serial1 The first integer
24: * @param serial2 The second integer
25: * @return 0 if the 2 numbers are equal, a positive number if serial1 is greater
26: * than serial2, and a negative number if serial2 is greater than serial1.
27: * @throws IllegalArgumentException serial1 or serial2 is out of range
28: */
29: public static int compare(long serial1, long serial2) {
30: if (serial1 < 0 || serial1 > MAX32)
31: throw new IllegalArgumentException(serial1
32: + " out of range");
33: if (serial2 < 0 || serial2 > MAX32)
34: throw new IllegalArgumentException(serial2
35: + " 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 increment(long serial) {
53: if (serial < 0 || serial > MAX32)
54: throw new IllegalArgumentException(serial + " out of range");
55: if (serial == MAX32)
56: return 0;
57: return serial + 1;
58: }
59:
60: }
|