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 "IDGenerator.java".  Description: 
010"Interface providing methods for generating message 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 * MessageID generator interface. This is a replacement for the legacy
032 * MessageIDGenerator class, which should not be used anymore.
033 * <p>
034 * An instance implementing this interface can be configured by setting the
035 * property in the {@link ca.uhn.hl7v2.parser.ParserConfiguration} class and
036 * will then be used to generate IDs for all messages created by HAPI. The
037 * default implementation is {@link FileBasedHiLoGenerator}.
038 * <p>
039 * HAPI does not provide ID generators that depend on external resources (except
040 * for files). Implementors are encouraged to add their own classes
041 * (implementing {@link IDGenerator.Ordered}, e.g. based on database tables
042 * or sequences, and use it as delegate in {@link DelegatingHiLoGenerator}.
043 * 
044 * @see DelegatingHiLoGenerator
045 * @see FileBasedHiLoGenerator
046 * 
047 * @author Christian Ohr
048 */
049public interface IDGenerator {
050
051        /**
052         * Generates and returns a new ID. Note: This method must be implemented to
053         * be thread-safe.
054         * 
055         * @return a new ID.
056         * 
057         * @throws IOException
058         */
059        String getID() throws IOException;
060
061        /**
062         * Extension for continuous ID sequences
063         */
064        interface Ordered extends IDGenerator {
065
066                void reset();
067                long getIncrement();
068
069        }
070
071        /**
072         * Ordered ID sequences with a configurable increment
073         */
074        abstract class OrderedSupport implements Ordered {
075
076                private long increment = 1L;
077
078                OrderedSupport(long increment) {
079                        super();
080                        this.increment = increment;
081                }
082
083                public OrderedSupport() {
084                        this(1);
085                }
086
087                public void reset() {
088                }
089
090                public long getIncrement() {
091                        return increment;
092                }
093
094        }
095
096}