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

The following examples show how to use java.util.Collections#checkedMap() . 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: AbstractRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
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 2
Source File: CollectionsTest.java    From j2objc with 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 3
Source File: CollectionsTest.java    From j2objc with 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 4
Source File: CollectionsTest.java    From j2objc with 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 5
Source File: CollectionsTest.java    From j2objc with 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 6
Source File: CollectionsTest.java    From j2objc with 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 7
Source File: java_util_Collections_CheckedMap.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getObject() {
    Map<String, String> map = Collections.singletonMap("key", "value");
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 8
Source File: java_util_Collections_CheckedMap.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 9
Source File: java_util_Collections_CheckedMap.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getObject() {
    Map<String, String> map = Collections.singletonMap("key", "value");
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 10
Source File: java_util_Collections_CheckedMap.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 11
Source File: java_util_Collections_CheckedMap.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 12
Source File: Collections2Test.java    From j2objc with 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 13
Source File: java_util_Collections_CheckedMap.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getObject() {
    Map<String, String> map = Collections.singletonMap("key", "value");
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 14
Source File: java_util_Collections_CheckedMap.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 15
Source File: java_util_Collections_CheckedMap.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 16
Source File: java_util_Collections_CheckedMap.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 17
Source File: java_util_Collections_CheckedMap.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
protected Map<String, String> getAnotherObject() {
    Map<String, String> map = Collections.emptyMap();
    return Collections.checkedMap(map, String.class, String.class);
}
 
Example 18
Source File: CertificateRevokedException.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * 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
Source File: CertificateRevokedException.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
Source File: CertificateRevokedException.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
}