下面列出了java.util.concurrent.ConcurrentHashMap#replace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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());
}
/**
* 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);
}
}
}
/**
* 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;
}
/**
* replace(null, x) throws NPE
*/
public void testReplace_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* 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) {}
}
/**
* replace(x, null) throws NPE
*/
public void testReplace2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace("whatever", null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* 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) {}
}
/**
* 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) {}
}
/**
* replace(null, x) throws NPE
*/
public void testReplace_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* 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) {}
}
/**
* replace(x, null) throws NPE
*/
public void testReplace2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace("whatever", null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* 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) {}
}
/**
* 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) {}
}
/**
* 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;
}