|
| 1 | +package elasta.composer; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.common.collect.ListMultimap; |
| 5 | +import io.vertx.core.MultiMap; |
| 6 | + |
| 7 | +import java.util.*; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by sohan on 5/15/2017. |
| 11 | + */ |
| 12 | +final public class VertxMultiMap implements MultiMap { |
| 13 | + final ListMultimap<String, String> multimap; |
| 14 | + |
| 15 | + public VertxMultiMap(ListMultimap<String, String> multimap) { |
| 16 | + Objects.requireNonNull(multimap); |
| 17 | + this.multimap = multimap; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public String get(CharSequence name) { |
| 22 | + return getOne(String.valueOf(name)); |
| 23 | + } |
| 24 | + |
| 25 | + private String getOne(String key) { |
| 26 | + List<String> list = multimap.get(String.valueOf(key)); |
| 27 | + if (list.size() <= 0) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + return list.get(0); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String get(String name) { |
| 35 | + return getOne(name); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public List<String> getAll(String name) { |
| 40 | + return multimap.get(name); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public List<String> getAll(CharSequence name) { |
| 45 | + return multimap.get(String.valueOf(name)); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public List<Map.Entry<String, String>> entries() { |
| 50 | + return ImmutableList.copyOf(multimap.entries()); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean contains(String name) { |
| 55 | + return multimap.containsKey(name); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean contains(CharSequence name) { |
| 60 | + return multimap.containsKey(String.valueOf(name)); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean isEmpty() { |
| 65 | + return multimap.isEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Set<String> names() { |
| 70 | + return multimap.keySet(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public MultiMap add(String name, String value) { |
| 75 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public MultiMap add(CharSequence name, CharSequence value) { |
| 80 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public MultiMap add(String name, Iterable<String> values) { |
| 85 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public MultiMap add(CharSequence name, Iterable<CharSequence> values) { |
| 90 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public MultiMap addAll(MultiMap map) { |
| 95 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public MultiMap addAll(Map<String, String> headers) { |
| 100 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public MultiMap set(String name, String value) { |
| 105 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public MultiMap set(CharSequence name, CharSequence value) { |
| 110 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public MultiMap set(String name, Iterable<String> values) { |
| 115 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public MultiMap set(CharSequence name, Iterable<CharSequence> values) { |
| 120 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public MultiMap setAll(MultiMap map) { |
| 125 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public MultiMap setAll(Map<String, String> headers) { |
| 130 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public MultiMap remove(String name) { |
| 135 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public MultiMap remove(CharSequence name) { |
| 140 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public MultiMap clear() { |
| 145 | + throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported"); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int size() { |
| 150 | + return multimap.size(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Iterator<Map.Entry<String, String>> iterator() { |
| 155 | + return multimap.entries().iterator(); |
| 156 | + } |
| 157 | +} |
0 commit comments