List of usage examples for java.util Map getOrDefault
default V getOrDefault(Object key, V defaultValue)
From source file:org.apache.solr.security.HadoopAuthPlugin.java
@Override public void init(Map<String, Object> pluginConfig) { try {/*from w w w. jav a 2 s .c o m*/ String delegationTokenEnabled = (String) pluginConfig.getOrDefault(DELEGATION_TOKEN_ENABLED_PROPERTY, "false"); authFilter = (Boolean.parseBoolean(delegationTokenEnabled)) ? new HadoopAuthFilter() : new AuthenticationFilter(); // Initialize kerberos before initializing curator instance. boolean initKerberosZk = Boolean .parseBoolean((String) pluginConfig.getOrDefault(INIT_KERBEROS_ZK, "false")); if (initKerberosZk) { (new Krb5HttpClientBuilder()).getBuilder(); } FilterConfig conf = getInitFilterConfig(pluginConfig); authFilter.init(conf); } catch (ServletException e) { throw new SolrException(ErrorCode.SERVER_ERROR, "Error initializing " + getClass().getName() + ": " + e); } }
From source file:io.anserini.doc.DataModel.java
private Object safeGet(Map<String, Object> obj, String key) { return obj.getOrDefault(key, defaults.getOrDefault(key, null)); }
From source file:com.couchbase.beersample.BreweryController.java
@RequestMapping(value = "/brewery/edit", method = RequestMethod.GET) String editGet(Model model, @RequestParam(value = "brewery", required = true) String brewery) { JsonDocument response = ConnectionManager.getItem(brewery); if (response != null) { Map<String, Object> map = response.content().toMap(); model.addAttribute("brewery", map); BreweryModel breweryModel = new BreweryModel(); breweryModel.setId(response.id()); breweryModel.setName(map.getOrDefault("name", "").toString()); breweryModel.setCity(map.getOrDefault("city", "").toString()); breweryModel.setDescription(map.getOrDefault("description", "").toString()); breweryModel.setState(map.getOrDefault("state", "").toString()); breweryModel.setCode(map.getOrDefault("code", "").toString()); breweryModel.setCountry(map.getOrDefault("country", "").toString()); breweryModel.setPhone(map.getOrDefault("phone", "").toString()); breweryModel.setWebsite(map.getOrDefault("website", "").toString()); breweryModel.setAddress(map.getOrDefault("address", "").toString()); breweryModel.setGeo(map.getOrDefault("geo", "").toString()); model.addAttribute("breweryModel", breweryModel); }// ww w. j a v a2s . c o m return "breweryEdit"; }
From source file:org.mitre.mpf.mst.TestStreamingJobStartStop.java
private boolean hasCorrectNodes() { List<Address> currentNodes = _masterNode.getCurrentNodeManagerHosts(); Map<NodeTypes, Long> nodeTypeCounts = currentNodes.stream().map(AddressParser::parse) .collect(groupingBy(Pair::getRight, counting())); long masterNodeCount = nodeTypeCounts.getOrDefault(NodeTypes.MasterNode, 0L); long childNodeCount = nodeTypeCounts.getOrDefault(NodeTypes.NodeManager, 0L); if (masterNodeCount == 1 && childNodeCount == 1) { return true; }/*ww w .j a va2s . co m*/ String currentNodeList = currentNodes.stream().map(Object::toString).collect(joining("\n")); LOG.warn("Current Nodes:\n{}", currentNodeList); if (masterNodeCount != 1) { LOG.warn("Incorrect number of master nodes. Expected 1 but there were {}.", masterNodeCount); } if (childNodeCount != 1) { LOG.warn("Incorrect number of child nodes. Expected 1 but there were {}.", childNodeCount); } return false; }
From source file:com.netflix.spinnaker.echo.artifacts.TemplateBasedArtifactExtractor.java
@Override public List<Artifact> getArtifacts(String source, Map payload) { if (webhookProperties == null) { return new ArrayList<>(); }//from w w w .j av a 2 s .co m String templatePath = webhookProperties.getTemplatePathForSource(source); if (StringUtils.isEmpty(templatePath)) { return (List<Artifact>) payload.getOrDefault("artifacts", new ArrayList<>()); } else { MessageArtifactTranslator translator; try { translator = new MessageArtifactTranslator(new FileInputStream(templatePath)); } catch (FileNotFoundException e) { throw new RuntimeException("Failed to read template path " + templatePath + ": " + e.getMessage(), e); } List<Artifact> result = new ArrayList<>(); try { result = translator.parseArtifacts(objectMapper.writeValueAsString(payload)); log.info("Webhook artifacts were processed: {}", result); } catch (Exception e) { log.error("Unable to translate artifacts: {}", payload, e); } return result; } }
From source file:com.pinterest.rocksplicator.controller.tasks.HealthCheckTask.java
/** * Count the number of replicas for each shard within a segment. * Throws exception if the number doesn't match the expected one. *///from ww w. j av a 2 s.c o m private void checkSegment(SegmentBean segment, int numReplicas) throws Exception { Map<Integer, Integer> shardCount = new HashMap<>(); for (HostBean hostBean : segment.getHosts()) { for (ShardBean shardBean : hostBean.getShards()) { int cnt = shardCount.getOrDefault(shardBean.getId(), 0); shardCount.put(shardBean.getId(), cnt + 1); } } if (shardCount.size() != segment.getNumShards()) { throw new Exception(String.format("Incorrect number of shards. Expected %d but actually %d.", segment.getNumShards(), shardCount.size())); } Map<Integer, Integer> badShards = new HashMap<>(); for (Map.Entry<Integer, Integer> entry : shardCount.entrySet()) { if (entry.getValue() != numReplicas) { badShards.put(entry.getKey(), entry.getValue()); } } if (!badShards.isEmpty()) { throw new Exception("Incorrect number of replicas. Bad shards: " + badShards.toString()); } }
From source file:com.yahoo.validatar.execution.rest.JSON.java
/** * Creates a HttpUriRequest based on the metadata configuration. * @param metadata The metadata configuration. * @return A configured request object.//from ww w . j a v a 2 s. com */ private HttpUriRequest createRequest(Map<String, String> metadata) { String verb = metadata.getOrDefault(VERB_KEY, DEFAULT_VERB); String url = metadata.get(URL_KEY); if (url == null || url.isEmpty()) { throw new IllegalArgumentException("The " + URL_KEY + " must be provided and contain a valid url."); } RequestBuilder builder; if (GET.equals(verb)) { builder = RequestBuilder.get(url); } else if (POST.equals(verb)) { builder = RequestBuilder.post(url); String body = metadata.getOrDefault(BODY_KEY, EMPTY_BODY); builder.setEntity(new StringEntity(body, Charset.defaultCharset())); } else { throw new UnsupportedOperationException("This HTTP method is not currently supported: " + verb); } // Everything else is assumed to be a header metadata.entrySet().stream().filter(entry -> !KNOWN_KEYS.contains(entry.getKey())) .forEach(entry -> builder.addHeader(entry.getKey(), entry.getValue())); return builder.build(); }
From source file:com.yahoo.validatar.execution.rest.JSON.java
/** * Creates a HttpClient to use for making requests. * * @param metadata The map containing the configuration for this client. * @return The created HttpClient object. *//*from ww w . j a v a 2 s. c o m*/ private HttpClient createClient(Map<String, String> metadata) { int timeout = Integer.valueOf(metadata.getOrDefault(METADATA_TIMEOUT_KEY, String.valueOf(defaultTimeout))); int retries = Integer.valueOf(metadata.getOrDefault(METADATA_RETRY_KEY, String.valueOf(defaultRetries))); RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build(); return HttpClientBuilder.create().setDefaultRequestConfig(config) .setRetryHandler(new DefaultHttpRequestRetryHandler(retries, false)).build(); }
From source file:org.codice.ddf.catalog.ui.metacard.associations.Associated.java
private void addEdge(Edge edge, Map<String, Metacard> metacards, Map<String, Metacard> changedMetacards) { String id = edge.parent.get(Metacard.ID).toString(); Metacard target = changedMetacards.getOrDefault(id, metacards.get(id)); ArrayList<String> values = Optional.of(target).map(m -> m.getAttribute(edge.relation)) .map(Attribute::getValues).map(util::getStringList).orElseGet(ArrayList::new); values.add(edge.child.get(Metacard.ID).toString()); target.setAttribute(new AttributeImpl(edge.relation, values)); changedMetacards.put(id, target);/*from ww w . j av a2 s .co m*/ }
From source file:edu.cmu.lti.oaqa.baseqa.providers.ml.classifiers.LibSvmProvider.java
@Override public String predict(Map<String, Double> features) { svm_node[] x = IntStream.range(1, fid2feat.size() + 1).mapToObj(j -> { svm_node node = new svm_node(); node.index = j;// w ww.j av a 2s . c o m node.value = features.getOrDefault(fid2feat.get(j), 0.0); return node; }).toArray(svm_node[]::new); double result = svm.svm_predict(model, x); return lid2label.get((int) result); }