@Override protected List<String> create(String[] elements) { List<String> fromList = Lists.newArrayList(elements); return Lists.transform(fromList, Functions.<String>identity()); } })
public void testTransformEmptyMapEquality() { Map<String, String> map = Maps.transformValues(ImmutableMap.<String, Integer>of(), Functions.toStringFunction()); assertMapsEqual(Maps.newHashMap(), map); }
/** Null values are not allowed. */ public void testUniqueIndexNullValue() { List<String> listWithNull = Lists.newArrayList((String) null); try { Maps.uniqueIndex(listWithNull, Functions.constant(1)); fail(); } catch (NullPointerException expected) { } }
public void testCompositionWildcard() { Map<String, Integer> mapJapaneseToInteger = Maps.newHashMap(); Function<String, Integer> japaneseToInteger = Functions.forMap(mapJapaneseToInteger); Function<Object, String> numberToSpanish = Functions.constant("Yo no se"); Function<String, String> japaneseToSpanish = Functions.compose(numberToSpanish, japaneseToInteger); }
public void testComposeOfFunctionsIsAssociative() { Map<Float, String> m = ImmutableMap.of(4.0f, "A", 3.0f, "B", 2.0f, "C", 1.0f, "D"); Function<? super Integer, Boolean> h = Functions.constant(Boolean.TRUE); Function<? super String, Integer> g = new HashCodeFunction(); Function<Float, String> f = Functions.forMap(m, "F"); Function<Float, Boolean> c1 = Functions.compose(Functions.compose(h, g), f); Function<Float, Boolean> c2 = Functions.compose(h, Functions.compose(g, f)); // Might be nice (eventually) to have: // assertEquals(c1, c2); // But for now, settle for this: assertEquals(c1.hashCode(), c2.hashCode()); assertEquals(c1.apply(1.0f), c2.apply(1.0f)); assertEquals(c1.apply(5.0f), c2.apply(5.0f)); }
public static final class Literal implements Function<Object, String> { @Override public String apply(Object input) { return StaticEnvironment.processing().getElementUtils().getConstantExpression(input); } public final Function<Object, String> string = new Function<Object, String>() { @Override public String apply(Object input) { return StringLiterals.toLiteral(String.valueOf(input)); } }; public final Function<Number, String> hex = new Function<Number, String>() { @Override public String apply(Number input) { if (input instanceof Long) { return "0x" + Long.toHexString(input.longValue()) + "L"; } if (input instanceof Integer) { return "0x" + Integer.toHexString(input.intValue()); } throw new IllegalArgumentException("unsupported non integer or long " + input); } @Override public String toString() { return Literal.this.toString() + ".hex"; }
public void testNullOnUnmatchedRegion() { LocationFromBucketName fn = new LocationFromBucketName(Functions.forMap(ImmutableMap .<String, Optional<String>> of("mybucket", Optional.of("eu-west-2"))), Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(region, region2))); assertEquals(fn.apply("mybucket"), null); }
@Test(expectedExceptions = IllegalStateException.class) public void testWithoutInitThrowsIllegalStateException() { Statement command = exec("doFoo"); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).credentials( LoginCredentials.builder().user("tester").password("notalot").build()).build(); SshClient sshClient = createMock(SshClient.class); replay(sshClient); RunScriptOnNodeAsInitScriptUsingSsh testMe = new RunScriptOnNodeAsInitScriptUsingSsh(Functions .forMap(ImmutableMap.of(node, sshClient)), eventBus, InitScriptConfigurationForTasks.create() .appendIncrementingNumberToAnonymousTaskNames(), node, command, new RunScriptOptions()); testMe.call(); }
public void testGetPublicIPAddress() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(AddressAsyncClient.class, "getPublicIPAddress", String.class); GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of(5)); assertRequestLineEquals(httpRequest, "GET http://localhost:8080/client/api?response=json&command=listPublicIpAddresses&listAll=true&id=5 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertPayloadEquals(httpRequest, null, null, false); assertResponseParserClassEquals(method, httpRequest, Functions.compose(IdentityFunction.INSTANCE, IdentityFunction.INSTANCE).getClass()); assertSaxResponseParserClassEquals(method, null); assertFallbackClassEquals(method, NullOnNotFoundOr404.class); checkFilters(httpRequest); }
public void testDefault() { Statement command = exec("doFoo"); NodeMetadata node = new NodeMetadataBuilder().ids("id").status(Status.RUNNING).credentials( LoginCredentials.builder().user("tester").password("notalot").build()).build(); SshClient sshClient = createMock(SshClient.class); expect(sshClient.getUsername()).andReturn("tester").atLeastOnce(); expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce(); .forMap(ImmutableMap.of(node, sshClient)), eventBus, InitScriptConfigurationForTasks.create() .appendIncrementingNumberToAnonymousTaskNames(), node, command, new RunScriptOptions()); assertEquals(testMe.getInitFile(), "/tmp/init-jclouds-script-0"); assertEquals(testMe.getNode(), node); assertEquals(testMe.getStatement(), init);
public void testBulkGetReturnsSorted() { for (Striped<?> striped : allImplementations()) { Map<Object, Integer> indexByLock = Maps.newHashMap(); for (int i = 0; i < striped.size(); i++) { indexByLock.put(striped.getAt(i), i); } // ensure that bulkGet returns locks in monotonically increasing order for (int objectsNum = 1; objectsNum <= striped.size() * 2; objectsNum++) { Set<Object> objects = Sets.newHashSetWithExpectedSize(objectsNum); for (int i = 0; i < objectsNum; i++) { objects.add(new Object()); } Iterable<?> locks = striped.bulkGet(objects); assertTrue(Ordering.natural().onResultOf(Functions.forMap(indexByLock)).isOrdered(locks)); // check idempotency Iterable<?> locks2 = striped.bulkGet(objects); assertEquals(Lists.newArrayList(locks), Lists.newArrayList(locks2)); } } }
@Test public void testIdentityCast() { TypeRegistry typeManager = new TypeRegistry(); FunctionRegistry registry = new FunctionRegistry(typeManager, new BlockEncodingManager(typeManager), new FeaturesConfig()); Signature exactOperator = registry.getCoercion(HYPER_LOG_LOG, HYPER_LOG_LOG); assertEquals(exactOperator.getName(), mangleOperatorName(OperatorType.CAST.name())); assertEquals(transform(exactOperator.getArgumentTypes(), Functions.toStringFunction()), ImmutableList.of(StandardTypes.HYPER_LOG_LOG)); assertEquals(exactOperator.getReturnType().getBase(), StandardTypes.HYPER_LOG_LOG); }
@Override protected Map<String, String> makePopulatedMap() { Map<String, Integer> underlying = Maps.newHashMap(); underlying.put("a", 1); underlying.put("b", 2); underlying.put("c", 3); return Maps.transformValues(underlying, Functions.toStringFunction()); }
Bucket.Cors toPb() { Bucket.Cors pb = new Bucket.Cors(); pb.setMaxAgeSeconds(maxAgeSeconds); pb.setResponseHeader(responseHeaders); if (methods != null) { pb.setMethod(newArrayList(transform(methods, Functions.toStringFunction()))); } if (origins != null) { pb.setOrigin(newArrayList(transform(origins, Functions.toStringFunction()))); } return pb; }
List<Symbol> lookupSymbolSchema = ImmutableList.copyOf(node.getLookupSymbols()); ImmutableList.Builder<Integer> remappedProbeKeyChannelsBuilder = ImmutableList.builder(); ImmutableList.Builder<Set<Integer>> overlappingFieldSetsBuilder = ImmutableList.builder(); for (Symbol lookupSymbol : lookupSymbolSchema) { Set<Integer> potentialProbeInputs = indexLookupToProbeInput.get(lookupSymbol); List<ColumnHandle> lookupSchema = Lists.transform(lookupSymbolSchema, forMap(node.getAssignments())); List<ColumnHandle> outputSchema = Lists.transform(node.getOutputSymbols(), forMap(node.getAssignments())); ConnectorIndex index = indexManager.getIndex(session, node.getIndexHandle(), lookupSchema, outputSchema);
Multimaps.invertFrom(typesByAttribute, HashMultimap.<String, String>create()); Map<String, ValueAttribute> attributeMap = Maps.newHashMapWithExpectedSize(attributes.size()); Function<String, ValueAttribute> getAttribute = Functions.forMap(attributeMap); ImmutableList.Builder<FromSupertypesModel.FromSupertype> builder = ImmutableList.builder(); builder.add(new FromSupertype(abstractTypeName, ImmutableList.<ValueAttribute>of())); ImmutableList.Builder<String> repeatingBuilder = ImmutableList.builder(); for (Entry<String, Collection<String>> e : typesByAttribute.asMap().entrySet()) { if (e.getValue().size() > 1) {
@Test public void testWhenNotFoundCreatesANewSecurityGroup() throws Exception { SecurityGroup securityGroup = createMock(SecurityGroup.class); RegionSecurityGroupNameAndPorts input = new RegionSecurityGroupNameAndPorts("region", "groupName", ImmutableSet .<Integer> of(22, 8080)); Function<RegionSecurityGroupNameAndPorts, SecurityGroup> groupCreator = Functions.forMap(ImmutableMap .of(input, securityGroup)); FindSecurityGroupOrCreate parser = new FindSecurityGroupOrCreate(groupCreator); assertEquals(parser.load(input), securityGroup); }
return new ExecutionFailureInfo(type, "[cyclic] " + throwable.getMessage(), null, ImmutableList.of(), ImmutableList.of(), null, GENERIC_INTERNAL_ERROR.toErrorCode(), remoteHost); Arrays.stream(throwable.getSuppressed()) .map(failure -> toFailure(failure, seenFailures)) .collect(toImmutableList()), Lists.transform(asList(throwable.getStackTrace()), toStringFunction()), getErrorLocation(throwable), errorCode,