001/** 002 * Copyright 2014 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.mint; 017 018import static com.codahale.metrics.MetricRegistry.name; 019import static com.google.common.base.Joiner.on; 020import static com.google.common.base.Splitter.fixedLength; 021import static java.util.UUID.randomUUID; 022 023import org.fcrepo.kernel.identifiers.PidMinter; 024import org.fcrepo.metrics.RegistryService; 025 026import com.codahale.metrics.Timer; 027 028/** 029 * PID minter that creates hierarchical IDs for a UUID 030 * 031 * @author awoods 032 */ 033public class UUIDPathMinter implements PidMinter { 034 035 static final Timer timer = RegistryService.getInstance().getMetrics().timer( 036 name(UUIDPathMinter.class, "mint")); 037 038 private static final int DEFAULT_LENGTH = 2; 039 040 private static final int DEFAULT_COUNT = 4; 041 042 private final int length; 043 044 private final int count; 045 046 /** 047 * Configure the path minter using some reasonable defaults for the length 048 * and count of the branch nodes 049 */ 050 public UUIDPathMinter() { 051 this(DEFAULT_LENGTH, DEFAULT_COUNT); 052 } 053 054 /** 055 * Configure the path minter for the length of the keys and depth of the 056 * branch node prefix 057 * 058 * @param length how long the branch node identifiers should be 059 * @param count how many branch nodes should be inserted 060 */ 061 public UUIDPathMinter(final int length, final int count) { 062 super(); 063 this.length = length; 064 this.count = count; 065 } 066 067 /** 068 * Mint a unique identifier as a UUID 069 * 070 * @return uuid 071 */ 072 @Override 073 public String mintPid() { 074 075 try (final Timer.Context context = timer.time()) { 076 final String s = randomUUID().toString(); 077 078 if (length == 0 || count == 0) { 079 return s; 080 } 081 082 final Iterable<String> split = 083 fixedLength(length).split(s.substring(0, length * count)); 084 085 return on("/").join(split) + "/" + s; 086 } 087 } 088}