KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > inheritance > MapFieldInheriter


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 18.10.2004
33  */

34 package com.nightlabs.inheritance;
35
36 import java.lang.reflect.Field JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * @author Marco Schulze - marco at nightlabs dot de
42  */

43 public class MapFieldInheriter implements FieldInheriter
44 {
45     /**
46      * @see com.nightlabs.inheritance.FieldInheriter#copyFieldValue(com.nightlabs.inheritance.Inheritable, com.nightlabs.inheritance.Inheritable, java.lang.Class, java.lang.Class, java.lang.reflect.Field, com.nightlabs.inheritance.FieldMetaData, com.nightlabs.inheritance.FieldMetaData)
47      */

48     public void copyFieldValue(Inheritable mother, Inheritable child, Class JavaDoc motherClass, Class JavaDoc childClass, Field JavaDoc field, FieldMetaData motherFieldMetaData, FieldMetaData childFieldMetaData)
49     {
50         if (!Map JavaDoc.class.isAssignableFrom(field.getType()))
51             throw new ClassCastException JavaDoc("field \""+field.getName()+"\" of class \""+motherClass.getName()+"\" is not a Map!");
52
53         if (!(motherFieldMetaData instanceof MapFieldMetaData))
54             throw new ClassCastException JavaDoc("motherFieldMetaData for field \""+field.getName()+"\" of class \""+motherClass.getName()+"\" is not an instance of MapFieldMetaData!");
55
56         if (!(childFieldMetaData instanceof MapFieldMetaData))
57             throw new ClassCastException JavaDoc("childFieldMetaData for field \""+field.getName()+"\" of class \""+childClass.getName()+"\" is not an instance of MapFieldMetaData!");
58         
59         MapFieldMetaData _motherFieldMetaData = (MapFieldMetaData)motherFieldMetaData;
60         MapFieldMetaData _childFieldMetaData = (MapFieldMetaData)childFieldMetaData;
61
62         Map JavaDoc motherMap;
63         Map JavaDoc childMap;
64         try {
65             field.setAccessible(true);
66             motherMap = (Map JavaDoc)field.get(mother);
67             childMap = (Map JavaDoc)field.get(child);
68         } catch (IllegalArgumentException JavaDoc e) {
69             throw e;
70         } catch (IllegalAccessException JavaDoc e) {
71             throw new RuntimeException JavaDoc(e);
72         }
73
74 // delete all values that do not exist anymore in the mother...
75
for (Iterator JavaDoc it = childMap.keySet().iterator(); it.hasNext(); ) {
76             Object JavaDoc key = it.next();
77             if (!motherMap.containsKey(key)) {
78                 MapEntryMetaData c_memd = _childFieldMetaData.getMapEntryMetaData(key);
79                 if (c_memd == null)
80                     throw new NullPointerException JavaDoc("childFieldMetaData.getMapEntryMetaData(\""+key+"\") field \""+field.getName()+"\" of class \""+childClass.getName()+"\" returned null!");
81
82                 if (c_memd.isValueInherited()) {
83                     it.remove();
84                     _childFieldMetaData.removeMapEntryMetaData(key);
85                 }
86             } // if (!motherMap.containsKey(key)) {
87
} // for (Iterator it = childMap.entrySet().iterator(); it.hasNext(); ) {
88

89 // and add/set all values that need to be copied
90
for (Iterator JavaDoc it = motherMap.entrySet().iterator(); it.hasNext(); ) {
91             Map.Entry JavaDoc me = (Map.Entry JavaDoc)it.next();
92             Object JavaDoc key = me.getKey();
93             Object JavaDoc value = me.getValue();
94             MapEntryMetaData m_memd = _motherFieldMetaData.getMapEntryMetaData(key);
95             if (m_memd == null)
96                 throw new NullPointerException JavaDoc("motherFieldMetaData.getMapEntryMetaData(\""+key+"\") field \""+field.getName()+"\" of class \""+motherClass.getName()+"\" returned null!");
97             MapEntryMetaData c_memd = _childFieldMetaData.getMapEntryMetaData(key);
98             if (c_memd == null)
99                 throw new NullPointerException JavaDoc("childFieldMetaData.getMapEntryMetaData(\""+key+"\") field \""+field.getName()+"\" of class \""+childClass.getName()+"\" returned null!");
100
101             if (c_memd.isValueInherited())
102                 childMap.put(key, value);
103         } // for (Iterator it = motherMap.entrySet().iterator(); it.hasNext(); ) {
104

105     }
106 }
107
Popular Tags