ProvidenceConfigUtil { @SuppressWarnings("unchecked") static <K,V> Map<K,V> asMap(Object value, PDescriptor keyType, PDescriptor itemType) throws ProvidenceConfigException { if (value instanceof Map) { boolean sorted = value instanceof TreeMap || value instanceof ImmutableSortedMap; Map<K,V> out = sorted ? new TreeMap<>() : new LinkedHashMap<>(); for (Map.Entry item : ((Map<?,?>) value).entrySet()) { out.put((K) asType(keyType, item.getKey()), (V) asType(itemType, item.getValue())); } return out; } throw new ProvidenceConfigException( "Unable to convert " + value.getClass().getSimpleName() + " to a collection"); } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); static final String UNDEFINED; }
@Test public void testAsMap() throws ProvidenceConfigException { assertThat(asMap(new TreeMap<>(), PPrimitive.STRING, PPrimitive.I32), is(instanceOf(TreeMap.class))); assertThat(asMap(new HashMap<>(), PPrimitive.STRING, PPrimitive.I32), is(instanceOf(LinkedHashMap.class))); assertThat(asMap(ImmutableSortedMap.of(1, (short) 2), PPrimitive.STRING, PPrimitive.I32), is(instanceOf(TreeMap.class))); assertThat(asMap(ImmutableMap.of(1, 2.0), PPrimitive.STRING, PPrimitive.I32), is(ImmutableMap.of("1", 2))); try { asMap(ImmutableSet.of(1, 2), PPrimitive.BOOL, PPrimitive.DOUBLE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Unable to convert RegularImmutableSet to a collection")); } }