01: /*
02: * Copyright (C) 2007, 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 06. November 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.mapper;
12:
13: import com.thoughtworks.xstream.converters.Converter;
14:
15: import java.util.HashMap;
16: import java.util.Map;
17:
18: /**
19: * A Mapper for locally defined converters for a member field.
20: *
21: * @author Jörg Schaible
22: * @since 1.3
23: */
24: public class LocalConversionMapper extends MapperWrapper {
25:
26: private final Map localConverters = new HashMap();
27:
28: /**
29: * Constructs a LocalConversionMapper.
30: *
31: * @param wrapped
32: * @since 1.3
33: */
34: public LocalConversionMapper(Mapper wrapped) {
35: super (wrapped);
36: }
37:
38: public void registerLocalConverter(Class definedIn,
39: String fieldName, Converter converter) {
40: localConverters.put(new Field(definedIn, fieldName), converter);
41: }
42:
43: public Converter getLocalConverter(Class definedIn, String fieldName) {
44: return (Converter) localConverters.get(new Field(definedIn,
45: fieldName));
46: }
47:
48: private static class Field {
49: private final String name;
50: private final Class declaringClass;
51:
52: private Field(Class definedIn, String name) {
53: this .name = name;
54: this .declaringClass = definedIn;
55: }
56:
57: public String getName() {
58: return this .name;
59: }
60:
61: public Class getDeclaringClass() {
62: return this .declaringClass;
63: }
64:
65: public boolean equals(Object obj) {
66: if (obj instanceof Field) {
67: final Field field = (Field) obj;
68: return name.equals(field.getName())
69: && declaringClass.equals(field
70: .getDeclaringClass());
71: }
72: return false;
73: }
74:
75: public int hashCode() {
76: return name.hashCode() ^ declaringClass.hashCode();
77: }
78:
79: public String toString() {
80: return declaringClass.getName() + "[" + name + "]";
81: }
82: }
83: }
|