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