|
| 1 | +/** |
| 2 | + * Copyright 2012-2018 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.mock; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +public class RequestHeaders { |
| 24 | + |
| 25 | + public static class Builder { |
| 26 | + |
| 27 | + private Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>(); |
| 28 | + |
| 29 | + private Builder() {} |
| 30 | + |
| 31 | + public Builder add(String key, Collection<String> values) { |
| 32 | + if (!headers.containsKey(key)) { |
| 33 | + headers.put(key, values); |
| 34 | + } else { |
| 35 | + Collection<String> previousValues = headers.get(key); |
| 36 | + previousValues.addAll(values); |
| 37 | + headers.put(key, previousValues); |
| 38 | + } |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public Builder add(String key, String value) { |
| 43 | + if (!headers.containsKey(key)) { |
| 44 | + headers.put(key, new ArrayList<String>(Arrays.asList(value))); |
| 45 | + } else { |
| 46 | + final Collection<String> values = headers.get(key); |
| 47 | + values.add(value); |
| 48 | + headers.put(key, values); |
| 49 | + } |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + public RequestHeaders build() { |
| 54 | + return new RequestHeaders(this); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + public static final Map<String, Collection<String>> EMPTY = Collections.emptyMap(); |
| 60 | + |
| 61 | + public static Builder builder() { |
| 62 | + return new Builder(); |
| 63 | + } |
| 64 | + |
| 65 | + public static RequestHeaders of(Map<String, Collection<String>> headers) { |
| 66 | + return new RequestHeaders(headers); |
| 67 | + } |
| 68 | + |
| 69 | + private Map<String, Collection<String>> headers; |
| 70 | + |
| 71 | + private RequestHeaders(Builder builder) { |
| 72 | + this.headers = builder.headers; |
| 73 | + } |
| 74 | + |
| 75 | + private RequestHeaders(Map<String, Collection<String>> headers) { |
| 76 | + this.headers = headers; |
| 77 | + } |
| 78 | + |
| 79 | + public int size() { |
| 80 | + return headers.size(); |
| 81 | + } |
| 82 | + |
| 83 | + public int sizeOf(String key) { |
| 84 | + if (!headers.containsKey(key)) { |
| 85 | + return 0; |
| 86 | + } |
| 87 | + return headers.get(key).size(); |
| 88 | + } |
| 89 | + |
| 90 | + public Collection<String> fetch(String key) { |
| 91 | + return headers.get(key); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean equals(Object obj) { |
| 96 | + if (this == obj) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + if (obj == null) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + if (getClass() != obj.getClass()) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + final RequestHeaders other = (RequestHeaders) obj; |
| 106 | + return this.headers.equals(other.headers); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String toString() { |
| 111 | + StringBuilder builder = new StringBuilder(); |
| 112 | + for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) { |
| 113 | + builder.append(entry).append(',').append(' '); |
| 114 | + } |
| 115 | + if (builder.length() > 0) { |
| 116 | + return builder.substring(0, builder.length() - 2); |
| 117 | + } |
| 118 | + return "no"; |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments