001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.util;
016    
017    /**
018     * A simple replacement for the more involved version in commons-lang; this is used
019     * to help construct the description string returned by an object's
020     * <code>toString()</code> method.
021     *
022     * @author Howard Lewis Ship
023     */
024    public class ToStringBuilder
025    {
026        private StringBuffer _buffer = new StringBuffer();
027    
028        private int _mode;
029        private int _attributeCount;
030    
031        private static int _defaultMode;
032    
033        public static final int INCLUDE_PACKAGE_PREFIX = 0x1;
034        public static final int INCLUDE_HASHCODE = 0x02;
035    
036        public ToStringBuilder(Object target)
037        {
038            this(target, _defaultMode);
039        }
040    
041        public ToStringBuilder(Object target, int mode)
042        {
043            _mode = mode;
044    
045            appendClassName(target);
046            appendHashCode(target);
047        }
048    
049        private void appendHashCode(Object target)
050        {
051            if ((_mode & INCLUDE_HASHCODE) == 0)
052                return;
053    
054            _buffer.append('@');
055            _buffer.append(Integer.toHexString(target.hashCode()));
056        }
057    
058        private void appendClassName(Object target)
059        {
060            String className = target.getClass().getName();
061    
062            if ((_mode & INCLUDE_PACKAGE_PREFIX) != 0)
063            {
064                _buffer.append(className);
065                return;
066            }
067    
068            int lastdotx = className.lastIndexOf('.');
069    
070            _buffer.append(className.substring(lastdotx + 1));
071        }
072    
073        public static int getDefaultMode()
074        {
075            return _defaultMode;
076        }
077    
078        public static void setDefaultMode(int i)
079        {
080            _defaultMode = i;
081        }
082    
083        /**
084         * Returns the final assembled string. This may only be invoked once, after
085         * all attributes have been appended.
086         */
087        public String toString()
088        {
089            if (_attributeCount > 0)
090                _buffer.append(']');
091    
092            String result = _buffer.toString();
093    
094            _buffer = null;
095    
096            return result;
097        }
098    
099        public void append(String attributeName, boolean value)
100        {
101            append(attributeName, String.valueOf(value));
102        }
103    
104        public void append(String attributeName, byte value)
105        {
106            append(attributeName, String.valueOf(value));
107    
108        }
109        public void append(String attributeName, short value)
110        {
111            append(attributeName, String.valueOf(value));
112        }
113    
114        public void append(String attributeName, int value)
115        {
116            append(attributeName, String.valueOf(value));
117        }
118    
119        public void append(String attributeName, Object value)
120        {
121            append(attributeName, String.valueOf(value));
122        }
123    
124        public void append(String attributeName, String value)
125        {
126            if (_attributeCount++ == 0)
127                _buffer.append('[');
128    
129            else
130                _buffer.append(' ');
131    
132            _buffer.append(attributeName);
133    
134            _buffer.append('=');
135    
136            _buffer.append(value);
137        }
138    }