001package org.hl7.fhir.utilities;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Collections;
006import java.util.Comparator;
007import java.util.HashMap;
008import java.util.Iterator;
009import java.util.List;
010import java.util.Map;
011
012public class NamedItemList<T extends org.hl7.fhir.utilities.NamedItemList.NamedItem> implements Collection<T> {
013  public interface NamedItem {
014    public String getListName();
015  }
016
017
018  private static final int SIZE_CUTOFF_MAP = 10;
019
020
021  private List<T> list = new ArrayList<>();
022  private Map<String, List<T>> map = null;
023
024  @Override
025  public int size() {
026    return list.size();
027  }
028
029  @Override
030  public boolean isEmpty() {
031    return list.isEmpty();
032  }
033
034  @Override
035  public boolean contains(Object o) {
036    return list.contains(o);
037  }
038
039  @Override
040  public Iterator<T> iterator() {
041    return list.iterator();
042  }
043
044  @Override
045  public Object[] toArray() {
046    return list.toArray();
047  }
048
049  @Override
050  public <T> T[] toArray(T[] a) {
051    return list.toArray(a);
052  }
053
054  @Override
055  public boolean add(T e) {
056    addToMap(e);
057    return list.add(e);
058  }
059  public void add(int index, T e) {
060    addToMap(e);
061    list.add(index, e);
062  }
063
064  @Override
065  public boolean remove(Object o) {
066    map = null;  
067    return list.remove(o);
068  }
069
070  @Override
071  public boolean containsAll(Collection<?> c) {
072    return list.containsAll(c);
073  }
074
075  @Override
076  public boolean addAll(Collection<? extends T> c) {
077    for(T e : c) {
078      addToMap(e);
079    }
080    return list.addAll(c);
081  }
082
083  @Override
084  public boolean removeAll(Collection<?> c) {
085    map = null;  
086    return list.removeAll(c);  
087  }
088
089  @Override
090  public boolean retainAll(Collection<?> c) {
091    map = null;  
092    return list.retainAll(c);
093  }
094
095  @Override
096  public void clear() {
097    list.clear();
098    map = null;    
099  }
100
101  public List<T> getByName(String name) {
102    List<T> res = new ArrayList<>();
103    if (size() > SIZE_CUTOFF_MAP) {
104      if (map == null) {
105        buildMap();
106      }
107      List<T> l = map.get(name);
108      if (l != null) {
109        res.addAll(l);
110      }
111    } else {
112      for (T child : list) {
113        if (name.equals(child.getListName())) {
114          res.add(child);
115        }
116      }
117    }
118    return res;
119  }
120  
121  public int getSizeByName(String name) {
122    if (map == null) {
123      buildMap();
124    }
125    List<T> l = map.get(name);
126    return l == null ? 0 : l.size();
127  }
128
129  public T get(int c) {
130    return list.get(c);
131  }
132
133  private void buildMap() {
134    map = new HashMap<>();
135    for (T child : list) {
136      addToMap(child);
137    }
138  }
139  
140  private void addToMap(T child) {
141    if (map == null) {
142      // map will be re-built anyway in next call to getByName
143      return;
144    }
145    String n = child.getListName();
146    List<T> l = map.get(n);
147    if (l == null) {
148      l = new ArrayList<>();
149      map.put(n,l);
150    }
151    l.add(child);
152  }
153
154  public void sort(Comparator<? super T> sorter) {
155    Collections.sort(list, sorter);
156  }
157
158  public void clearMap() {
159    map = null;
160  }
161}