下面列出了java.util.concurrent.ConcurrentHashMap#KeySetView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Removes all occurrences of the referencing product type key from {@link #missingNestedProductTypes}.
* If there are no referencing product types for any missing nested product type, the whole entry for this
* missing nested product type will be removed from {@link #missingNestedProductTypes}.
*
* <p>Important: This method is meant to be used only for internal use of the library and should not be used by
* externally.
*
* @param referencingProductTypeKey the key that should be removed from {@link #missingNestedProductTypes}.
*/
public void removeReferencingProductTypeKey(@Nonnull final String referencingProductTypeKey) {
final Iterator<ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
referencingProductTypesIterator = missingNestedProductTypes.values().iterator();
while (referencingProductTypesIterator.hasNext()) {
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
referencingProductTypes = referencingProductTypesIterator.next();
referencingProductTypes.remove(referencingProductTypeKey);
// If there are no referencing product types for this missing nested product type,
// remove referencing product types map.
if (referencingProductTypes.isEmpty()) {
referencingProductTypesIterator.remove();
}
}
}
/**
* KeySetView.getMappedValue returns the map's mapped value
*/
public void testGetMappedValue() {
ConcurrentHashMap map = map5();
assertNull(((ConcurrentHashMap.KeySetView) map.keySet()).getMappedValue());
try {
map.keySet(null);
shouldThrow();
} catch (NullPointerException success) {}
ConcurrentHashMap.KeySetView set = map.keySet(one);
assertFalse(set.add(one));
assertTrue(set.add(six));
assertTrue(set.add(seven));
assertTrue(set.getMappedValue() == one);
assertTrue(map.get(one) != one);
assertTrue(map.get(six) == one);
assertTrue(map.get(seven) == one);
}
@Test
void getNumberOfProductTypesWithMissingNestedProductTypes_WithOneReferencingProdTypes_ShouldBe1() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
productTypesReferencingMissing1.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test and assertion
assertThat(productTypeSyncStatistics.getNumberOfProductTypesWithMissingNestedProductTypes()).isEqualTo(1);
}
@Test
void getNumberOfProductTypesWithMissingNestedProductTypes_WithSameReferencingProdTypes_CountDistinct() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
productTypesReferencingMissing1.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing1.put("referencing-product-type-2", ConcurrentHashMap.newKeySet());
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing2 = new ConcurrentHashMap<>();
productTypesReferencingMissing2.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing2.put("referencing-product-type-2", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
missingProductTypeReferences.put("missing2", productTypesReferencingMissing2);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test and assertion
assertThat(productTypeSyncStatistics.getNumberOfProductTypesWithMissingNestedProductTypes()).isEqualTo(2);
}
@Test
void getNumberOfProductTypesWithMissingNestedProductTypes_WithDifferentReferencingProdTypes_CountDistinct() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
productTypesReferencingMissing1.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing1.put("referencing-product-type-2", ConcurrentHashMap.newKeySet());
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing2 = new ConcurrentHashMap<>();
productTypesReferencingMissing2.put("referencing-product-type-3", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing2.put("referencing-product-type-4", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
missingProductTypeReferences.put("missing2", productTypesReferencingMissing2);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test and assertion
assertThat(productTypeSyncStatistics.getNumberOfProductTypesWithMissingNestedProductTypes()).isEqualTo(4);
}
/**
* KeySetView.getMappedValue returns the map's mapped value
*/
public void testGetMappedValue() {
ConcurrentHashMap map = map5();
assertNull(map.keySet().getMappedValue());
try {
map.keySet(null);
shouldThrow();
} catch (NullPointerException success) {}
ConcurrentHashMap.KeySetView set = map.keySet(one);
assertFalse(set.add(one));
assertTrue(set.add(six));
assertTrue(set.add(seven));
assertTrue(set.getMappedValue() == one);
assertTrue(map.get(one) != one);
assertTrue(map.get(six) == one);
assertTrue(map.get(seven) == one);
}
@Test
void removeReferencingProductTypeKey_WithEmptyMap_ShouldRemoveNothing() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.removeReferencingProductTypeKey("foo");
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).isEmpty();
}
@Test
void removeReferencingProductTypeKey_WithMultipleOccurrence_ShouldRemoveAllOccurrences() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
productTypesReferencingMissing1.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing1.put("referencing-product-type-2", ConcurrentHashMap.newKeySet());
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing2 = new ConcurrentHashMap<>();
productTypesReferencingMissing2.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing2.put("referencing-product-type-4", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
missingProductTypeReferences.put("missing2", productTypesReferencingMissing2);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.removeReferencingProductTypeKey("referencing-product-type-1");
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(2);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing1"))
.containsOnlyKeys("referencing-product-type-2");
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing2"))
.containsOnlyKeys("referencing-product-type-4");
}
@Test
void removeReferencingProductTypeKey_WithOnlyOccurrenceForMissingRef_ShouldRemoveMissingRef() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
productTypesReferencingMissing1.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing2 = new ConcurrentHashMap<>();
productTypesReferencingMissing2.put("referencing-product-type-1", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing2.put("referencing-product-type-4", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
missingProductTypeReferences.put("missing2", productTypesReferencingMissing2);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.removeReferencingProductTypeKey("referencing-product-type-1");
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(1);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing2"))
.containsOnlyKeys("referencing-product-type-4");
}
@Nonnull
private ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>> asMap(
@Nonnull final String referencingProductTypeKey,
@Nonnull final AttributeDefinitionDraft referencingAttributeDefinitionDraft) {
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
newReferencingProductTypes = new ConcurrentHashMap<>();
newReferencingProductTypes
.put(
referencingProductTypeKey,
asSet(referencingAttributeDefinitionDraft));
return newReferencingProductTypes;
}
@Test
void getNumberOfProductTypesWithMissingNestedProductTypes_WithEmptyMap_ShouldReturn0() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test and assertion
assertThat(productTypeSyncStatistics.getNumberOfProductTypesWithMissingNestedProductTypes()).isEqualTo(0);
}
@Test
void putMissingNestedProductType_WithNullExistingReferencingProductTypes_ShouldCreateANewMap() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
final AttributeDefinitionDraft referencingAttributeDefinitionDraft = AttributeDefinitionDraftBuilder
.of(NestedAttributeType.of(
ProductType.referenceOfId("missingPT")), "attr-name", ofEnglish("label"), true)
.build();
final ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean> definitionDrafts =
ConcurrentHashMap.newKeySet();
definitionDrafts.add(referencingAttributeDefinitionDraft);
productTypesReferencingMissing1.put("referencingPT", definitionDrafts);
missingProductTypeReferences.put("missingPT", productTypesReferencingMissing1);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.putMissingNestedProductType("newMissing",
"referencingPT",
referencingAttributeDefinitionDraft);
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(2);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("newMissing")).hasSize(1);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()
.get("newMissing")
.get("referencingPT"))
.contains(referencingAttributeDefinitionDraft);
}
@Test
void putMissingNestedProductType_WithNullExistingAttributeDefs_ShouldCreateANewMap() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
final AttributeDefinitionDraft referencingAttributeDefinitionDraft = AttributeDefinitionDraftBuilder
.of(NestedAttributeType.of(
ProductType.referenceOfId("missingPT")), "attr-name", ofEnglish("label"), true)
.build();
final ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean> definitionDrafts =
ConcurrentHashMap.newKeySet();
definitionDrafts.add(referencingAttributeDefinitionDraft);
missingProductTypeReferences.put("missingPT", productTypesReferencingMissing1);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.putMissingNestedProductType("missingPT",
"referencingPT",
referencingAttributeDefinitionDraft);
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(1);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missingPT")).hasSize(1);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()
.get("missingPT")
.get("referencingPT"))
.contains(referencingAttributeDefinitionDraft);
}
@Test
void putMissingNestedProductType_WithIdenticalOneReferencingAttrToAnEmptyMap_ShouldOverwriteExisting() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
final AttributeDefinitionDraft referencingAttributeDefinitionDraft = AttributeDefinitionDraftBuilder
.of(NestedAttributeType.of(
ProductType.referenceOfId("missingPT")), "attr-name", ofEnglish("label"), true)
.build();
final ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean> definitionDrafts =
ConcurrentHashMap.newKeySet();
definitionDrafts.add(referencingAttributeDefinitionDraft);
productTypesReferencingMissing1.put("referencingPT", definitionDrafts);
missingProductTypeReferences.put("missingPT", productTypesReferencingMissing1);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.putMissingNestedProductType("missingPT",
"referencingPT",
referencingAttributeDefinitionDraft);
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(1);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missingPT")).hasSize(1);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()
.get("missingPT")
.get("referencingPT"))
.contains(referencingAttributeDefinitionDraft);
}
@Override
public ConcurrentHashMap.KeySetView keySet() {
return (ConcurrentHashMap.KeySetView) (Object) new KeySetView<K>(delegate.keySet());
}
@Test
void putMissingNestedProductType_WithAdditionalAttribute_ShouldAppendAttribute() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
final ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean> definitionDrafts =
ConcurrentHashMap.newKeySet();
final AttributeDefinitionDraft existingReferencingAttr = AttributeDefinitionDraftBuilder
.of(NestedAttributeType.of(
ProductType.referenceOfId("missing1")), "attr-name-1", ofEnglish("label"), true)
.build();
definitionDrafts.add(existingReferencingAttr);
productTypesReferencingMissing1.put("referencing-product-type-1", definitionDrafts);
productTypesReferencingMissing1.put("referencing-product-type-2", ConcurrentHashMap.newKeySet());
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing2 = new ConcurrentHashMap<>();
productTypesReferencingMissing2.put("referencing-product-type-3", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing2.put("referencing-product-type-4", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
missingProductTypeReferences.put("missing2", productTypesReferencingMissing2);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
final AttributeDefinitionDraft referencingAttributeDefinitionDraft = AttributeDefinitionDraftBuilder
.of(NestedAttributeType.of(
ProductType.referenceOfId("missing1")), "attr-name", ofEnglish("label"), true)
.build();
// test
productTypeSyncStatistics.putMissingNestedProductType("missing1",
"referencing-product-type-1",
referencingAttributeDefinitionDraft);
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(2);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing1")).hasSize(2);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()
.get("missing1")
.get("referencing-product-type-1"))
.containsExactlyInAnyOrder(existingReferencingAttr, referencingAttributeDefinitionDraft);
}
@Override
public String[] getFileNames() throws IOException {
ConcurrentHashMap.KeySetView<String, MongoFile> strings = nameToFileMap.keySet();
return strings.toArray(new String[strings.size()]);
}
@Test
void removeReferencingProductTypeKey_WithOneOccurrence_ShouldRemoveOccurrence() {
// preparation
final ConcurrentHashMap<String,
ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>>
missingProductTypeReferences = new ConcurrentHashMap<>();
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing1 = new ConcurrentHashMap<>();
final ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean> definitionDrafts =
ConcurrentHashMap.newKeySet();
final AttributeDefinitionDraft existingReferencingAttr = AttributeDefinitionDraftBuilder
.of(NestedAttributeType.of(
ProductType.referenceOfId("missing1")), "attr-name-1", ofEnglish("label"), true)
.build();
definitionDrafts.add(existingReferencingAttr);
productTypesReferencingMissing1.put("referencing-product-type-1", definitionDrafts);
productTypesReferencingMissing1.put("referencing-product-type-2", ConcurrentHashMap.newKeySet());
final ConcurrentHashMap<String, ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>
productTypesReferencingMissing2 = new ConcurrentHashMap<>();
productTypesReferencingMissing2.put("referencing-product-type-3", ConcurrentHashMap.newKeySet());
productTypesReferencingMissing2.put("referencing-product-type-4", ConcurrentHashMap.newKeySet());
missingProductTypeReferences.put("missing1", productTypesReferencingMissing1);
missingProductTypeReferences.put("missing2", productTypesReferencingMissing2);
final ProductTypeSyncStatistics productTypeSyncStatistics =
new ProductTypeSyncStatistics(missingProductTypeReferences);
// test
productTypeSyncStatistics.removeReferencingProductTypeKey("referencing-product-type-1");
// assertion
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents()).hasSize(2);
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing1"))
.containsOnlyKeys("referencing-product-type-2");
assertThat(productTypeSyncStatistics.getProductTypeKeysWithMissingParents().get("missing2"))
.containsOnlyKeys("referencing-product-type-3", "referencing-product-type-4");
}
/**
* Return an enumeration of the coorelation ids.
* @return coorelation ids.
*/
ConcurrentHashMap.KeySetView<String, Future> getFuturesKeySet() {
return this.futures.keySet();
}
/**
* @return an unmodifiable {@link ConcurrentHashMap} ({@code missingNestedProductTypes}) which keeps track of the
* keys of missing product types, the keys of the product types which are referencing those missing product
* types and a list of attribute definitions which contains those references.
* <ul>
* <li>key: key of the missing product type</li>
* <li>value: a map of which consists of:
* <ul>
* <li>key: key of the product type referencing the missing product type.</li>
* <li>value: a set of the attribute definition drafts which contains the reference
* to the missing product type.</li>
* </ul>
* </li>
* </ul>
*/
public Map<String,
ConcurrentHashMap<String,
ConcurrentHashMap.KeySetView<AttributeDefinitionDraft, Boolean>>> getProductTypeKeysWithMissingParents() {
return Collections.unmodifiableMap(missingNestedProductTypes);
}