Introduce Predicates2.startsWith and endsWith

This commit is contained in:
Andrew Gaul 2012-07-24 21:38:59 -07:00 committed by Andrew Gaul
parent 3128eb25fc
commit 3648863a3c
8 changed files with 82 additions and 83 deletions

View File

@ -24,6 +24,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.crypto.CryptoStreams; import org.jclouds.crypto.CryptoStreams;
import org.jclouds.util.Predicates2;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -38,35 +39,16 @@ import com.google.common.base.Predicates;
*/ */
@Singleton @Singleton
public class EncodedRSAPublicKeyToBase64 implements Function<Object, String> { public class EncodedRSAPublicKeyToBase64 implements Function<Object, String> {
private static Predicate<Object> startsWith(String value) { private static final Predicate<String> ALLOWED_MARKERS = Predicates.or(
return new ToStringStartsWith(value); Predicates2.startsWith("ssh-rsa"),
} Predicates2.startsWith("-----BEGIN CERTIFICATE-----"),
Predicates2.startsWith("---- BEGIN SSH2 PUBLIC KEY ----"));
private static final class ToStringStartsWith implements Predicate<Object> {
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<Object> ALLOWED_MARKERS = Predicates.or(startsWith("ssh-rsa"),
startsWith("-----BEGIN CERTIFICATE-----"), startsWith("---- BEGIN SSH2 PUBLIC KEY ----"));
@Override @Override
public String apply(Object from) { public String apply(Object from) {
checkNotNull(from, "input"); checkNotNull(from, "input");
checkArgument(ALLOWED_MARKERS.apply(from), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS); String fromString = from.toString();
return CryptoStreams.base64(from.toString().getBytes(Charsets.UTF_8)); checkArgument(ALLOWED_MARKERS.apply(fromString), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS);
return CryptoStreams.base64(fromString.getBytes(Charsets.UTF_8));
} }
} }

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set; import java.util.Set;
import org.jclouds.http.options.BaseHttpRequestOptions; import org.jclouds.http.options.BaseHttpRequestOptions;
import org.jclouds.util.Predicates2;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -52,13 +53,7 @@ public class BaseEC2RequestOptions extends BaseHttpRequestOptions {
protected Set<String> getFormValuesWithKeysPrefixedBy(final String prefix) { protected Set<String> getFormValuesWithKeysPrefixedBy(final String prefix) {
Builder<String> values = ImmutableSet.builder(); Builder<String> values = ImmutableSet.builder();
for (String key : Iterables.filter(formParameters.keySet(), new Predicate<String>() { for (String key : Iterables.filter(formParameters.keySet(), Predicates2.startsWith(prefix))) {
public boolean apply(String input) {
return input.startsWith(prefix);
}
})) {
values.add(Iterables.get(formParameters.get(key), 0)); values.add(Iterables.get(formParameters.get(key), 0));
} }
return values.build(); return values.build();

View File

@ -31,9 +31,11 @@ import javax.inject.Singleton;
import org.jclouds.location.Iso3166; import org.jclouds.location.Iso3166;
import org.jclouds.location.suppliers.LocationIdToIso3166CodesSupplier; import org.jclouds.location.suppliers.LocationIdToIso3166CodesSupplier;
import org.jclouds.util.Predicates2;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
@ -63,14 +65,9 @@ public class LocationIdToIso3166CodesFromConfiguration implements LocationIdToIs
@Iso3166 @Iso3166
@Override @Override
public Map<String, Supplier<Set<String>>> get() { public Map<String, Supplier<Set<String>>> get() {
Map<String, String> stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(new Predicate<String>() { Map<String, String> stringsBoundWithRegionOrZonePrefix = filterStringsBoundByName.apply(Predicates.<String>or(
Predicates2.startsWith(PROPERTY_REGION),
@Override Predicates2.startsWith(PROPERTY_ZONE)));
public boolean apply(String input) {
return (input.startsWith(PROPERTY_REGION) || input.startsWith(PROPERTY_ZONE));
}
});
Builder<String, Supplier<Set<String>>> codes = ImmutableMap.builder(); Builder<String, Supplier<Set<String>>> codes = ImmutableMap.builder();
for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE)) { for (String key : ImmutableSet.of(PROPERTY_REGION, PROPERTY_ZONE)) {
String regionOrZoneString = stringsBoundWithRegionOrZonePrefix.get(key + "s"); String regionOrZoneString = stringsBoundWithRegionOrZonePrefix.get(key + "s");

View File

@ -48,6 +48,7 @@ import org.jclouds.rest.internal.AsyncRestClientProxy;
import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.rest.internal.SeedAnnotationCache; import org.jclouds.rest.internal.SeedAnnotationCache;
import org.jclouds.util.Maps2; import org.jclouds.util.Maps2;
import org.jclouds.util.Predicates2;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -111,15 +112,7 @@ public class RestModule extends AbstractModule {
@Singleton @Singleton
@Named("TIMEOUTS") @Named("TIMEOUTS")
protected Map<String, Long> timeouts(Function<Predicate<String>, Map<String, String>> filterStringsBoundByName) { protected Map<String, Long> timeouts(Function<Predicate<String>, Map<String, String>> filterStringsBoundByName) {
Map<String, String> stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(new Predicate<String>() { Map<String, String> stringBoundWithTimeoutPrefix = filterStringsBoundByName.apply(Predicates2.startsWith(PROPERTY_TIMEOUTS_PREFIX));
@Override
public boolean apply(String input) {
return input.startsWith(PROPERTY_TIMEOUTS_PREFIX);
}
});
Map<String, Long> longsByName = Maps.transformValues(stringBoundWithTimeoutPrefix, new Function<String, Long>() { Map<String, Long> longsByName = Maps.transformValues(stringBoundWithTimeoutPrefix, new Function<String, Long>() {
@Override @Override

View File

@ -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<String> startsWith(final String prefix) {
return new Predicate<String>() {
@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<String> endsWith(final String suffix) {
return new Predicate<String>() {
@Override
public boolean apply(final String input) {
return input.endsWith(suffix);
}
@Override
public String toString() {
return "endsWith(" + suffix + ")";
}
};
}
}

View File

@ -51,10 +51,12 @@ import org.jclouds.location.Provider;
import org.jclouds.nodepool.Backend; import org.jclouds.nodepool.Backend;
import org.jclouds.rest.annotations.ApiVersion; import org.jclouds.rest.annotations.ApiVersion;
import org.jclouds.rest.annotations.BuildVersion; import org.jclouds.rest.annotations.BuildVersion;
import org.jclouds.util.Predicates2;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -96,14 +98,9 @@ public class BindBackendComputeService extends BindJcloudsModules {
}); });
} }
private static final Predicate<String> keys = new Predicate<String>() { private static final Predicate<String> keys = Predicates.<String>and(
Predicates.not(Predicates2.startsWith("jclouds.nodepool")),
@Override Predicates.not(Predicates2.startsWith("nodepool")));
public boolean apply(String input) {
return !input.startsWith("jclouds.nodepool") && !input.startsWith("nodepool");
}
};
@Provides @Provides
@Singleton @Singleton

View File

@ -39,9 +39,11 @@ import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.slicehost.domain.Slice; import org.jclouds.slicehost.domain.Slice;
import org.jclouds.util.Predicates2;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -109,22 +111,8 @@ public class SliceToNodeMetadata implements Function<Slice, NodeMetadata> {
builder.operatingSystem(parseOperatingSystem(from)); builder.operatingSystem(parseOperatingSystem(from));
builder.hardware(parseHardware(from)); builder.hardware(parseHardware(from));
builder.status(sliceToNodeStatus.get(from.getStatus())); builder.status(sliceToNodeStatus.get(from.getStatus()));
builder.publicAddresses(Iterables.filter(from.getAddresses(), new Predicate<String>() { builder.publicAddresses(Iterables.filter(from.getAddresses(), Predicates.not(Predicates2.startsWith("10."))));
builder.privateAddresses(Iterables.filter(from.getAddresses(), Predicates2.startsWith("10.")));
@Override
public boolean apply(String input) {
return !input.startsWith("10.");
}
}));
builder.privateAddresses(Iterables.filter(from.getAddresses(), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("10.");
}
}));
return builder.build(); return builder.build();
} }

View File

@ -39,11 +39,13 @@ import org.jclouds.slicehost.domain.Image;
import org.jclouds.slicehost.domain.Slice; import org.jclouds.slicehost.domain.Slice;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.SshException; import org.jclouds.ssh.SshException;
import org.jclouds.util.Predicates2;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import com.google.inject.Injector; import com.google.inject.Injector;
@ -268,15 +270,7 @@ public class SlicehostClientLiveTest extends BaseComputeServiceContextLiveTest {
} }
private String getIp(Slice newDetails) { private String getIp(Slice newDetails) {
String ip = Iterables.find(newDetails.getAddresses(), new Predicate<String>() { return Iterables.find(newDetails.getAddresses(), Predicates.not(Predicates2.startsWith("10.")));
@Override
public boolean apply(String input) {
return !input.startsWith("10.");
}
});
return ip;
} }
@Test(enabled = true, timeOut = 10 * 60 * 1000, dependsOnMethods = "testSliceDetails") @Test(enabled = true, timeOut = 10 * 60 * 1000, dependsOnMethods = "testSliceDetails")