List of usage examples for java.util Map getOrDefault
default V getOrDefault(Object key, V defaultValue)
From source file:org.elasticsearch.repositories.s3.AmazonS3Fixture.java
/** * Retrieves the object name from all derives paths named {pathX} where 0 <= X < 10. * * This is the counterpart of {@link #objectsPaths(String)} */// w ww . j a v a 2 s. co m private static String objectName(final Map<String, String> params) { final StringBuilder name = new StringBuilder(); for (int i = 0; i < 10; i++) { String value = params.getOrDefault("path" + i, null); if (value != null) { if (name.length() > 0) { name.append('/'); } name.append(value); } } return name.toString(); }
From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java
private static AlchemyGenerator<?> determineGeneratorForMapField(Field mapField, Class<?> mapType, Map<Class<?>, AlchemyGenerator<?>> generatorMappings) { ParameterizedType parameterizedType = (ParameterizedType) mapField.getGenericType(); Class<?> keyType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; Class<?> valueType = (Class<?>) parameterizedType.getActualTypeArguments()[1]; AlchemyGenerator<?> keyGenerator = generatorMappings.getOrDefault(keyType, determineGeneratorFor(keyType, mapField, generatorMappings)); AlchemyGenerator<?> valueGenerator = generatorMappings.getOrDefault(valueType, determineGeneratorFor(valueType, mapField, generatorMappings)); Map<Object, Object> map = new HashMap<>(); int size = one(integers(10, 100)); for (int i = 0; i < size; ++i) { map.put(keyGenerator.get(), valueGenerator.get()); }/*from w ww .j av a 2s. com*/ return () -> map; }
From source file:rgu.jclos.foldbuilder.FoldBuilder.java
/** * Generates K folds and writes them to disk * @param inputFile The CSV file from which the data comes from. * @param outputDirectory The directory in which the folds will be written. * @param separator The separating character in the CSV file. * @param indexLabel The index of the labels in the CSV file. Used for stratification of the folds. * @param k The number of folds to generates. * @param speak Whether to print some status messages along the way. * @return A pair containing a list of folds with ids of documents, and a dictionary that allows the user to retrieve aformentioned documents using the ids, in order to save space. * @throws IOException If something stops the program from reading or writing the files. *///from ww w . jav a2 s . c o m public static Pair<List<Set<String>>, Map<String, Instance>> getFolds(String inputFile, String outputDirectory, String separator, int indexLabel, int k, boolean speak) throws IOException { Random rng = new Random(); Map<String, Instance> dictionary = new HashMap<>(); Map<String, Integer> classes = new HashMap<>(); Map<String, List<String>> reversedDictionary = new HashMap<>(); int id = 0; for (String line : Files.readAllLines(new File(inputFile).toPath())) { Instance inst = new Instance(); String[] elements = line.split(separator); inst.content = line; inst.label = elements[indexLabel]; String iid = "inst" + id; dictionary.put(iid, inst); classes.put(inst.label, classes.getOrDefault(inst.label, 0) + 1); if (reversedDictionary.containsKey(inst.label)) { reversedDictionary.get(inst.label).add(iid); } else { List<String> ids = new ArrayList<>(); ids.add(iid); reversedDictionary.put(inst.label, ids); } id++; } int numberOfInstances = id; int sizeOfEachFold = (int) Math.floor(numberOfInstances / k); Map<String, Double> classRatios = new HashMap<>(); for (Map.Entry<String, Integer> classFrequency : classes.entrySet()) { classRatios.put(classFrequency.getKey(), (double) classFrequency.getValue() / (double) numberOfInstances); } List<Set<String>> folds = new ArrayList<>(); for (int i = 0; i < k; i++) { Set<String> fold = new HashSet<>(); for (Map.Entry<String, List<String>> c : reversedDictionary.entrySet()) { int currentSize = fold.size(); int numberRequired = (int) Math.floor(classRatios.get(c.getKey()) * sizeOfEachFold); while (fold.size() < currentSize + numberRequired && c.getValue().size() > 0) { int nextPick = rng.nextInt(c.getValue().size()); fold.add(c.getValue().get(nextPick)); c.getValue().remove(nextPick); } } folds.add(fold); if (speak) System.out.println("Finished computing fold " + (i + 1) + " of size " + fold.size()); } if (speak) System.out.println("Writing folds on disk"); return Pair.of(folds, dictionary); }
From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java
private static AlchemyGenerator<?> determineGeneratorForCollectionField(Field collectionField, Class<?> collectionType, Map<Class<?>, AlchemyGenerator<?>> generatorMappings) { if (isMapType(collectionType)) { return determineGeneratorForMapField(collectionField, collectionType, generatorMappings); }//from ww w. j av a2 s . c o m ParameterizedType parameterizedType = (ParameterizedType) collectionField.getGenericType(); Class<?> valueType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; AlchemyGenerator<?> generator = generatorMappings.getOrDefault(valueType, determineGeneratorFor(valueType, collectionField, generatorMappings)); List<Object> list = new ArrayList<>(); int size = one(integers(10, 100)); for (int i = 0; i < size; ++i) { list.add(generator.get()); } if (Set.class.isAssignableFrom(collectionType)) { Set set = new HashSet<>(list); return () -> set; } return () -> list; }
From source file:com.yahoo.bullet.drpc.JoinBoltTest.java
public static final void enableMetadataInConfig(Map<String, Object> config, String metaConcept, String key) { List<Map<String, String>> metadataConfig = (List<Map<String, String>>) config .getOrDefault(BulletConfig.RESULT_METADATA_METRICS, new ArrayList<>()); Map<String, String> conceptConfig = new HashMap<>(); conceptConfig.put(BulletConfig.RESULT_METADATA_METRICS_CONCEPT_KEY, metaConcept); conceptConfig.put(BulletConfig.RESULT_METADATA_METRICS_NAME_KEY, key); metadataConfig.add(conceptConfig);/* w w w.j a v a 2 s . c o m*/ config.put(BulletConfig.RESULT_METADATA_ENABLE, true); config.putIfAbsent(BulletConfig.RESULT_METADATA_METRICS, metadataConfig); }
From source file:org.apache.metron.elasticsearch.utils.ElasticsearchUtils.java
/** * Instantiates an Elasticsearch client based on es.client.class, if set. Defaults to * org.elasticsearch.transport.client.PreBuiltTransportClient. * * @param globalConfiguration Metron global config * @return/*from w ww.ja v a2s .c o m*/ */ public static TransportClient getClient(Map<String, Object> globalConfiguration) { Set<String> customESSettings = new HashSet<>(); customESSettings.addAll(Arrays.asList("es.client.class", USERNAME_CONFIG_KEY, PWD_FILE_CONFIG_KEY)); Settings.Builder settingsBuilder = Settings.builder(); Map<String, String> esSettings = getEsSettings(globalConfiguration); for (Map.Entry<String, String> entry : esSettings.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (!customESSettings.contains(key)) { settingsBuilder.put(key, value); } } settingsBuilder.put("cluster.name", globalConfiguration.get("es.clustername")); settingsBuilder.put("client.transport.ping_timeout", esSettings.getOrDefault("client.transport.ping_timeout", "500s")); setXPackSecurityOrNone(settingsBuilder, esSettings); try { LOG.info("Number of available processors in Netty: {}", NettyRuntimeWrapper.availableProcessors()); // Netty sets available processors statically and if an attempt is made to set it more than // once an IllegalStateException is thrown by NettyRuntime.setAvailableProcessors(NettyRuntime.java:87) // https://discuss.elastic.co/t/getting-availableprocessors-is-already-set-to-1-rejecting-1-illegalstateexception-exception/103082 // https://discuss.elastic.co/t/elasticsearch-5-4-1-availableprocessors-is-already-set/88036 System.setProperty("es.set.netty.runtime.available.processors", "false"); TransportClient client = createTransportClient(settingsBuilder.build(), esSettings); for (HostnamePort hp : getIps(globalConfiguration)) { client.addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(hp.hostname), hp.port)); } return client; } catch (UnknownHostException exception) { throw new RuntimeException(exception); } }
From source file:org.apache.pulsar.broker.loadbalance.impl.LoadManagerShared.java
/** * It computes least number of namespace owned by any of the domain and then it filters out all the domains that own * namespaces more than this count./*from ww w . j a va2s . co m*/ * * @param brokerToAntiAffinityNamespaceCount * @param candidates * @param brokerToDomainMap */ private static void filterDomainsNotHavingLeastNumberAntiAffinityNamespaces( Map<String, Integer> brokerToAntiAffinityNamespaceCount, Set<String> candidates, Map<String, String> brokerToDomainMap) { if (brokerToDomainMap == null || brokerToDomainMap.isEmpty()) { return; } final Map<String, Integer> domainNamespaceCount = Maps.newHashMap(); int leastNamespaceCount = Integer.MAX_VALUE; candidates.forEach(broker -> { final String domain = brokerToDomainMap.getOrDefault(broker, DEFAULT_DOMAIN); final int count = brokerToAntiAffinityNamespaceCount.getOrDefault(broker, 0); domainNamespaceCount.compute(domain, (domainName, nsCount) -> nsCount == null ? count : nsCount + count); }); // find leastNameSpaceCount for (Entry<String, Integer> domainNsCountEntry : domainNamespaceCount.entrySet()) { if (domainNsCountEntry.getValue() < leastNamespaceCount) { leastNamespaceCount = domainNsCountEntry.getValue(); } } final int finalLeastNamespaceCount = leastNamespaceCount; // only keep domain brokers which has leastNamespaceCount candidates.removeIf(broker -> { Integer nsCount = domainNamespaceCount.get(brokerToDomainMap.getOrDefault(broker, DEFAULT_DOMAIN)); return nsCount != null && nsCount != finalLeastNamespaceCount; }); }
From source file:controllers.nwbib.Lobid.java
private static void mapIsilsToUris(JsonNode items, Map<String, List<String>> result) { Iterator<JsonNode> elements = items.isArray() ? items.elements() : Arrays.asList(items).iterator(); while (elements.hasNext()) { String itemUri = elements.next().asText(); try {//from w ww . j a va 2s .c o m String isil = itemUri.split(":")[2]; List<String> uris = result.getOrDefault(isil, new ArrayList<>()); uris.add(itemUri); result.put(isil, uris); } catch (ArrayIndexOutOfBoundsException x) { Logger.error(x.getMessage()); } } }
From source file:org.apache.nifi.processors.hl7.ExtractHL7Attributes.java
private static void addSegments(final Group group, final Map<String, Segment> segments, final Map<String, Integer> segmentIndexes) throws HL7Exception { if (!isEmpty(group)) { for (final String name : group.getNames()) { for (final Structure structure : group.getAll(name)) { if (group.isGroup(name) && structure instanceof Group) { addSegments((Group) structure, segments, segmentIndexes); } else if (structure instanceof Segment) { addSegments((Segment) structure, segments, segmentIndexes); }//from ww w .jav a2 s. c o m } segmentIndexes.put(name, segmentIndexes.getOrDefault(name, 1) + 1); } } }
From source file:org.apache.pulsar.broker.loadbalance.impl.LoadManagerShared.java
/** * * It checks if given anti-affinity namespace should be unloaded by broker due to load-shedding. If all the brokers * are owning same number of anti-affinity namespaces then unloading this namespace again ends up at the same broker * from which it was unloaded. So, this util checks that given namespace should be unloaded only if it can be loaded * by different broker.//from w ww . jav a 2s .co m * * @param namespace * @param bundle * @param currentBroker * @param pulsar * @param brokerToNamespaceToBundleRange * @param candidateBroekrs * @return * @throws Exception */ public static boolean shouldAntiAffinityNamespaceUnload(String namespace, String bundle, String currentBroker, final PulsarService pulsar, Map<String, Map<String, Set<String>>> brokerToNamespaceToBundleRange, Set<String> candidateBroekrs) throws Exception { Map<String, Integer> brokerNamespaceCount = getAntiAffinityNamespaceOwnedBrokers(pulsar, namespace, brokerToNamespaceToBundleRange).get(10, TimeUnit.SECONDS); if (brokerNamespaceCount != null && !brokerNamespaceCount.isEmpty()) { int leastNsCount = Integer.MAX_VALUE; int currentBrokerNsCount = 0; for (String broker : candidateBroekrs) { int nsCount = brokerNamespaceCount.getOrDefault(broker, 0); if (currentBroker.equals(broker)) { currentBrokerNsCount = nsCount; } if (leastNsCount > nsCount) { leastNsCount = nsCount; } } // check if there is any other broker has less number of ns if (leastNsCount == 0 || currentBrokerNsCount > leastNsCount) { return true; } // check if all the brokers having same number of ns-count then broker can't unload int leastNsOwnerBrokers = 0; for (String broker : candidateBroekrs) { if (leastNsCount == brokerNamespaceCount.getOrDefault(broker, 0)) { leastNsOwnerBrokers++; } } // if all candidate brokers own same-number of ns then broker can't unload return candidateBroekrs.size() != leastNsOwnerBrokers; } return true; }