View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.models.idgen;
21  
22  import com.fasterxml.uuid.Generators;
23  import com.fasterxml.uuid.impl.TimeBasedGenerator;
24  import com.google.common.collect.ListMultimap;
25  import eu.ehri.project.persistence.Bundle;
26  
27  import java.util.Collection;
28  import java.util.UUID;
29  
30  /**
31   * Generates a generic ID for tertiary node types.
32   */
33  public enum GenericIdGenerator implements IdGenerator {
34  
35      INSTANCE;
36  
37      // NB: We use a time-based UUID generator here because
38      // sequential UUIDs prevent index fragmentation.
39      private static final TimeBasedGenerator timeBasedGenerator
40              = Generators.timeBasedGenerator();
41  
42      public ListMultimap<String, String> handleIdCollision(Collection<String> scopeIds, Bundle bundle) {
43          throw new RuntimeException(String.format("Index collision generating identifier for item type '%s' with data: '%s'",
44                  bundle.getType().getName(), bundle));
45      }
46  
47      /**
48       * Generates a random String.
49       *
50       * @param scopeIds array of scope ids
51       * @param bundle   The entity's bundle data
52       * @return A generated ID string
53       */
54      public String generateId(Collection<String> scopeIds, Bundle bundle) {
55          return getIdBase(bundle);
56      }
57  
58      /**
59       * Return the base data for the id, sans scoping.
60       * @param bundle The entity's bundle.
61       * @return The base id string.
62       */
63      public String getIdBase(Bundle bundle) {
64          return getTimeBasedUUID().toString();
65      }
66  
67      /**
68       * Get a new time-based UUID.
69       * @return A time based UUID.
70       */
71      public static UUID getTimeBasedUUID() {
72          return timeBasedGenerator.generate();
73      }
74  }