net.sf.saxon.tree.util.DocumentNumberAllocator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Saxon-HE Show documentation
Show all versions of Saxon-HE Show documentation
The XSLT and XQuery Processor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2023 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.tree.util;
/**
* This class (which has one instance per Configuration) is used to allocate unique document
* numbers. It's a separate class so that it can act as a monitor for synchronization
*/
public class DocumentNumberAllocator {
// Changed to a long in Saxon 9.4, because a user reported an int overflowing
// on a system that had been in live operation for several months. The effect wasn't fatal,
// but could cause incorrect node identity tests.
private long nextDocumentNumber = 0;
// Negative document numbers are used for streamed documents. This means that streamed
// nodes always precede unstreamed nodes in document order. We take advantage of this
// when sorting a sequence that contains both streamed and unstreamed nodes.
private long nextStreamedDocumentNumber = -2; // -1 is special
/**
* Allocate a unique document number
*
* @return a unique document number
*/
public synchronized long allocateDocumentNumber() {
return nextDocumentNumber++;
}
/**
* Allocate a unique document number for a streamed document
*
* @return a unique document number for a streamed document
*/
public synchronized long allocateStreamedDocumentNumber() {
return nextStreamedDocumentNumber--;
}
}