提交 eb44a8fc 编写于 作者: K kohsuke

improved the XML representation of CopyOnWriteMap.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3413 71c3de6d-444a-0410-be80-ed276b4c234a
上级 e02e6b65
package hudson.util;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.TreeMapConverter;
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
......@@ -120,6 +128,27 @@ public abstract class CopyOnWriteMap<K,V> implements Map<K,V> {
protected Map<K,V> copy() {
return new HashMap<K,V>(core);
}
public static class ConverterImpl extends MapConverter {
public ConverterImpl(Mapper mapper) {
super(mapper);
}
@Override
public boolean canConvert(Class type) {
return type==Hash.class;
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
return new Hash((Map) super.unmarshal(reader,context));
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
super.marshal(((Hash)source).core,writer,context);
}
}
}
/**
......@@ -146,5 +175,27 @@ public abstract class CopyOnWriteMap<K,V> implements Map<K,V> {
m.putAll(core);
return m;
}
public static class ConverterImpl extends TreeMapConverter {
public ConverterImpl(Mapper mapper) {
super(mapper);
}
@Override
public boolean canConvert(Class type) {
return type==Tree.class;
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
TreeMap tm = (TreeMap) super.unmarshal(reader,context);
return new Tree(tm,tm.comparator());
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
super.marshal(((Tree)source).core,writer,context);
}
}
}
}
......@@ -46,6 +46,7 @@ public class XStream2 extends XStream {
registerConverter(new RobustCollectionConverter(getMapper()),10);
registerConverter(new CopyOnWriteList.ConverterImpl(getMapper()),10);
registerConverter(new DescribableList.ConverterImpl(getMapper()),10);
registerConverter(new CopyOnWriteMap.Tree.ConverterImpl(getMapper()),10); // needs to override MapConverter
// this should come after all the XStream's default simpler converters,
// but before reflection-based one kicks in.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册