List of usage examples for com.google.common.collect ImmutableMap builder
public static <K, V> Builder<K, V> builder()
From source file:com.netflix.scheduledactions.web.controllers.ValidationController.java
@RequestMapping(value = "/validateCronExpression", method = RequestMethod.GET) @ResponseStatus(value = HttpStatus.OK)/*from w ww . ja v a2 s. co m*/ public Map<String, Object> validateCronExpression(@RequestParam String cronExpression) { ImmutableMap.Builder<String, Object> mapBuilder = ImmutableMap.builder(); try { TriggerUtils.validateCronExpression(cronExpression); mapBuilder.put("response", "Cron expression is valid"); try { Options options = new Options(); options.setZeroBasedDayOfWeek(false); mapBuilder.put("description", CronExpressionDescriptor.getDescription(cronExpression, options)); } catch (ParseException IGNORED) { mapBuilder.put("description", "No description available"); } return mapBuilder.build(); } catch (IllegalArgumentException e) { throw new InvalidCronExpressionException( String.format("Cron expression '%s' is not valid: %s", cronExpression, e.getMessage())); } }
From source file:org.jclouds.googlecomputeengine.compute.functions.BuildInstanceMetadata.java
@Override public ImmutableMap.Builder apply(TemplateOptions input) { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); if (input.getPublicKey() != null) { builder.put("sshKeys", format("%s:%s %s@localhost", checkNotNull(input.getLoginUser(), "loginUser cannot be null"), input.getPublicKey(), input.getLoginUser())); }//from w w w .j a v a 2 s . c o m builder.putAll(input.getUserMetadata()); return builder; }
From source file:org.prebake.service.plan.PlanGraph.java
public static Builder builder(Product... nodes) { ImmutableMap.Builder<BoundName, Product> products = ImmutableMap.builder(); for (Product p : nodes) { products.put(p.name, p);/*www . j a v a 2 s . c om*/ } return new Builder(products.build()); }
From source file:org.openqa.selenium.server.htmlrunner.ReflectivelyDiscoveredSteps.java
private static ImmutableMap<String, CoreStepFactory> discover() { ImmutableMap.Builder<String, CoreStepFactory> factories = ImmutableMap.builder(); Set<String> seenNames = new HashSet<>(); // seed the seen names with methods we definitely don't want folks accessing seenNames.add("addCustomRequestHeader"); seenNames.add("allowNativeXpath"); seenNames.add("pause"); seenNames.add("rollup"); seenNames.add("setBrowserLogLevel"); seenNames.add("setExtensionJs"); seenNames.add("start"); seenNames.add("stop"); for (final Method method : Selenium.class.getMethods()) { if (!seenNames.add(method.getName())) { continue; }//from w w w . ja v a 2s. c o m if (method.getParameterCount() > 2) { continue; } CoreStepFactory factory = ((locator, value) -> (selenium, state) -> invokeMethod(method, selenium, locator, value, (result -> NextStepDecorator.IDENTITY))); factories.put(method.getName(), factory); // Methods of the form getFoo(target) result in commands: // getFoo, assertFoo, verifyFoo, assertNotFoo, verifyNotFoo // storeFoo, waitForFoo, and waitForNotFoo. final String shortName; boolean isAccessor; if (method.getName().startsWith("get")) { shortName = method.getName().substring("get".length()); isAccessor = true; } else if (method.getName().startsWith("is")) { shortName = method.getName().substring("is".length()); isAccessor = true; } else { shortName = null; isAccessor = false; } if (shortName != null) { String negatedName = negateName(shortName); factories.put("assert" + shortName, (loc, val) -> (selenium, state) -> invokeMethod(method, selenium, state.expand(loc), state.expand(val), (seen -> { Object expected = getExpectedValue(method, state.expand(loc), state.expand(val)); try { SeleneseTestBase.assertEquals(expected, seen); } catch (AssertionError e) { return NextStepDecorator.ASSERTION_FAILED(e.getMessage()); } return NextStepDecorator.IDENTITY; }))); factories.put("assert" + negatedName, (loc, val) -> (selenium, state) -> invokeMethod(method, selenium, state.expand(loc), state.expand(val), (seen) -> { Object expected = getExpectedValue(method, state.expand(loc), state.expand(val)); try { SeleneseTestBase.assertNotEquals(expected, seen); return NextStepDecorator.IDENTITY; } catch (AssertionError e) { return NextStepDecorator.ASSERTION_FAILED(e.getMessage()); } })); factories.put("verify" + shortName, (loc, val) -> (selenium, state) -> invokeMethod(method, selenium, state.expand(loc), state.expand(val), (seen) -> { Object expected = getExpectedValue(method, state.expand(loc), state.expand(val)); try { SeleneseTestBase.assertEquals(expected, seen); return NextStepDecorator.IDENTITY; } catch (AssertionError e) { return NextStepDecorator.VERIFICATION_FAILED(e.getMessage()); } })); factories.put("verify" + negatedName, (loc, val) -> (selenium, state) -> invokeMethod(method, selenium, state.expand(loc), state.expand(val), (seen) -> { Object expected = getExpectedValue(method, state.expand(loc), state.expand(val)); try { SeleneseTestBase.assertNotEquals(expected, seen); return NextStepDecorator.IDENTITY; } catch (AssertionError e) { return NextStepDecorator.VERIFICATION_FAILED(e.getMessage()); } })); } if (isAccessor) { factories.put("store" + shortName, (loc, val) -> (selenium, state) -> invokeMethod(method, selenium, state.expand(loc), state.expand(val), (toStore) -> { state.store(state.expand(loc), toStore); return NextStepDecorator.IDENTITY; })); factories.put("waitFor" + shortName, (loc, val) -> (selenium, state) -> { String[] args = buildArgs(method, state.expand(loc), state.expand(val)); try { new Wait() { @Override public boolean until() { try { Object result = method.invoke(selenium, args); if (Boolean.class.isAssignableFrom(result.getClass())) { return (Boolean) result; } return true; } catch (IllegalAccessException e) { // It's going to be interesting to see this be thrown throw new RuntimeException(e); } catch (InvocationTargetException e) { return false; } } }.wait("Can't wait for " + shortName); } catch (Wait.WaitTimedOutException e) { return NextStepDecorator.ERROR(e); } return NextStepDecorator.IDENTITY; }); factories.put("waitFor" + negateName(shortName), (loc, val) -> (selenium, state) -> { String[] args = buildArgs(method, state.expand(loc), state.expand(val)); try { new Wait() { @Override public boolean until() { try { Object result = method.invoke(selenium, args); if (Boolean.class.isAssignableFrom(result.getClass())) { return !(Boolean) result; } return false; } catch (IllegalAccessException e) { // It's going to be interesting to see this be thrown throw new RuntimeException(e); } catch (InvocationTargetException e) { return true; } } }.wait("Managed to wait for " + shortName); } catch (Wait.WaitTimedOutException e) { return NextStepDecorator.ERROR(e); } return NextStepDecorator.IDENTITY; }); // factories.put( // "waitFor" + shortName, // (loc, val) -> (selenium, state) -> { // String locator = state.expand(loc); // String value = state.expand(val); // // String[] args = buildArgs(method, locator, value); // // new Wait() { // @Override // public boolean until() { // invokeMethod(method, selenium, buildArgs(method, state.expand(loc), state.expand(val))); // return true; // } // }.wait("Can't wait for " + shortName); // return NextStepDecorator.IDENTITY; // }); // // factories.put( // "waitFor" + negateName(shortName), // (loc, val) -> (selenium, state) -> { // String locator = state.expand(loc); // String value = state.expand(val); // try { // new Wait() { // @Override // public boolean until() { // invokeMethod(method, selenium, buildArgs(method, locator, value)); // return true; // } // }.wait("Can't wait for " + shortName); // return NextStepDecorator.ASSERTION_FAILED("Wait succeeded but should have failed"); // } catch (Wait.WaitTimedOutException e) { // return NextStepDecorator.IDENTITY; // } // }); } factories.put(method.getName() + "AndWait", (loc, val) -> (selenium, state) -> invokeMethod(method, selenium, state.expand(loc), state.expand(val), (seen) -> { // TODO: Hard coding this is obviously bogus selenium.waitForPageToLoad("30000"); return NextStepDecorator.IDENTITY; })); } return factories.build(); }
From source file:suneido.runtime.BuiltinMethods.java
/** get methods through reflection */ public static Map<String, SuCallable> methods(String className, Class<?> c) { ImmutableMap.Builder<String, SuCallable> b = ImmutableMap.builder(); MethodHandles.Lookup lookup = MethodHandles.lookup(); for (Method m : c.getDeclaredMethods()) { int mod = m.getModifiers(); String methodName = methodName(m); if (!isCapitalized(methodName)) continue; if (Modifier.isPublic(mod) && Modifier.isStatic(mod)) { try { MethodHandle mh = lookup.unreflect(m); b.put(methodName, Builtin.method(mh, className + "." + methodName, params(m, 1))); } catch (IllegalAccessException e) { throw new SuInternalError("error getting method " + c.getName() + " " + m.getName(), e); }//from w w w .j a v a 2 s . c o m } else { throw new SuInternalError("BuiltinMethods found capitalized method " + c.getName() + " " + m.getName() + " that was not public static"); } } return b.build(); }
From source file:com.rapid7.conqueso.client.property.CustomPropertyDefinitionsProvider.java
public CustomPropertyDefinitionsProvider(Collection<PropertyDefinition> definitions) { ImmutableMap.Builder<String, PropertyDefinition> builder = ImmutableMap.builder(); for (PropertyDefinition definition : definitions) { builder.put(definition.getName(), definition); }/*from w ww .j a v a 2 s.c o m*/ this.propertyDefinitions = builder.build(); }
From source file:com.teradata.benchto.driver.loader.BenchmarkByActiveVariablesFilter.java
public BenchmarkByActiveVariablesFilter(BenchmarkProperties properties) { Optional<Map<String, String>> activeVariables = requireNonNull(properties, "properties is null") .getActiveVariables();/*www.j a va 2 s . c o m*/ ImmutableMap.Builder<String, Pattern> builder = ImmutableMap.<String, Pattern>builder(); if (activeVariables.isPresent()) { for (String variableKey : activeVariables.get().keySet()) { builder.put(variableKey, Pattern.compile(activeVariables.get().get(variableKey))); } } variablePatterns = builder.build(); }
From source file:org.jclouds.openstack.trove.v1.binders.BindCreateDatabaseToJson.java
@Override public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) { Builder<String, String> databaseBuilder = ImmutableMap.builder(); databaseBuilder.put("name", (String) postParams.get("database")); if (postParams.get("character_set") != null) { databaseBuilder.put("character_set", (String) postParams.get("character_set")); }/* w ww. ja v a2 s . c o m*/ if (postParams.get("collate") != null) { databaseBuilder.put("collate", (String) postParams.get("collate")); } return jsonBinder.bindToRequest(request, ImmutableMap.of("databases", ImmutableSet.of(databaseBuilder.build()))); }
From source file:org.autobet.ImmutableCollectors.java
public static <K, V> Collector<V, ?, ImmutableMap<K, V>> toImmutableMap(Function<V, K> keyMapper) { return Collector.<V, ImmutableMap.Builder<K, V>, ImmutableMap<K, V>>of(ImmutableMap.Builder::new, (builder, value) -> builder.put(keyMapper.apply(value), value), (left, right) -> { left.putAll(right.build()); return left; }, ImmutableMap.Builder::build); }
From source file:com.tkmtwo.sarapi.mapping.ValuePuttingEntryHandler.java
public void setStaticValueStrings(Map<String, String> m) { checkNotNull(m, "Map of values is null."); ImmutableMap.Builder<String, Value> vb = new ImmutableMap.Builder<String, Value>(); for (Map.Entry<String, String> me : m.entrySet()) { ArsField af = getSchemaHelper().getField(getFormName(), me.getKey()); vb.put(me.getKey(), af.getDataType().getArValue(me.getValue())); }/* www .j av a 2s .c o m*/ staticValues = vb.build(); }