001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.treematrix;
025    
026    import java.lang.reflect.Field;
027    import java.util.Collection;
028    import java.util.HashMap;
029    import java.util.LinkedList;
030    import java.util.List;
031    import java.util.Map;
032    
033    import org.ujmp.core.collections.ArrayIndexList;
034    
035    public class ObjectTreeMatrix extends AbstractTreeMatrix {
036            private static final long serialVersionUID = -7343649063964349539L;
037    
038            private final List<Object> objects = new ArrayIndexList<Object>();
039    
040            private Object root = null;
041    
042            private final Map<Object, List<Object>> childrenMap = new HashMap<Object, List<Object>>();
043    
044            private final Map<Object, Object> parentMap = new HashMap<Object, Object>();
045    
046            public ObjectTreeMatrix(Object o) {
047                    root = new NameAndValue("Root", o);
048                    addSuperclass("Root", o);
049                    addFields("Root", o);
050            }
051    
052            public Map<Object, Object> getParentMap() {
053                    return parentMap;
054            }
055    
056            private void addSuperclass(String name, Object o) {
057                    Class<?> superclass = o.getClass().getSuperclass();
058                    if (superclass != null && !Object.class.equals(superclass)
059                                    && !Number.class.equals(superclass)) {
060    
061                            addSuperclass("super", superclass);
062                            addFields("super", superclass);
063    
064                            addChild(new NameAndValue(name, o), new NameAndValue("super", superclass));
065                    }
066            }
067    
068            private void addFields(String name, Object o) {
069                    if (o == null) {
070                            return;
071                    }
072    
073                    NameAndValue no = new NameAndValue(name, o);
074    
075                    if (objects.contains(no)) {
076                            return;
077                    }
078    
079                    objects.add(no);
080    
081                    addSuperclass(name, o);
082    
083                    if (o instanceof Long) {
084                            return;
085                    }
086                    if (o instanceof Integer) {
087                            return;
088                    }
089                    if (o instanceof String) {
090                            return;
091                    }
092                    if (o instanceof Boolean) {
093                            return;
094                    }
095                    if (o instanceof Float) {
096                            return;
097                    }
098                    if (o instanceof Short) {
099                            return;
100                    }
101                    if (o instanceof Byte) {
102                            return;
103                    }
104    
105                    Field[] fields = o.getClass().getDeclaredFields();
106                    if (fields != null) {
107                            for (Field f : fields) {
108                                    try {
109                                            f.setAccessible(true);
110                                            Object child = f.get(o);
111    
112                                            System.out.println(f.getName() + "=" + child);
113    
114                                            addFields(f.getName(), child);
115    
116                                            addChild(new NameAndValue(name, o), new NameAndValue(f.getName(), child));
117    
118                                    } catch (Exception e) {
119                                            e.printStackTrace();
120                                    }
121                            }
122                    }
123            }
124    
125            
126            public List<Object> getChildren(Object o) {
127                    List<Object> children = childrenMap.get(o);
128                    if (children == null) {
129                            children = new LinkedList<Object>();
130                            childrenMap.put(o, children);
131                    }
132                    return children;
133            }
134    
135            
136            public Collection<Object> getObjectList() {
137                    return objects;
138            }
139    
140            
141            public Object getRoot() {
142                    return root;
143            }
144    
145            
146            public void setRoot(Object o) {
147                    root = o;
148            }
149    
150    }
151    
152    class NameAndValue {
153    
154            private String name = null;
155    
156            private Object value = null;
157    
158            public NameAndValue(String name, Object value) {
159                    this.name = name;
160                    this.value = value;
161            }
162    
163            
164            public String toString() {
165                    String s = name + " = " + value;
166                    if (s.length() > 50) {
167                            s = s.substring(0, 50) + "...";
168                    }
169                    return s;
170            }
171    
172            
173            public int hashCode() {
174                    return (name + " - " + value).hashCode();
175            }
176    
177            
178            public boolean equals(Object o) {
179                    if (o instanceof NameAndValue) {
180                            NameAndValue no = (NameAndValue) o;
181                            return (name + " - " + value).equals(no.name + " - " + no.value);
182                    } else {
183                            return false;
184                    }
185            }
186    
187    }