001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006package org.fcrepo.kernel.api.services.functions; 007 008import org.springframework.stereotype.Component; 009 010import static java.util.UUID.randomUUID; 011 012import java.util.StringJoiner; 013import java.util.stream.IntStream; 014 015/** 016 * Unique value minter that creates hierarchical IDs from a UUID 017 * 018 * @author rdfloyd 019 * @author whikloj 020 */ 021@Component 022public class ConfigurableHierarchicalSupplier implements UniqueValueSupplier { 023 024 private static final int DEFAULT_LENGTH = 0; 025 private static final int DEFAULT_COUNT = 0; 026 027 private final int length; 028 private final int count; 029 030 031 /** 032 * Mint a hierarchical identifier with args to control length and count of the pairtree. A length or count of ZERO 033 * will return a non-hierarchical identifier. 034 * 035 * @param desiredLength the desired length of pairtree parts 036 * @param desiredCount the desired number of pairtree parts 037 */ 038 public ConfigurableHierarchicalSupplier(final int desiredLength, final int desiredCount) { 039 length = desiredLength; 040 count = desiredCount; 041 } 042 043 /** 044 * Mint a unique identifier by default using defaults 045 * 046 */ 047 public ConfigurableHierarchicalSupplier() { 048 length = DEFAULT_LENGTH; 049 count = DEFAULT_COUNT; 050 } 051 052 /** 053 * Mint a unique identifier as a UUID 054 * 055 * @return uuid 056 */ 057 @Override 058 public String get() { 059 060 final String s = randomUUID().toString(); 061 final String id; 062 063 if (count > 0 && length > 0) { 064 final StringJoiner joiner = new StringJoiner("/", "", "/" + s); 065 066 IntStream.rangeClosed(0, count - 1) 067 .forEach(x -> joiner.add(s.substring(x * length, (x + 1) * length))); 068 id = joiner.toString(); 069 } else { 070 id = s; 071 } 072 return id; 073 } 074}