com.hubspot.horizon.Headers Maven / Gradle / Ivy
package com.hubspot.horizon;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
public class Headers implements Iterable {
private final List headerList;
private final LinkedHashMap> headerMap;
public Headers(List headers) {
this.headerList = headers;
this.headerMap = new LinkedHashMap>();
for (Header header : headers) {
String key = header.getName().toLowerCase();
if (headerMap.containsKey(key)) {
headerMap.get(key).add(header.getValue());
} else {
headerMap.put(key, Lists.newArrayList(header.getValue()));
}
}
}
@Override
public Iterator iterator() {
return headerList.iterator();
}
public List get(String name) {
List headers = headerMap.get(Preconditions.checkNotNull(name).toLowerCase());
return headers == null ? Collections.emptyList() : headers;
}
@Nullable
public String getFirst(String name) {
List headers = get(Preconditions.checkNotNull(name).toLowerCase());
return headers.isEmpty() ? null : headers.get(0);
}
}