001/*
002 * Copyright 2015 DuraSpace, Inc.
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 */
016package org.fcrepo.kernel.api.services.functions;
017
018import static java.util.UUID.randomUUID;
019
020import java.util.stream.IntStream;
021import java.util.StringJoiner;
022
023/**
024 * Unique value minter that creates hierarchical IDs from a UUID
025 *
026 * @author awoods
027 * @author acoburn
028 */
029public interface HierarchicalIdentifierSupplier extends UniqueValueSupplier {
030
031    static final int DEFAULT_LENGTH = 2;
032    static final int DEFAULT_COUNT = 4;
033
034    /**
035     * Mint a unique identifier as a UUID
036     *
037     * @return uuid
038     */
039    @Override
040    default public String get() {
041
042        final String s = randomUUID().toString();
043        final StringJoiner joiner = new StringJoiner("/", "", "/" + s);
044
045        IntStream.rangeClosed(0, DEFAULT_COUNT - 1)
046                 .forEach(x -> joiner.add(s.substring(x * DEFAULT_LENGTH, (x + 1) * DEFAULT_LENGTH)));
047
048        return joiner.toString();
049    }
050}