001package org.hl7.fhir.r5.context;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.Comparator;
006import java.util.HashMap;
007import java.util.List;
008import java.util.Map;
009import java.util.Set;
010import java.util.UUID;
011
012import org.hl7.fhir.r5.context.BaseWorkerContext.MetadataResourceVersionComparator;
013import org.hl7.fhir.r5.model.CodeSystem;
014import org.hl7.fhir.r5.model.MetadataResource;
015import org.hl7.fhir.utilities.VersionUtilities;
016
017/**
018 * This manages a cached list of resources, and provides high speed access by URL / URL+version, and assumes that patch version doesn't matter for access
019 * note, though, that not all resources have semver versions
020 * 
021 * @author graha
022 *
023 */
024
025public class MetadataResourceManager<T extends MetadataResource> {
026
027  public class MetadataResourceVersionComparator<T extends MetadataResource> implements Comparator<T> {
028    @Override
029    public int compare(T arg1, T arg2) {
030      String v1 = arg1.getVersion();
031      String v2 = arg2.getVersion();
032      if (v1 == null && v2 == null) {
033        return Integer.compare(list.indexOf(arg1), list.indexOf(arg2)); // retain original order
034      } else if (v1 == null) {
035        return -1;
036      } else if (v2 == null) {
037        return 1;
038      } else {
039        String mm1 = VersionUtilities.getMajMin(v1);
040        String mm2 = VersionUtilities.getMajMin(v2);
041        if (mm1 == null || mm2 == null) {
042          return v1.compareTo(v2);
043        } else {
044          return mm1.compareTo(mm2);
045        }
046      }
047    }
048  }
049
050
051  private List<T> list = new ArrayList<>();
052  private Map<String, T> map = new HashMap<>();
053  
054  public void copy(MetadataResourceManager<T> source) {
055    list.clear();
056    map.clear();
057    list.addAll(source.list);
058    map.putAll(source.map);
059  }
060  
061  public void see(T r) {
062    if (!r.hasId()) {
063      r.setId(UUID.randomUUID().toString());
064    }
065    if (map.containsKey(r.getId())) {
066      drop(r.getId());      
067    }
068    list.add(r);
069    map.put(r.getId(), r); // we do this so we can drop by id
070
071    if (r.hasUrl()) {
072      // first, this is the correct reosurce for this version (if it has a version)
073      if (r.hasVersion()) {
074        map.put(r.getUrl()+"|"+r.getVersion(), r);
075      }
076      updateList(r.getUrl(), r.getVersion());
077    }
078  }
079
080  private void updateList(String url, String version) {
081    List<T> rl = new ArrayList<T>();
082    for (T t : list) {
083      if (url.equals(t.getUrl()) && !rl.contains(t)) {
084        rl.add(t);
085      }
086    }
087    if (rl.size() > 0) {
088      // sort by version as much as we are able
089      Collections.sort(rl, new MetadataResourceVersionComparator<T>());
090      // the current is the latest
091      map.put(url, rl.get(rl.size()-1));
092      // now, also, the latest for major/minor
093      if (version != null) {
094        T latest = null;
095        for (T t : rl) {
096          if (VersionUtilities.versionsCompatible(t.getVersion(), version)) {
097            latest = t;
098          }
099        }
100        if (latest != null) { // might be null if it's not using semver
101          map.put(url+"|"+VersionUtilities.getMajMin(latest.getVersion()), rl.get(rl.size()-1));
102        }
103      }
104    }
105  }
106 
107
108  public T get(String url) {
109    return map.get(url);
110  }
111  
112  public boolean has(String url) {
113    return map.containsKey(url);
114  }
115  
116  public T get(String system, String version) {
117    if (map.containsKey(system+"|"+version))
118      return map.get(system+"|"+version);
119    String mm = VersionUtilities.getMajMin(version);
120    if (mm != null)
121      return map.get(system+"|"+mm);
122    else
123      return null;
124  }
125  
126  public boolean has(String system, String version) {
127    if (map.containsKey(system+"|"+version))
128      return true;
129    String mm = VersionUtilities.getMajMin(version);
130    if (mm != null)
131      return map.containsKey(system+"|"+mm);
132    else
133      return false;
134  }
135  
136  public int size() {
137    return list.size();
138  }
139  
140  public void drop(String id) {
141    T res = map.get(id);
142    if (res != null) {
143      list.remove(res);
144      map.remove(id);
145      map.remove(res.getUrl());
146      if (res.hasVersion()) {
147        map.remove(res.getUrl()+"|"+res.getVersion());
148        String mm = VersionUtilities.getMajMin(res.getVersion());
149        if (mm != null) {
150          map.remove(res.getUrl()+"|"+mm);
151        }
152      }
153      updateList(res.getUrl(), res.getVersion()); 
154    }
155  }
156  
157  
158  public void listAll(List<T> result) {
159    result.addAll(list);    
160  }
161
162  public void listAllM(List<MetadataResource> result) {
163    result.addAll(list);    
164  }
165
166  public void clear() {
167    list.clear();
168    map.clear();
169    
170  }
171
172  public List<T> getList() {
173    return list;
174  }
175
176  public Set<String> keys() {
177    return map.keySet();
178  }
179  
180}