001/*
002 * (C) Copyright 2006-2007 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Dragos Mihalache
018 */
019package org.nuxeo.ecm.core.uidgen;
020
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * UID Sequencer interface defines a method to retrieve next ids based on a given key.
026 */
027public interface UIDSequencer {
028
029    /**
030     * Gets the sequencer name.
031     *
032     * @since 7.4
033     */
034    String getName();
035
036    /**
037     * Sets the sequencer name.
038     *
039     * @since 7.4
040     */
041    void setName(String name);
042
043    /**
044     * Init Sequencer
045     *
046     * @since 7.3
047     */
048    void init();
049
050    /**
051     * Initializes the sequencer with the given key to at least the given long id.
052     * <p>
053     * A sequence can only be incremented, so if its current id is greater than the given id the sequence won't be
054     * decremented to reach the given id.
055     *
056     * @since 9.10
057     */
058    void initSequence(String key, long id);
059
060    /**
061     * Initializes the sequencer with the given key to at least the given id.
062     * @since 7.4
063     * @deprecated since 9.10 use {@link #initSequence(String, long)} instead.
064     */
065    @Deprecated
066    void initSequence(String key, int id);
067
068    /**
069     * For the given key returns the incremented UID which is also stored in the same sequence entry. This is a
070     * "one time use" function for a document.
071     *
072     * @deprecated since 9.10 use {@link #getNextLong(String)} instead.
073     */
074    @Deprecated
075    int getNext(String key);
076
077    /**
078     * Extends {@link UIDSequencer#getNext(java.lang.String)} to return a long value. This method is compatible
079     * with getNext in the integer range.
080     *
081     * @since 8.3
082     */
083    long getNextLong(String key);
084
085    /**
086     * Returns a block containing {@code blockSize} sequences.
087     *
088     * @since 10.3
089     */
090    default List<Long> getNextBlock(String key, int blockSize) {
091        List<Long> ret = new ArrayList<>(blockSize);
092        for (int i = 0; i < blockSize; i++) {
093            ret.add(getNextLong(key));
094        }
095        return ret;
096    }
097
098    /**
099     * Cleanup callback
100     *
101     * @since 7.3
102     */
103    void dispose();
104
105}