001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * created at May 31, 2008
021 */
022package org.biojava.nbio.structure.io.mmcif.model;
023
024import org.biojava.nbio.structure.Chain;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import java.lang.reflect.InvocationTargetException;
029import java.lang.reflect.Method;
030import java.util.List;
031
032/** a generic class that implements the toString method for a bean
033 *
034 * @author Andreas Prlic
035 *
036 */
037public abstract class AbstractBean {
038
039        private static final Logger logger = LoggerFactory.getLogger(AbstractBean.class);
040
041        @Override
042        @SuppressWarnings({  "unchecked" })
043        public String toString(){
044                StringBuffer buf = new StringBuffer();
045                buf.append(this.getClass().getName()).append(": ");
046                /* disabled for the moment
047
048                buf.append(" chains: " );
049                Iterator<Chain> iter = chainList.iterator();
050                while (iter.hasNext()){
051                        Chain c = iter.next();
052                        buf.append (c.getName() + " ");
053                }
054
055                 */
056                try {
057                        Class<? extends AbstractBean> c = this.getClass();
058                        Method[] methods  = c.getMethods();
059
060                        for (int i = 0; i < methods.length; i++) {
061                                Method m = methods[i];
062
063                                String name = m.getName();
064                                if ( name.substring(0,3).equals("get")) {
065
066                                        Object o  = m.invoke(this, new Object[]{});
067                                        if ( o instanceof String){
068                                                buf.append(name.substring(3, name.length())+": "+ o + " ");
069                                        }
070                                        else if ( o instanceof List){
071                                                buf.append(name.substring(3, name.length())).append(": ");
072
073                                                List<Object>lst = (List<Object>)o;
074                                                for (Object obj : lst){
075                                                        if ( obj instanceof Chain){
076                                                                continue;
077                                                        }
078                                                        buf.append(obj).append(" ");
079                                                }
080
081                                        }
082                                        else {
083                                                // ignore...
084                                        }
085                                }
086
087                        }
088
089                } catch (InvocationTargetException e){
090                        logger.error("Exception caught while producing toString",e);
091                } catch (IllegalAccessException e) {
092                        logger.error("Exception caught while producing toString",e);
093                }
094
095
096                //if ( organismScientific != null)
097                //      buf.append(" organism scientific: " + organismScientific);
098
099
100                return buf.toString();
101        }
102
103}