View Javadoc
1   /*
2    * Copyright 2018 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.support;
18  
19  
20  import org.hl7.fhir.dstu3.model.Bundle;
21  import org.hl7.fhir.dstu3.model.NamingSystem;
22  
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.function.Predicate;
28  import java.util.stream.Collectors;
29  import java.util.stream.Stream;
30  
31  /**
32   * Abstract Implementation that holds naming systems in memory as a map of {@link NamingSystem} sets. Before an
33   * instance can be used, one of the adder methods of implementations must be called to initialize the
34   * bundle.
35   *
36   * @author Christian Ohr
37   * @since 3.4
38   */
39  public class AbstractNamingSystemServiceImpl implements NamingSystemService {
40  
41      protected transient Map<String, Set<NamingSystem>> namingSystems = new HashMap<>();
42  
43      public void addNamingSystems(Bundle bundle) {
44          this.namingSystems.merge(bundle.getId(), setOfNamingSystems(bundle), (set1, set2) -> {
45              Set<NamingSystem> result = new HashSet<>(set1);
46              result.addAll(set2);
47              return result;
48          });
49      }
50  
51      @Override
52      public Stream<NamingSystem> findNamingSystems(String id, Predicate<? super NamingSystem> predicate) {
53          if (!namingSystems.containsKey(id)) {
54              throw new IllegalArgumentException("No NamingSystem known with ID " + id);
55          }
56          return namingSystems.get(id).stream().filter(predicate);
57      }
58  
59      private Set<NamingSystem> setOfNamingSystems(Bundle bundle) {
60          return bundle.getEntry().stream()
61                  .map(bec -> (NamingSystem) bec.getResource())
62                  .collect(Collectors.toSet());
63      }
64  
65  }