From dbf59adce139e525841b451abe03cb7a1734c00a Mon Sep 17 00:00:00 2001 From: danikov Date: Wed, 18 Apr 2012 16:59:01 +0100 Subject: [PATCH] bugfix for self-referential loop + test --- .../org/jclouds/util/ConcreteFunction.java | 45 +++++++++++++++++++ .../test/java/org/jclouds/util/Maps2Test.java | 15 +++++++ 2 files changed, 60 insertions(+) create mode 100644 core/src/main/java/org/jclouds/util/ConcreteFunction.java diff --git a/core/src/main/java/org/jclouds/util/ConcreteFunction.java b/core/src/main/java/org/jclouds/util/ConcreteFunction.java new file mode 100644 index 0000000000..27ac98c67d --- /dev/null +++ b/core/src/main/java/org/jclouds/util/ConcreteFunction.java @@ -0,0 +1,45 @@ +/** + * 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 com.google.common.base.Function; + + +/** + * For wrapping covariant functions for passing to non-covariant methods + * + * @author danikov + */ +public class ConcreteFunction implements Function { + private final Function delegate; + + public static ConcreteFunction wrap(Function delegate) { + return new ConcreteFunction(delegate); + } + + public ConcreteFunction(Function delegate) { + this.delegate = delegate; + } + + @Override + public T apply(F input) { + return delegate.apply(input); + } + +} diff --git a/core/src/test/java/org/jclouds/util/Maps2Test.java b/core/src/test/java/org/jclouds/util/Maps2Test.java index d0f3cbf1fd..8c52536fa7 100644 --- a/core/src/test/java/org/jclouds/util/Maps2Test.java +++ b/core/src/test/java/org/jclouds/util/Maps2Test.java @@ -31,6 +31,7 @@ import org.testng.annotations.Test; import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; /** @@ -86,4 +87,18 @@ public class Maps2Test { }), expected); } + @Test + public void testCovariantUniqueIndex() { + Iterable values = Lists.newArrayList(1, 2, 3, 4, 5); + Map map = Maps2.uniqueIndex(values, new Function() { + + @Override + public Double apply(Object input) { + return (Integer)input + 0.1; + } + }); + + assertEquals(map.get(1.1), 1); + } + }