private LinkedHashMap<String, Long> getOrCreateFacet(String facetName) { return facetsByName.computeIfAbsent(facetName, n -> new LinkedHashMap<>()); } }
@Override @Nullable public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) { String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key); if (oldKey != null) { return this.targetMap.get(oldKey); } return this.targetMap.computeIfAbsent(key, mappingFunction); }
private void update(Map<TopicPartition, S> partitionToState) { LinkedHashMap<String, List<TopicPartition>> topicToPartitions = new LinkedHashMap<>(); for (TopicPartition tp : partitionToState.keySet()) { List<TopicPartition> partitions = topicToPartitions.computeIfAbsent(tp.topic(), k -> new ArrayList<>()); partitions.add(tp); } for (Map.Entry<String, List<TopicPartition>> entry : topicToPartitions.entrySet()) { for (TopicPartition tp : entry.getValue()) { S state = partitionToState.get(tp); map.put(tp, state); } } }
@Override @Nullable public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) { String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key); if (oldKey != null) { return this.targetMap.get(oldKey); } return this.targetMap.computeIfAbsent(key, mappingFunction); }
/** * This method will return the value for the specified key if it is cached, * and if not, will calculate the value using the supplied method. The * computed value may or may not be stored in the cache afterwards. This * method is guaranteed to be lock-free, and might calculate the value * instead of using the cached one to avoid blocking. * * @param key the key to retrieve the value for * @param compute method to use to compute the value if it is not cached * @return the cached or computed value */ public long getOrCompute(K key, LongSupplier compute) { if (free.compareAndSet(true, false)) { try { return cache.computeIfAbsent(key, k -> compute.getAsLong()); } finally { free.set(true); } } else { return compute.getAsLong(); } }
private void handleWrappedFilterArgs(Map<String, String> args) { LinkedHashMap<String, Map<String, String>> wrappedFilterArgs = new LinkedHashMap<>(); splitAt(',', wrappedFilters).forEach(filterName -> { // Format: SPIname[-id] filterName = filterName.trim().toLowerCase(Locale.ROOT); // Treat case-insensitively if (wrappedFilterArgs.containsKey(filterName)) { throw new IllegalArgumentException("wrappedFilters contains duplicate '" + filterName + "'. Add unique '-id' suffixes (stripped prior to SPI lookup)."); } wrappedFilterArgs.put(filterName, new HashMap<>()); }); for (Iterator<Map.Entry<String, String>> iterator = args.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry<String, String> entry = iterator.next(); String filterArgKey = entry.getKey(); String argValue = entry.getValue(); List<String> splitKey = splitAt(FILTER_ARG_SEPARATOR, filterArgKey); // Format: filterName.argKey if (splitKey.size() == 2) { // Skip if no slash String filterName = splitKey.get(0).toLowerCase(Locale.ROOT); if (wrappedFilterArgs.containsKey(filterName)) { // Skip if not in "wrappedFilter" arg Map<String, String> filterArgs = wrappedFilterArgs.computeIfAbsent(filterName, k -> new HashMap<>()); String argKey = splitKey.get(1); filterArgs.put(argKey, argValue); // argKey is guaranteed unique, don't need to check for duplicates iterator.remove(); } } } if (args.isEmpty()) { populateInnerFilters(wrappedFilterArgs); } }
/** * Add a 'pending drops' changeSet for the given version. */ public void add(MigrationVersion version, ChangeSet changeSet) { Entry entry = map.computeIfAbsent(version.normalised(), k -> new Entry(version)); entry.add(changeSet); }
@Override public String[] computeIfAbsent(String key, Function<? super String, ? extends String[]> mappingFunction) { synchronized (this) { return super.computeIfAbsent(key, mappingFunction); } } };
@Override public V get(K key) { synchronized (map) { return map.computeIfAbsent(key, loadingFunction); } }
@Override public synchronized Object computeIfAbsent(Object key, Function<? super Object, ?> mappingFunction) { return propertyMap.computeIfAbsent(key, mappingFunction); }
private LinkedHashMap<String, Long> getOrCreateFacet(String facetName) { return facetsByName.computeIfAbsent(facetName, n -> new LinkedHashMap<>()); } }
public static ArrayList<IResearch> getPlayerResearch(UUID uuid) { if (uuid != null) { research.computeIfAbsent(uuid, k -> new ArrayList<>()); return research.get(uuid); } return new ArrayList<>(); } }
/** * Add a field/value pair to the document * * @param field the name of the field * @param value any value */ public void add(final ParserField field, final Object value) { if (value == null) return; if (fields == null) fields = new LinkedHashMap<>(); final List<Object> values = (List<Object>) fields.computeIfAbsent(field.name, f -> new ArrayList<>(1)); if (value instanceof Collection) values.addAll((Collection) value); else values.add(value); }
public VariantBuilder addSample(String sampleName, List<String> data) { checkStudy("add sample"); if (samplesData == null) { samplesData = new ArrayList<>(samplesPosition != null ? samplesPosition.size() : 1); } if (samplesPosition == null) { samplesPosition = new LinkedHashMap<>(); } Integer idx = samplesPosition.computeIfAbsent(sampleName, k -> samplesPosition.size()); addSample(idx, data); return this; }
@Override @Nullable public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) { String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key); if (oldKey != null) { return this.targetMap.get(oldKey); } return this.targetMap.computeIfAbsent(key, mappingFunction); }
public void setProperty(String key, String value) { Key keyy = new Key(ItemType.VALUE, key); properties.computeIfAbsent(keyy, key1 -> Item.value("%s=%s", key1.id, null)).value = value; }
/** * @param bagger Bagger */ private void initializeProfile(final Bagger bagger) { final Collection<Profile> profiles = bagger.loadProfiles(); userProfiles = new HashMap<>(); profileFieldsMap = new LinkedHashMap<>(); for (final Profile profile : profiles) { userProfiles.put(profile.getName(), profile); final LinkedHashMap<String, ProfileField> standardFields = profile.getStandardFields(); final LinkedHashMap<String, ProfileField> customFields = profile.getCustomFields(); final LinkedHashMap<String, ProfileField> mergedMap = new LinkedHashMap<>(); mergedMap.putAll(standardFields); mergedMap.putAll(customFields); for (final ProfileField profileField : mergedMap.values()) { final List<ProfileField> list = profileFieldsMap.computeIfAbsent(profile.getName(), k -> new ArrayList<>()); list.add(profileField); } } }
/** * Add a 'pending drops' changeSet for the given version. */ public void add(MigrationVersion version, ChangeSet changeSet) { Entry entry = map.computeIfAbsent(version.normalised(), k -> new Entry(version)); entry.add(changeSet); }
private static Collector<PizzaView, ?, List<Pizza>> toPizzaList() { BiConsumer<LinkedHashMap<Long, Pizza>, PizzaView> accumulator = (acc, pv) -> { Topping t = new Topping(pv.toppingId); t.setName(pv.toppingName); Pizza p = acc.computeIfAbsent(pv.id, pv::toPizza); p.getToppings().add(t); acc.put(pv.id, p); }; return Collector.of(LinkedHashMap::new, accumulator, (l, r) -> { r.forEach((key, value) -> l.merge(key, value, (p1, p2) -> { p1.getToppings().addAll(p2.getToppings()); return p1; })); return l; }, map -> new ArrayList<>(map.values())); }
private static String expectedMultimapJson(Multimap<?, ?> multimap, int opts) { final LinkedHashMap<Object, List<Object>> map = new LinkedHashMap<>(); multimap.forEach(e -> { List<Object> list = map.computeIfAbsent(e._1, k -> new ArrayList<>()); list.add(e._2); }); StringBuilder sb = new StringBuilder("{"); map.forEach((k, l) -> sb.append(expectedJson(k.toString(), opts)).append(":").append(expectedJson(io.vavr.collection.Stream.ofAll(l)))); sb.append("}"); return sb.toString(); }