Java Code Examples for java.util.concurrent.ConcurrentHashMap#replace()

The following examples show how to use java.util.concurrent.ConcurrentHashMap#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaFileUtilTest.java    From vxquery with Apache License 2.0 6 votes vote down vote up
/**
 * Test the update metadata file process.
 */
@Test
public void step5_testUpdateMetadata()
        throws IOException, ClassNotFoundException, NoSuchAlgorithmException, JAXBException {
    ConcurrentHashMap<String, XmlMetadata> fromFileMap = metaFileUtil.getMetadata();
    XmlMetadata modified = fromFileMap.get(TestConstants.XML_FILE);

    File xml = new File(TestConstants.XML_FILE);
    modified.setMd5(metaFileUtil.generateMD5(xml));

    fromFileMap.replace(TestConstants.XML_FILE, modified);

    metaFileUtil.updateMetadataMap(fromFileMap, TestConstants.INDEX_DIR);

    Assert.assertNotNull(metaFileUtil.getMetadata());

}
 
Example 2
Source File: ExpectedMBeanInfo.java    From openjdk-systemtest with Apache License 2.0 5 votes vote down vote up
/**
 * Load up the static data into our tracking structure.
 *
 * @param <code>category</code> Category of item to load.
 */
private static void initChecklist(String category) {
	ConcurrentHashMap<String, Integer> checklist = information.get(category).checklist;
	String[] itemList = information.get(category).items;
	Integer tempInt;
	for (String item : itemList) {
		tempInt = checklist.putIfAbsent(item, 1);
		if (tempInt != null) { // JFDI
			checklist.replace(item, tempInt + 1);
		}
	}
}
 
Example 3
Source File: ExpectedMBeanInfo.java    From openjdk-systemtest with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to check off items from checklist. 
 * 
 * @param name Name of checklist.
 * @param item Name of item to decrement.
 * @return The new count value of the check list item.
 */
public static Integer checkOffChecklistItem(String name, String item) {
	if (information == null) {  
		initExpectedMBean();   
	}

	ConcurrentHashMap<String, Integer> checklist = information.get(name).checklist;
	Integer currentValue = checklist.get(item);
	Integer nextValue = 1;
	if (currentValue == null) {  
		item = UNEXPECTED_PREFIX + item;
		currentValue = checklist.putIfAbsent(item, 1);
		if (currentValue != null) {
			nextValue = currentValue + 1;
		}
	} else {
		nextValue = currentValue - 1;
	}
	
	if (currentValue != null) {
		if (!checklist.replace(item, currentValue, nextValue)) {
			Report.printlnErr("Cannot successfully check off item " +
					item +
					" from " +
					name +
					" checklist! Checklist count is still " +
					currentValue);
		}
	}
	return nextValue;
}
 
Example 4
Source File: ConcurrentHashMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * replace(null, x) throws NPE
 */
public void testReplace_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace(null, "whatever");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 5
Source File: ConcurrentHashMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * replace(null, x, y) throws NPE
 */
public void testReplaceValue_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace(null, one, "whatever");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 6
Source File: ConcurrentHashMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * replace(x, null) throws NPE
 */
public void testReplace2_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace("whatever", null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 7
Source File: ConcurrentHashMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * replace(x, null, y) throws NPE
 */
public void testReplaceValue2_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace("whatever", null, "A");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 8
Source File: ConcurrentHashMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * replace(x, y, null) throws NPE
 */
public void testReplaceValue3_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace("whatever", one, null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 9
Source File: ConcurrentHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * replace(null, x) throws NPE
 */
public void testReplace_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace(null, "whatever");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 10
Source File: ConcurrentHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * replace(null, x, y) throws NPE
 */
public void testReplaceValue_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace(null, one, "whatever");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 11
Source File: ConcurrentHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * replace(x, null) throws NPE
 */
public void testReplace2_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace("whatever", null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 12
Source File: ConcurrentHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * replace(x, null, y) throws NPE
 */
public void testReplaceValue2_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace("whatever", null, "A");
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 13
Source File: ConcurrentHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * replace(x, y, null) throws NPE
 */
public void testReplaceValue3_NullPointerException() {
    ConcurrentHashMap c = new ConcurrentHashMap(5);
    try {
        c.replace("whatever", one, null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 14
Source File: TrimmedConfigurationCache.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the entry in the cache where the given key is canonical and updates or removes it.
 *
 * <p>Only one attempt is made, and if there's a collision with another change, false is returned
 * and the map is not changed.
 *
 * <p>This method succeeds (returns {@code true}) without doing anything if this key is currently
 * not canonical.
 *
 * @param transformation The transformation to apply to the given entry. The entry will be
 *     replaced with the value returned from invoking this on the original value. If it returns
 *     null, the entry will be removed instead. If it returns the same instance, nothing will be
 *     done to the entry.
 */
private boolean updateEntryIfNoConflicts(
    KeyT key, UnaryOperator<KeyAndState<KeyT>> transformation) {
  DescriptorT descriptor = getDescriptorFor(key);
  ConcurrentHashMap<ConfigurationT, KeyAndState<KeyT>> trimmingsOfDescriptor =
      descriptors.get(descriptor);
  if (trimmingsOfDescriptor == null) {
    // There are no entries at all for this descriptor.
    return true;
  }

  for (Entry<ConfigurationT, KeyAndState<KeyT>> entry : trimmingsOfDescriptor.entrySet()) {
    KeyAndState<KeyT> currentValue = entry.getValue();
    if (currentValue.getKey().equals(key)) {
      KeyAndState<KeyT> newValue = transformation.apply(currentValue);
      if (newValue == null) {
        return trimmingsOfDescriptor.remove(entry.getKey(), currentValue);
      } else if (newValue != currentValue) {
        return trimmingsOfDescriptor.replace(entry.getKey(), currentValue, newValue);
      } else {
        // newValue == currentValue, there's nothing to do
        return true;
      }
    }
  }
  // The key requested wasn't in the map, so there's nothing to do
  return true;
}