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: private StringBuffer _buffer = new StringBuffer();
026:
027: private int _mode;
028: private int _attributeCount;
029:
030: private static int _defaultMode;
031:
032: public static final int INCLUDE_PACKAGE_PREFIX = 0x1;
033: public static final int INCLUDE_HASHCODE = 0x02;
034:
035: public ToStringBuilder(Object target) {
036: this (target, _defaultMode);
037: }
038:
039: public ToStringBuilder(Object target, int mode) {
040: _mode = mode;
041:
042: appendClassName(target);
043: appendHashCode(target);
044: }
045:
046: private void appendHashCode(Object target) {
047: if ((_mode & INCLUDE_HASHCODE) == 0)
048: return;
049:
050: _buffer.append('@');
051: _buffer.append(Integer.toHexString(target.hashCode()));
052: }
053:
054: private void appendClassName(Object target) {
055: String className = target.getClass().getName();
056:
057: if ((_mode & INCLUDE_PACKAGE_PREFIX) != 0) {
058: _buffer.append(className);
059: return;
060: }
061:
062: int lastdotx = className.lastIndexOf('.');
063:
064: _buffer.append(className.substring(lastdotx + 1));
065: }
066:
067: public static int getDefaultMode() {
068: return _defaultMode;
069: }
070:
071: public static void setDefaultMode(int i) {
072: _defaultMode = i;
073: }
074:
075: /**
076: * Returns the final assembled string. This may only be invoked once, after
077: * all attributes have been appended.
078: */
079: public String toString() {
080: if (_attributeCount > 0)
081: _buffer.append(']');
082:
083: String result = _buffer.toString();
084:
085: _buffer = null;
086:
087: return result;
088: }
089:
090: public void append(String attributeName, boolean value) {
091: append(attributeName, String.valueOf(value));
092: }
093:
094: public void append(String attributeName, byte value) {
095: append(attributeName, String.valueOf(value));
096:
097: }
098:
099: public void append(String attributeName, short value) {
100: append(attributeName, String.valueOf(value));
101: }
102:
103: public void append(String attributeName, int value) {
104: append(attributeName, String.valueOf(value));
105: }
106:
107: public void append(String attributeName, Object value) {
108: append(attributeName, String.valueOf(value));
109: }
110:
111: public void append(String attributeName, String value) {
112: if (_attributeCount++ == 0)
113: _buffer.append('[');
114:
115: else
116: _buffer.append(' ');
117:
118: _buffer.append(attributeName);
119:
120: _buffer.append('=');
121:
122: _buffer.append(value);
123: }
124: }
|