001package com.fs.starfarer.api.util;
002
003import java.util.ArrayList;
004import java.util.LinkedHashMap;
005import java.util.List;
006
007public class ListMap<V> extends LinkedHashMap<String, List<V>> {
008        private static final long serialVersionUID = 1L;
009
010        public void add(String key, V value) {
011                if (value == null) return;
012                List<V> list = getList(key);
013                list.add(value);
014        }
015        public void remove(String key, V value) {
016                List<V> list = getList(key);
017                list.remove(value);
018                if (list.isEmpty()) {
019                        remove(key);
020                }
021        }
022        
023        @Override
024        public List<V> get(Object key) {
025                return getList((String) key);
026        }
027        public List<V> getList(String key) {
028                List<V> list = super.get(key);
029                if (list == null) {
030                        list = new ArrayList<V>();
031                        put(key, list);
032                }
033                return list;
034        }
035
036        public void cleanupEmptyLists() {
037                for (String id : new ArrayList<>(keySet())) {
038                        if (containsKey(id) && getList(id).isEmpty()) {
039                                remove(id);
040                        }
041                }
042        }
043}