View Javadoc
1   /*
2    *  Copyright 2022 Búraló Technologies
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package com.buralotech.oss.identifier.api;
18  
19  import java.time.Instant;
20  import java.time.temporal.Temporal;
21  
22  /**
23   * Generate identifiers and parse binary and textual representations of identifiers.
24   */
25  public interface IdentifierService {
26  
27      /**
28       * Generate an identifier using an underlying UUID generator.
29       *
30       * @return The generated identifier.
31       */
32      Identifier generate();
33  
34      /**
35       * Decode an identifier using its text representation.
36       *
37       * @param text The text representation.
38       * @return The identifier.
39       * @throws IllegalArgumentException If the text representation is not valid.
40       */
41      Identifier fromText(String text);
42  
43      /**
44       * Decode an identifier using its binary representation.
45       *
46       * @param binary The binary representation.
47       * @return The identifier.
48       * @throws IllegalArgumentException If the binary representation is not valid.
49       */
50      Identifier fromBinary(byte[] binary);
51  
52      /**
53       * Extract an instant from an identifier.
54       *
55       * @param identifier The identifier.
56       * @return The instant.
57       * @throws UnsupportedOperationException If the operation is not supported.
58       */
59      default Instant toInstant(final Identifier identifier) {
60          throw new UnsupportedOperationException("Cannot extract timestamp from " + identifier);
61      }
62  
63      /**
64       * Generate a lower-bound identifier for temporal value that can be used in range queries.
65       *
66       * @param time The temporal value ({@link java.time.Instant}, {@link java.time.LocalDate},
67       *             {@link java.time.LocalDateTime}, {@link java.time.OffsetDateTime},
68       *             {@link java.time.ZonedDateTime})
69       * @return A lower-bound identifier that can be used in a range query.
70       * @throws UnsupportedOperationException If the operation is not supported for the identifier type.
71       * @throws IllegalArgumentException If the temporal type is not supported.
72       */
73      default Identifier asLowerBound(final Temporal time) {
74          throw new UnsupportedOperationException("Cant generate lower-bound identifier for " + time);
75      }
76  
77      /**
78       * Generate an upper-bound identifier for temporal value that can be used in range queries.
79       *
80       * @param time The temporal value ({@link java.time.Instant}, {@link java.time.LocalDate},
81       *             {@link java.time.LocalDateTime}, {@link java.time.OffsetDateTime},
82       *             {@link java.time.ZonedDateTime})
83       * @return A upper-bound identifier that can be used in a range query.
84       * @throws UnsupportedOperationException If the operation is not supported for the identifier type.
85       * @throws IllegalArgumentException If the temporal type is not supported.
86       */
87      default Identifier asUpperBound(final Temporal time) {
88          throw new UnsupportedOperationException("Cant generate upper-bound identifier for " + time);
89      }
90  }