diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/functions/EncodedRSAPublicKeyToBase64.java b/apis/ec2/src/main/java/org/jclouds/ec2/functions/EncodedRSAPublicKeyToBase64.java index 0d877d6343..a9ebf45dd0 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/functions/EncodedRSAPublicKeyToBase64.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/functions/EncodedRSAPublicKeyToBase64.java @@ -24,6 +24,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import javax.inject.Singleton; import org.jclouds.crypto.CryptoStreams; +import org.jclouds.util.Predicates2; import com.google.common.base.Charsets; import com.google.common.base.Function; @@ -38,35 +39,16 @@ import com.google.common.base.Predicates; */ @Singleton public class EncodedRSAPublicKeyToBase64 implements Function { - private static Predicate startsWith(String value) { - return new ToStringStartsWith(value); - } - - private static final class ToStringStartsWith implements Predicate { - private final String value; - - private ToStringStartsWith(String value) { - this.value = value; - } - - @Override - public boolean apply(Object input) { - return input.toString().startsWith(value); - } - - public String toString() { - return "toStringStartsWith(" + value + ")"; - } - } - - @SuppressWarnings("unchecked") - private static final Predicate ALLOWED_MARKERS = Predicates.or(startsWith("ssh-rsa"), - startsWith("-----BEGIN CERTIFICATE-----"), startsWith("---- BEGIN SSH2 PUBLIC KEY ----")); + private static final Predicate ALLOWED_MARKERS = Predicates.or( + Predicates2.startsWith("ssh-rsa"), + Predicates2.startsWith("-----BEGIN CERTIFICATE-----"), + Predicates2.startsWith("---- BEGIN SSH2 PUBLIC KEY ----")); @Override public String apply(Object from) { checkNotNull(from, "input"); - checkArgument(ALLOWED_MARKERS.apply(from), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS); - return CryptoStreams.base64(from.toString().getBytes(Charsets.UTF_8)); + String fromString = from.toString(); + checkArgument(ALLOWED_MARKERS.apply(fromString), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS); + return CryptoStreams.base64(fromString.getBytes(Charsets.UTF_8)); } } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/options/internal/BaseEC2RequestOptions.java b/apis/ec2/src/main/java/org/jclouds/ec2/options/internal/BaseEC2RequestOptions.java index cbb336a444..72a6c21503 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/options/internal/BaseEC2RequestOptions.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/options/internal/BaseEC2RequestOptions.java @@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Set; import org.jclouds.http.options.BaseHttpRequestOptions; +import org.jclouds.util.Predicates2; import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; @@ -52,13 +53,7 @@ public class BaseEC2RequestOptions extends BaseHttpRequestOptions { protected Set getFormValuesWithKeysPrefixedBy(final String prefix) { Builder values = ImmutableSet.builder(); - for (String key : Iterables.filter(formParameters.keySet(), new Predicate() { - - public boolean apply(String input) { - return input.startsWith(prefix); - } - - })) { + for (String key : Iterables.filter(formParameters.keySet(), Predicates2.startsWith(prefix))) { values.add(Iterables.get(formParameters.get(key), 0)); } return values.build(); diff --git a/core/src/main/java/org/jclouds/location/suppliers/fromconfig/LocationIdToIso3166CodesFromConfiguration.java b/core/src/main/java/org/jclouds/location/suppliers/fromconfig/LocationIdToIso3166CodesFromConfiguration.java index 5eb0a2c1f9..67125202c1 100644 --- a/core/src/main/java/org/jclouds/location/suppliers/fromconfig/LocationIdToIso3166CodesFromConfiguration.java +++ b/core/src/main/java/org/jclouds/location/suppliers/fromconfig/LocationIdToIso3166CodesFromConfiguration.java @@ -31,9 +31,11 @@ import javax.inject.Singleton; import org.jclouds.location.Iso3166; import org.jclouds.location.suppliers.LocationIdToIso3166CodesSupplier; +import org.jclouds.util.Predicates2; import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.base.Splitter; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; @@ -63,14 +65,9 @@ public class LocationIdToIso3166CodesFromConfiguration implements LocationIdToIs @Iso3166 @Override public Map>> get() { - Map stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(new Predicate() { - - @Override - public boolean apply(String input) { - return (input.startsWith(PROPERTY_REGION) || input.startsWith(PROPERTY_ZONE)); - } - - }); + Map stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(Predicates.or( + Predicates2.startsWith(PROPERTY_REGION), + Predicates2.startsWith(PROPERTY_ZONE))); Builder>> codes = ImmutableMap.builder(); for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE)) { String regionOrZoneString = stringsBoundWithRegionOrZonePrefix.get(key + "s"); diff --git a/core/src/main/java/org/jclouds/rest/config/RestModule.java b/core/src/main/java/org/jclouds/rest/config/RestModule.java index 4b5e19f829..99e12558b8 100644 --- a/core/src/main/java/org/jclouds/rest/config/RestModule.java +++ b/core/src/main/java/org/jclouds/rest/config/RestModule.java @@ -48,6 +48,7 @@ import org.jclouds.rest.internal.AsyncRestClientProxy; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.SeedAnnotationCache; import org.jclouds.util.Maps2; +import org.jclouds.util.Predicates2; import com.google.common.base.Function; import com.google.common.base.Predicate; @@ -111,15 +112,7 @@ public class RestModule extends AbstractModule { @Singleton @Named("TIMEOUTS") protected Map timeouts(Function, Map> filterStringsBoundByName) { - Map stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(new Predicate() { - - @Override - public boolean apply(String input) { - return input.startsWith(PROPERTY_TIMEOUTS_PREFIX); - } - - }); - + Map stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(Predicates2.startsWith(PROPERTY_TIMEOUTS_PREFIX)); Map longsByName = Maps.transformValues(stringBoundWithTimeoutPrefix, new Function() { @Override diff --git a/core/src/main/java/org/jclouds/util/Predicates2.java b/core/src/main/java/org/jclouds/util/Predicates2.java new file mode 100644 index 0000000000..9872e4b1e0 --- /dev/null +++ b/core/src/main/java/org/jclouds/util/Predicates2.java @@ -0,0 +1,53 @@ +/** + * 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.Predicate; + +public class Predicates2 { + /** Returns a predicate that evaluates to true if the String being tested starts with a prefix. */ + public static Predicate startsWith(final String prefix) { + return new Predicate() { + @Override + public boolean apply(final String input) { + return input.startsWith(prefix); + } + + @Override + public String toString() { + return "startsWith(" + prefix + ")"; + } + }; + } + + /** Returns a predicate that evaluates to true if the String being tested ends with a prefix. */ + public static Predicate endsWith(final String suffix) { + return new Predicate() { + @Override + public boolean apply(final String input) { + return input.endsWith(suffix); + } + + @Override + public String toString() { + return "endsWith(" + suffix + ")"; + } + }; + } +} diff --git a/labs/nodepool/src/main/java/org/jclouds/nodepool/config/BindBackendComputeService.java b/labs/nodepool/src/main/java/org/jclouds/nodepool/config/BindBackendComputeService.java index 3832eb0ef3..ff9205a924 100644 --- a/labs/nodepool/src/main/java/org/jclouds/nodepool/config/BindBackendComputeService.java +++ b/labs/nodepool/src/main/java/org/jclouds/nodepool/config/BindBackendComputeService.java @@ -51,10 +51,12 @@ import org.jclouds.location.Provider; import org.jclouds.nodepool.Backend; import org.jclouds.rest.annotations.ApiVersion; import org.jclouds.rest.annotations.BuildVersion; +import org.jclouds.util.Predicates2; import com.google.common.annotations.Beta; import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.base.Supplier; import com.google.common.base.Suppliers; import com.google.common.collect.Iterables; @@ -96,14 +98,9 @@ public class BindBackendComputeService extends BindJcloudsModules { }); } - private static final Predicate keys = new Predicate() { - - @Override - public boolean apply(String input) { - return !input.startsWith("jclouds.nodepool") && !input.startsWith("nodepool"); - } - - }; + private static final Predicate keys = Predicates.and( + Predicates.not(Predicates2.startsWith("jclouds.nodepool")), + Predicates.not(Predicates2.startsWith("nodepool"))); @Provides @Singleton diff --git a/providers/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java b/providers/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java index 2c106e2cc5..5e1c260197 100644 --- a/providers/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java +++ b/providers/slicehost/src/main/java/org/jclouds/slicehost/compute/functions/SliceToNodeMetadata.java @@ -39,9 +39,11 @@ import org.jclouds.compute.functions.GroupNamingConvention; import org.jclouds.domain.Location; import org.jclouds.logging.Logger; import org.jclouds.slicehost.domain.Slice; +import org.jclouds.util.Predicates2; import com.google.common.base.Function; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.base.Supplier; import com.google.common.collect.Iterables; @@ -109,22 +111,8 @@ public class SliceToNodeMetadata implements Function { builder.operatingSystem(parseOperatingSystem(from)); builder.hardware(parseHardware(from)); builder.status(sliceToNodeStatus.get(from.getStatus())); - builder.publicAddresses(Iterables.filter(from.getAddresses(), new Predicate() { - - @Override - public boolean apply(String input) { - return !input.startsWith("10."); - } - - })); - builder.privateAddresses(Iterables.filter(from.getAddresses(), new Predicate() { - - @Override - public boolean apply(String input) { - return input.startsWith("10."); - } - - })); + builder.publicAddresses(Iterables.filter(from.getAddresses(), Predicates.not(Predicates2.startsWith("10.")))); + builder.privateAddresses(Iterables.filter(from.getAddresses(), Predicates2.startsWith("10."))); return builder.build(); } diff --git a/providers/slicehost/src/test/java/org/jclouds/slicehost/SlicehostClientLiveTest.java b/providers/slicehost/src/test/java/org/jclouds/slicehost/SlicehostClientLiveTest.java index f82c1e2416..b1db169031 100644 --- a/providers/slicehost/src/test/java/org/jclouds/slicehost/SlicehostClientLiveTest.java +++ b/providers/slicehost/src/test/java/org/jclouds/slicehost/SlicehostClientLiveTest.java @@ -39,11 +39,13 @@ import org.jclouds.slicehost.domain.Image; import org.jclouds.slicehost.domain.Slice; import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshException; +import org.jclouds.util.Predicates2; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import com.google.common.base.Predicate; +import com.google.common.base.Predicates; import com.google.common.collect.Iterables; import com.google.common.net.HostAndPort; import com.google.inject.Injector; @@ -268,15 +270,7 @@ public class SlicehostClientLiveTest extends BaseComputeServiceContextLiveTest { } private String getIp(Slice newDetails) { - String ip = Iterables.find(newDetails.getAddresses(), new Predicate() { - - @Override - public boolean apply(String input) { - return !input.startsWith("10."); - } - - }); - return ip; + return Iterables.find(newDetails.getAddresses(), Predicates.not(Predicates2.startsWith("10."))); } @Test(enabled = true, timeOut = 10 * 60 * 1000, dependsOnMethods = "testSliceDetails")