| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.tree.util; |
| 9 | |
| 10 | /** |
| 11 | * This class (which has one instance per Configuration) is used to allocate unique document |
| 12 | * numbers. It's a separate class so that it can act as a monitor for synchronization |
| 13 | */ |
| 14 | public class DocumentNumberAllocator { |
| 15 | |
| 16 | // Changed to a long in Saxon 9.4, because a user reported an int overflowing |
| 17 | // on a system that had been in live operation for several months. The effect wasn't fatal, |
| 18 | // but could cause incorrect node identity tests. |
| 19 | |
| 20 | private long nextDocumentNumber = 0; |
| 21 | |
| 22 | // Negative document numbers are used for streamed documents. This means that streamed |
| 23 | // nodes always precede unstreamed nodes in document order. We take advantage of this |
| 24 | // when sorting a sequence that contains both streamed and unstreamed nodes. |
| 25 | |
| 26 | private long nextStreamedDocumentNumber = -2; // -1 is special |
| 27 | |
| 28 | /** |
| 29 | * Allocate a unique document number |
| 30 | * |
| 31 | * @return a unique document number |
| 32 | */ |
| 33 | |
| 34 | public synchronized long allocateDocumentNumber() { |
| 35 | return nextDocumentNumber++; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Allocate a unique document number for a streamed document |
| 40 | * |
| 41 | * @return a unique document number for a streamed document |
| 42 | */ |
| 43 | |
| 44 | public synchronized long allocateStreamedDocumentNumber() { |
| 45 | return nextStreamedDocumentNumber--; |
| 46 | } |
| 47 | } |
| 48 |