This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 210
Look up headers in a case insensitive way #199
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* -\-\- | ||
* Spotify Apollo API Interfaces | ||
* -- | ||
* Copyright (C) 2013 - 2017 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
package com.spotify.apollo; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
import java.util.AbstractMap.SimpleImmutableEntry; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@AutoValue | ||
abstract class Headers { | ||
|
||
static Headers EMPTY = create(Collections.emptyMap()); | ||
|
||
static Headers create(Map<String, String> headers) { | ||
final List<Map.Entry<String, String>> headersList = new ArrayList<>(headers.size()); | ||
headers.entrySet().forEach(h -> insertOrReplace(headersList, h)); | ||
return new AutoValue_Headers(ImmutableList.copyOf(headersList)); | ||
} | ||
|
||
public Optional<String> get(String name) { | ||
Objects.requireNonNull(name, "Header names cannot be null"); | ||
|
||
for (int i = 0; i < entries().size(); i++) { | ||
final Map.Entry<String, String> headerEntry = entries().get(i); | ||
if (name.equalsIgnoreCase(headerEntry.getKey())) { | ||
return Optional.ofNullable(headerEntry.getValue()); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
public Map<String, String> asMap() { | ||
ImmutableMap.Builder<String, String> headers = ImmutableMap.builder(); | ||
entries().forEach(headers::put); | ||
return headers.build(); | ||
} | ||
|
||
public abstract ImmutableList<Map.Entry<String, String>> entries(); | ||
|
||
private static void insertOrReplace(List<Map.Entry<String, String>> headerList, | ||
Map.Entry<String, String> newHeader) { | ||
|
||
// Replace existing header with new key (letter case can be overwritten) and value | ||
for (int i = 0; i < headerList.size(); i++) { | ||
Map.Entry<String, String> currentHeader = headerList.get(i); | ||
if (currentHeader.getKey().equalsIgnoreCase(newHeader.getKey())) { | ||
headerList.set(i, new SimpleImmutableEntry<>(newHeader)); | ||
return; | ||
} | ||
} | ||
|
||
// No matching entry present, add new entry | ||
headerList.add(new SimpleImmutableEntry<>(newHeader)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used a lot in apollo. Should I memoized it already in the constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure; doing so would increase the memory footprint - you'd need some measurements to be sure, I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave the optimizations for later then.