Java Code Examples for java.util.Collections#checkedMap()

The following examples show how to use java.util.Collections#checkedMap() . These examples are extracted from open source projects. 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 Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 6 votes vote down vote up
public void test_CheckedMap_computeIfAbsent() {
    Map<Integer, Double> map = new HashMap<>();
    Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
    MapDefaultMethodTester.test_computeIfAbsent(checkedMap, true /* acceptsNullKey */,
            true /* acceptsNullValue */);

    // Without generics to check the typeCheck implementation
    Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
    checkedMap2.put(1, A_STRING);

    // When key is present, function should not be invoked
    assertSame(A_STRING, checkedMap2.computeIfAbsent(1, k -> {
        throw new AssertionFailedError("key present: function should not be invoked");
    }));

    // When key is absent, computed value's type should be checked
    checkedMap2.clear();
    try {
        checkedMap2.computeIfAbsent(1, k -> NOT_A_STRING);
        fail();
    } catch (ClassCastException expected) {}
}
 
Example 2
PojoWithObjectMapFields fill()
{
    TreeMap<String, String> tm = new TreeMap<String, String>();
    tm.put("foo", "bar");

    EnumMap<GuitarPickup, Size> em = new EnumMap<GuitarPickup, Size>(
            GuitarPickup.class);
    em.put(GuitarPickup.CONTACT, Size.SMALL);

    emptyMap = Collections.emptyMap();
    singletonMap = Collections.singletonMap("key", "value");

    unmodifiableMap = Collections.unmodifiableMap(Collections
            .emptyMap());
    unmodifiableSortedMap = Collections.unmodifiableSortedMap(tm);

    synchronizedMap = Collections.synchronizedMap(em);
    synchronizedSortedMap = Collections.synchronizedSortedMap(tm);

    checkedMap = Collections.checkedMap(em, GuitarPickup.class,
            Size.class);
    checkedSortedMap = Collections.checkedSortedMap(tm, String.class,
            String.class);

    return this;
}
 
Example 3
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_CheckedMap_merge() {
    Map<Integer, Double> map = new HashMap<>();
    Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
    MapDefaultMethodTester.test_merge(checkedMap, true /* acceptsNullKey */);

    // Without generics to check the typeCheck implementation
    Map checkedMap2 =
            Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
    checkedMap2.put(1, A_STRING);

    try {
        checkedMap2.merge(1, A_STRING, (v1, v2) -> NOT_A_STRING);
        fail();
    } catch (ClassCastException expected) {}
}
 
Example 4
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_CheckedMap_compute() {
    Map<Integer, Double> map = new HashMap<>();
    Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
    MapDefaultMethodTester.test_compute(checkedMap, true /* acceptsNullKey */);

    Map checkedMap2 = Collections.checkedMap(new HashMap(), Integer.class, String.class);
    checkedMap2.put(1, A_STRING);
    try {
        checkedMap2.compute(1, (k, v) -> NOT_A_STRING);
        fail();
    } catch (ClassCastException expected) {}
}
 
Example 5
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_CheckedMap_replaceAll() {
    Map<Integer, Integer> map = new HashMap<>();
    Map checkedMap = Collections.checkedMap(map, Integer.class, Integer.class);
    checkedMap.put(1, 10);
    checkedMap.put(2, 20);
    checkedMap.put(3, 30);
    checkedMap.replaceAll((k, v) -> (Integer)k + (Integer)v);
    assertEquals(11, checkedMap.get(1));
    assertEquals(22, checkedMap.get(2));
    assertEquals(33, checkedMap.get(3));
    assertEquals(3, checkedMap.size());
}
 
Example 6
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_CheckedMap_replace$K$V$V() {
    Map<Integer, Double> map = new HashMap<>();
    Map checkedMap = Collections.checkedMap(map, Integer.class, Double.class);
    MapDefaultMethodTester.test_replace$K$V$V(checkedMap, true /* acceptsNullKey */,
            true /* acceptsNullValue */);

    // Without generics to check the typeCheck implementation
    Map checkedMap2 = Collections.checkedMap(new HashMap<>(), Integer.class, String.class);
    checkedMap2.put(1, A_STRING);

    try {
        checkedMap2.replace(1, NOT_A_STRING);
        fail();
    } catch (ClassCastException expected) {}
}
 
Example 7
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 8
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 9
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 10
protected Map<String, String> getObject() {
    Map<String, String> map = Collections.singletonMap("key", "value");
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 11
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 12
protected Map<String, String> getObject() {
    Map<String, String> map = Collections.singletonMap("key", "value");
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 13
Source Project: j2objc   File: Collections2Test.java    License: Apache License 2.0 4 votes vote down vote up
public void test_checkedMapSerializationCompatability() throws Exception {
    Map<String, String> c = new HashMap<String, String>();
    assertFalse(c instanceof SortedMap);
    c = Collections.checkedMap(c, String.class, String.class);
    SerializationTester.assertCompabilityEquals(c, "serialization/org/apache/harmony/tests/java/util/Collections_CheckedMap.golden.ser");
}
 
Example 14
protected Map<String, String> getObject() {
    Map<String, String> map = Collections.singletonMap("key", "value");
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 15
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 16
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 17
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 18
/**
 * Constructs a {@code CertificateRevokedException} with
 * the specified revocation date, reason code, authority name, and map
 * of extensions.
 *
 * @param revocationDate the date on which the certificate was revoked. The
 *    date is copied to protect against subsequent modification.
 * @param reason the revocation reason
 * @param extensions a map of X.509 Extensions. Each key is an OID String
 *    that maps to the corresponding Extension. The map is copied to
 *    prevent subsequent modification.
 * @param authority the {@code X500Principal} that represents the name
 *    of the authority that signed the certificate's revocation status
 *    information
 * @throws NullPointerException if {@code revocationDate},
 *    {@code reason}, {@code authority}, or
 *    {@code extensions} is {@code null}
 */
public CertificateRevokedException(Date revocationDate, CRLReason reason,
    X500Principal authority, Map<String, Extension> extensions) {
    if (revocationDate == null || reason == null || authority == null ||
        extensions == null) {
        throw new NullPointerException();
    }
    this.revocationDate = new Date(revocationDate.getTime());
    this.reason = reason;
    this.authority = authority;
    // make sure Map only contains correct types
    this.extensions = Collections.checkedMap(new HashMap<>(),
                                             String.class, Extension.class);
    this.extensions.putAll(extensions);
}
 
Example 19
/**
 * Constructs a {@code CertificateRevokedException} with
 * the specified revocation date, reason code, authority name, and map
 * of extensions.
 *
 * @param revocationDate the date on which the certificate was revoked. The
 *    date is copied to protect against subsequent modification.
 * @param reason the revocation reason
 * @param extensions a map of X.509 Extensions. Each key is an OID String
 *    that maps to the corresponding Extension. The map is copied to
 *    prevent subsequent modification.
 * @param authority the {@code X500Principal} that represents the name
 *    of the authority that signed the certificate's revocation status
 *    information
 * @throws NullPointerException if {@code revocationDate},
 *    {@code reason}, {@code authority}, or
 *    {@code extensions} is {@code null}
 */
public CertificateRevokedException(Date revocationDate, CRLReason reason,
    X500Principal authority, Map<String, Extension> extensions) {
    if (revocationDate == null || reason == null || authority == null ||
        extensions == null) {
        throw new NullPointerException();
    }
    this.revocationDate = new Date(revocationDate.getTime());
    this.reason = reason;
    this.authority = authority;
    // make sure Map only contains correct types
    this.extensions = Collections.checkedMap(new HashMap<>(),
                                             String.class, Extension.class);
    this.extensions.putAll(extensions);
}
 
Example 20
/**
 * Constructs a {@code CertificateRevokedException} with
 * the specified revocation date, reason code, authority name, and map
 * of extensions.
 *
 * @param revocationDate the date on which the certificate was revoked. The
 *    date is copied to protect against subsequent modification.
 * @param reason the revocation reason
 * @param extensions a map of X.509 Extensions. Each key is an OID String
 *    that maps to the corresponding Extension. The map is copied to
 *    prevent subsequent modification.
 * @param authority the {@code X500Principal} that represents the name
 *    of the authority that signed the certificate's revocation status
 *    information
 * @throws NullPointerException if {@code revocationDate},
 *    {@code reason}, {@code authority}, or
 *    {@code extensions} is {@code null}
 * @throws ClassCastException if {@code extensions} contains an incorrectly
 *    typed key or value
 */
public CertificateRevokedException(Date revocationDate, CRLReason reason,
    X500Principal authority, Map<String, Extension> extensions) {
    if (revocationDate == null || reason == null || authority == null ||
        extensions == null) {
        throw new NullPointerException();
    }
    this.revocationDate = new Date(revocationDate.getTime());
    this.reason = reason;
    this.authority = authority;
    // make sure Map only contains correct types
    this.extensions = Collections.checkedMap(new HashMap<>(),
                                             String.class, Extension.class);
    this.extensions.putAll(extensions);
}