From 27dee0bfd9480c2f179b135ec6defac7bb07d54b Mon Sep 17 00:00:00 2001 From: Everett Toews Date: Mon, 20 Aug 2012 19:02:33 -0500 Subject: [PATCH] Moved HttpMessage.getLowercaseHeaders() to Multimaps2.transformKeys() and org.jclouds.functions.ToLowerCase for better reuse. Added org.jclouds.util.Multimaps2Test for unit test. --- .../org/jclouds/functions/ToLowerCase.java | 39 ++++++++++++++++++ .../java/org/jclouds/http/HttpMessage.java | 20 +++------ .../java/org/jclouds/util/Multimaps2.java | 26 ++++++++++++ .../java/org/jclouds/util/Multimaps2Test.java | 41 +++++++++++++++++++ 4 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 core/src/main/java/org/jclouds/functions/ToLowerCase.java create mode 100644 core/src/test/java/org/jclouds/util/Multimaps2Test.java diff --git a/core/src/main/java/org/jclouds/functions/ToLowerCase.java b/core/src/main/java/org/jclouds/functions/ToLowerCase.java new file mode 100644 index 0000000000..488376973d --- /dev/null +++ b/core/src/main/java/org/jclouds/functions/ToLowerCase.java @@ -0,0 +1,39 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you 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 org.jclouds.functions; + +import static com.google.common.base.Preconditions.checkNotNull; + +import javax.inject.Singleton; + +import com.google.common.base.Function; + +/** + * @author Everett Toews + */ +@Singleton +public class ToLowerCase implements Function { + + @Override + public String apply(String input) { + checkNotNull(input, "input cannot be null"); + return input.toLowerCase(); + } + +} diff --git a/core/src/main/java/org/jclouds/http/HttpMessage.java b/core/src/main/java/org/jclouds/http/HttpMessage.java index 99d59313c8..5c1acb892e 100644 --- a/core/src/main/java/org/jclouds/http/HttpMessage.java +++ b/core/src/main/java/org/jclouds/http/HttpMessage.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.InputStream; import java.util.Collection; +import org.jclouds.functions.ToLowerCase; import org.jclouds.http.internal.PayloadEnclosingImpl; import org.jclouds.io.Payload; import org.jclouds.io.Payloads; @@ -32,7 +33,6 @@ import org.jclouds.util.Multimaps2; import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; -import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; @@ -186,25 +186,15 @@ public class HttpMessage extends PayloadEnclosingImpl { return headers; } - private Multimap getLowercaseHeaders() { - Multimap lowercaseHeaders = HashMultimap.create(getHeaders().keys().size(), 1); - - for (String header: getHeaders().keys()) { - for (String value: getHeaders().get(header)) { - lowercaseHeaders.put(header.toLowerCase(), value); - } - } - - return lowercaseHeaders; - } - /** * try to get the value, then try as lowercase. */ public String getFirstHeaderOrNull(String string) { Collection values = headers.get(string); - if (values.size() == 0) - values = getLowercaseHeaders().get(string.toLowerCase()); + if (values.size() == 0) { + Multimap lowerCaseHeaders = Multimaps2.transformKeys(getHeaders(), new ToLowerCase()); + values = lowerCaseHeaders.get(string.toLowerCase()); + } return (values.size() >= 1) ? values.iterator().next() : null; } diff --git a/core/src/main/java/org/jclouds/util/Multimaps2.java b/core/src/main/java/org/jclouds/util/Multimaps2.java index aaabbf8591..5d82137b83 100644 --- a/core/src/main/java/org/jclouds/util/Multimaps2.java +++ b/core/src/main/java/org/jclouds/util/Multimaps2.java @@ -22,7 +22,9 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Map; import java.util.Set; +import java.util.Map.Entry; +import com.google.common.base.Function; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; @@ -89,4 +91,28 @@ public class Multimaps2 { public static Multimap withoutKeys(Multimap fromMultimap, Set keys) { return Multimaps. filterKeys(fromMultimap, Predicates.not(Predicates.in(keys))); } + + /** + * change the keys but keep the values in-tact. + * + * @param + * input key type + * @param + * output key type + * @param + * value type + * @param in + * input map to transform + * @param fn + * how to transform the values + * @return immutableMap with the new keys. + */ + public static Multimap transformKeys(Multimap in, Function fn) { + checkNotNull(in, "input map"); + checkNotNull(fn, "function"); + Builder returnVal = ImmutableMultimap.builder(); + for (Entry entry : in.entries()) + returnVal.put(fn.apply(entry.getKey()), entry.getValue()); + return returnVal.build(); + } } diff --git a/core/src/test/java/org/jclouds/util/Multimaps2Test.java b/core/src/test/java/org/jclouds/util/Multimaps2Test.java new file mode 100644 index 0000000000..101e3609d2 --- /dev/null +++ b/core/src/test/java/org/jclouds/util/Multimaps2Test.java @@ -0,0 +1,41 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you 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 org.jclouds.util; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.functions.ToLowerCase; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; + +/** + * @author Everett Toews + */ +@Test(groups = "unit") +public class Multimaps2Test { + public void testTransformKeysToLowerCase() { + Multimap map = ImmutableMultimap.of("oNe", "1", "TWO", "2", "three", "3", "Three", "3.0"); + Multimap expected = ImmutableMultimap.of("one", "1", "two", "2", "three", "3", "three", "3.0"); + Multimap transformed = Multimaps2.transformKeys(map, new ToLowerCase()); + + assertEquals(transformed, expected); + } +}