001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "DelegatingHiLoGenerator.java".  Description: 
010"Generator that delegates generation of hi IDs " 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132001.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025 */
026package ca.uhn.hl7v2.util.idgenerator;
027
028import java.io.IOException;
029
030/**
031 * Default implementation of a HiLo ID generator that allows to use another
032 * (non-HiLo) ID generator for generating the "hi" part of the ID. The delegate
033 * must increment its ID is discrete steps > 1, so that the gaps can be filled
034 * with "lo" IDs.
035 * <p>
036 *     Example:
037 * </p>
038 * <pre>
039 *     Hi IDs: 0, 100, 200, 300 (increment = 100)
040 *     Lo IDs: 1,2,3,...,100,1,2,3,....
041 *     Resulting ID (Hi + Lo): 1,2,3,99,100,101,102,...
042 * </pre>
043 * 
044 * @see FileBasedHiLoGenerator
045 * @author Christian Ohr
046 */
047public class DelegatingHiLoGenerator extends HiLoGenerator {
048
049        private IDGenerator.Ordered delegate;
050
051        public DelegatingHiLoGenerator() {
052                super();
053        }
054
055        public DelegatingHiLoGenerator(IDGenerator.Ordered delegate) {
056                super();
057                this.delegate = delegate;
058        }
059
060        @Override
061        protected long getNextHiId() throws IOException {
062                if (delegate == null)
063                        throw new NullPointerException(
064                                        "Must initialize delegate IDGenerator");
065                return Long.parseLong(delegate.getID());
066        }
067
068        public void setDelegate(IDGenerator.Ordered delegate) {
069                this.delegate = delegate;
070        }
071
072        @Override
073        protected void resetHiId() {
074                delegate.reset();
075        }
076
077    /**
078     * THe maximum "lo" ID is the increment of the hi ID.
079     * @return
080     */
081        @Override
082        public long getMaxLo() {
083                return delegate.getIncrement();
084        }
085
086}