View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
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 org.openehealth.ipf.commons.ihe.fhir.translation;
18  
19  import java.util.Optional;
20  import java.util.function.Supplier;
21  import java.util.stream.Stream;
22  
23  /**
24   * <p>
25   * The FHIR Identifier type introduces a new mechanism for conveying the originating system of a particular identifier.
26   * Whereas HL7-based messages identify an assigning organization as a HD or an OID in the 'root'
27   * attribute respectively, HL7 FHIR permits the use of a URI. This requires some configuration on the part of actors
28   * to correctly map a URI to an OID or HD to maintain consistency with other actors which are not implementing the
29   * FHIR specification.
30   * </p>
31   * <p>
32   * The same is basically true for code system identifications. HL7 FHIR permits the use of a URI, whereas
33   * HL7-based messages and requests use HD or OIDs.
34   * </p>
35   *
36   * @author Christian Ohr
37   * @since 3.1
38   */
39  public interface UriMapper {
40  
41      /**
42       * Translates an URI into an OID. If the URI is an (OID) URN, the namespace-specific part should,
43       * be used as the OID.
44       *
45       * @param uri the URI
46       * @return the mapped OID
47       * @throws InvalidUriSyntaxException if the uri string is no valid URI
48       */
49      Optional<String> uriToOid(String uri);
50  
51      /**
52       * Translates an URI into a Namespace.
53       *
54       * @param uri the URI
55       * @return the mapped namespace
56       * @throws InvalidUriSyntaxException if the uri string is no valid URI
57       */
58      Optional<String> uriToNamespace(String uri);
59  
60      /**
61       * Translates an OID into an URI. Instead of a real mapping, an URN can be derived from the OID
62       * (i.e. urn:oid:1.2.3.4), but in general the inverse mapping to {@link #uriToOid(String)} should
63       * be applied.
64       *
65       * @param oid the OID
66       * @return the mapped URI
67       * @throws InvalidUriSyntaxException
68       */
69      String oidToUri(String oid);
70  
71      /**
72       * Translates an Namespace into an URI. Instead of a real mapping, an URN can be derived from the namespace
73       * (i.e. urn:pin:namespace), but in general the inverse mapping to {@link #uriToNamespace(String)} (String)} should
74       * be applied.
75       *
76       * @param namespace the namespace
77       * @return the mapped URI
78       * @throws InvalidUriSyntaxException
79       */
80      String namespaceToUri(String namespace);
81  
82  
83      static <T> Optional<T> findFirst(Supplier<Optional<T>>... suppliers) {
84          return Stream.of(suppliers)
85                  .map(Supplier::get)
86                  .filter(Optional::isPresent)
87                  .map(Optional::get)
88                  .findFirst();
89      }
90  
91  }