Typename fix for collection types.

This commit is contained in:
Harald Kuhr 2016-11-07 16:41:11 +01:00
parent aff252f278
commit ee002dfc87
2 changed files with 47 additions and 0 deletions

View File

@ -30,6 +30,10 @@ package com.twelvemonkeys.imageio.metadata.xmp;
import com.twelvemonkeys.imageio.metadata.AbstractEntry; import com.twelvemonkeys.imageio.metadata.AbstractEntry;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* XMPEntry * XMPEntry
* *
@ -63,6 +67,23 @@ final class XMPEntry extends AbstractEntry {
return fieldName != null ? fieldName : XMP.DEFAULT_NS_MAPPING.get(getIdentifier()); return fieldName != null ? fieldName : XMP.DEFAULT_NS_MAPPING.get(getIdentifier());
} }
@Override
public String getTypeName() {
// Special handling for collections
Object value = getValue();
if (value instanceof List) {
return "List";
}
else if (value instanceof Set) {
return "Set";
}
else if (value instanceof Map) {
return "Map";
}
// Fall back to class name
return super.getTypeName();
}
@Override @Override
public String toString() { public String toString() {
String type = getTypeName(); String type = getTypeName();

View File

@ -30,6 +30,11 @@ package com.twelvemonkeys.imageio.metadata.xmp;
import com.twelvemonkeys.imageio.metadata.Entry; import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.EntryAbstractTest; import com.twelvemonkeys.imageio.metadata.EntryAbstractTest;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.assertEquals;
/** /**
* XMPEntryTest * XMPEntryTest
@ -43,4 +48,25 @@ public class XMPEntryTest extends EntryAbstractTest {
protected Entry createEntry(Object value) { protected Entry createEntry(Object value) {
return new XMPEntry(XMP.NS_XAP + ":foo", value); return new XMPEntry(XMP.NS_XAP + ":foo", value);
} }
@Test
public void testTypeNameMap() {
assertEquals("Map", createEntry(new HashMap<>()).getTypeName());
assertEquals("Map", createEntry(Collections.emptyMap()).getTypeName());
assertEquals("Map", createEntry(Collections.singletonMap("foo", "bar")).getTypeName());
}
@Test
public void testTypeNameSet() {
assertEquals("Set", createEntry(new HashSet<>()).getTypeName());
assertEquals("Set", createEntry(Collections.singleton("foo")).getTypeName());
assertEquals("Set", createEntry(Collections.unmodifiableSet(Collections.singleton("foo"))).getTypeName());
}
@Test
public void testTypeNameList() {
assertEquals("List", createEntry(new ArrayList<>()).getTypeName());
assertEquals("List", createEntry(Collections.emptyList()).getTypeName());
assertEquals("List", createEntry(Arrays.asList("foo", "bar")).getTypeName());
}
} }