可以编写泛型XmlAdapter [英] Can generic XmlAdapter be written
问题描述
我知道,我可以使用Raw类型来编写XMLAdapter,但是我可以使用泛型类型。我尝试阅读API(链接),但是甚至没有注意到这方面的线索。
例如map:
我想要使用,如:
@XmlJavaTypeAdapter(GenericMapAdapter< String,Double> .class)//
private final HashMap< String,Double> depWageSum = //
new HashMap< String,Double>(); b
$ b code>< depWageSum>
< entry key =RI> 289.001< / entry>
< entry key =VT> 499.817< / entry>
< entry key =HI> 41.824< / entry>
...
< depWageSum>
类本身可能会看起来如下所示:
@SuppressWarnings(serial)public class GenericMapAdapter< K,V>扩展XmlAdapter< GenericMapAdapter.MapType< K,V>,Map< K,V>> {
public static class MapType< K,V> {
@XmlValue protected final List< MapTypeEntry< K,V>> entry = new ArrayList< MapTypeEntry< K,V>>();
public static class MapTypeEntry< K,V> {
@XmlAttribute受保护的K键;
@XmlValue保护V值;
私人MapTypeEntry(){};
public static< K,V> MapTypeEntry< K,V> (final K k,final V v){
返回新的MapTypeEntry< K,V>(){{this.key = k; this.value = v;}};
}}}
@Override public Map< K,V> (GenericMapAdapter.MapType.MapTypeEntry< K,V> myEntryType:v.entry)返回新的HashMap< K,V>(){{for(GenericMapAdapter.MapType.MapTypeEntry<
this.put(myEntryType.key,myEntryType.value);}};
}
@Override public MapType< K,V> marshal(final Map< K,V> v)抛出Exception {
return new GenericMapAdapter.MapType< K,V>(){{for(K key:v.keySet())
this.entry .add(MapTypeEntry.of(key,v.get(key)));}};
}}
您无法使用如上所述做到这一点。类的参数不会被类保留。但是,您可以引入一些简单的子类,它们可以利用GenericMapAdapter中的逻辑:
public class StringDoubleMapAdapter extends GenericMapAdapter< String,Double> {
}
然后在属性上使用适配器子类:
@XmlJavaTypeAdapter(StringDoubleMapAdapter.class)//
private final HashMap< String,Double> depWageSum = //
new HashMap< String,Double>();
有关XmlAdapter的更多信息,请参阅:
I know, that I can use Raw types to write XMLAdapter, but can I use generic types. I tried reading the API ( link ), but did not even notice a clue about this.
For example map:
I want to use, something like:
@XmlJavaTypeAdapter(GenericMapAdapter<String, Double>.class)//
private final HashMap<String, Double> depWageSum = //
new HashMap<String, Double>();
to get
<depWageSum>
<entry key="RI">289.001</entry>
<entry key="VT">499.817</entry>
<entry key="HI">41.824</entry>
...
<depWageSum>
And class itself would probably look something in the lines of:
@SuppressWarnings("serial") public class GenericMapAdapter<K, V> extends XmlAdapter<GenericMapAdapter.MapType<K, V>, Map<K, V>> {
public static class MapType<K, V> {
@XmlValue protected final List<MapTypeEntry<K, V>> entry = new ArrayList<MapTypeEntry<K, V>>();
public static class MapTypeEntry<K, V> {
@XmlAttribute protected K key;
@XmlValue protected V value;
private MapTypeEntry() {};
public static <K, V> MapTypeEntry<K, V> of(final K k, final V v) {
return new MapTypeEntry<K, V>() {{this.key = k; this.value = v;}};
} } }
@Override public Map<K, V> unmarshal(final GenericMapAdapter.MapType<K, V> v) throws Exception {
return new HashMap<K, V>() {{ for (GenericMapAdapter.MapType.MapTypeEntry<K, V> myEntryType : v.entry)
this.put(myEntryType.key, myEntryType.value);}};
}
@Override public MapType<K, V> marshal(final Map<K, V> v) throws Exception {
return new GenericMapAdapter.MapType<K, V>() {{for (K key : v.keySet())
this.entry.add(MapTypeEntry.of(key, v.get(key)));}};
} }
You will not be able to do this as described. The type parameters will not be retained by the class. However you could introduce some simple subclasses that could leverage the logic from your GenericMapAdapter:
public class StringDoubleMapAdapter extends GenericMapAdapter<String, Double> {
}
Then use the adapter sub class on the property:
@XmlJavaTypeAdapter(StringDoubleMapAdapter.class)//
private final HashMap<String, Double> depWageSum = //
new HashMap<String, Double>();
For more information on XmlAdapter see:
这篇关于可以编写泛型XmlAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!