|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ToStringBuilder.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.util;
|
|
16 |
|
|
17 |
/**
|
|
18 |
* A simple replacement for the more involved version in commons-lang; this is used
|
|
19 |
* to help construct the description string returned by an object's
|
|
20 |
* <code>toString()</code> method.
|
|
21 |
*
|
|
22 |
* @author Howard Lewis Ship
|
|
23 |
*/
|
|
24 |
public class ToStringBuilder |
|
25 |
{ |
|
26 |
private StringBuffer _buffer = new StringBuffer(); |
|
27 |
|
|
28 |
private int _mode; |
|
29 |
private int _attributeCount; |
|
30 |
|
|
31 |
private static int _defaultMode; |
|
32 |
|
|
33 |
public static final int INCLUDE_PACKAGE_PREFIX = 0x1; |
|
34 |
public static final int INCLUDE_HASHCODE = 0x02; |
|
35 |
|
|
36 | 748 |
public ToStringBuilder(Object target)
|
37 |
{ |
|
38 | 748 |
this(target, _defaultMode);
|
39 |
} |
|
40 |
|
|
41 | 751 |
public ToStringBuilder(Object target, int mode) |
42 |
{ |
|
43 | 751 |
_mode = mode; |
44 |
|
|
45 | 751 |
appendClassName(target); |
46 | 750 |
appendHashCode(target); |
47 |
} |
|
48 |
|
|
49 | 750 |
private void appendHashCode(Object target) |
50 |
{ |
|
51 | 750 |
if ((_mode & INCLUDE_HASHCODE) == 0)
|
52 | 747 |
return;
|
53 |
|
|
54 | 3 |
_buffer.append('@'); |
55 | 3 |
_buffer.append(Integer.toHexString(target.hashCode())); |
56 |
} |
|
57 |
|
|
58 | 751 |
private void appendClassName(Object target) |
59 |
{ |
|
60 | 751 |
String className = target.getClass().getName(); |
61 |
|
|
62 | 750 |
if ((_mode & INCLUDE_PACKAGE_PREFIX) != 0)
|
63 |
{ |
|
64 | 2 |
_buffer.append(className); |
65 | 2 |
return;
|
66 |
} |
|
67 |
|
|
68 | 748 |
int lastdotx = className.lastIndexOf('.');
|
69 |
|
|
70 | 748 |
_buffer.append(className.substring(lastdotx + 1)); |
71 |
} |
|
72 |
|
|
73 | 13 |
public static int getDefaultMode() |
74 |
{ |
|
75 | 13 |
return _defaultMode;
|
76 |
} |
|
77 |
|
|
78 | 14 |
public static void setDefaultMode(int i) |
79 |
{ |
|
80 | 14 |
_defaultMode = i; |
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* Returns the final assembled string. This may only be invoked once, after
|
|
85 |
* all attributes have been appended.
|
|
86 |
*/
|
|
87 | 751 |
public String toString()
|
88 |
{ |
|
89 | 751 |
if (_attributeCount > 0)
|
90 | 746 |
_buffer.append(']'); |
91 |
|
|
92 | 751 |
String result = _buffer.toString(); |
93 |
|
|
94 | 750 |
_buffer = null;
|
95 |
|
|
96 | 750 |
return result;
|
97 |
} |
|
98 |
|
|
99 | 2 |
public void append(String attributeName, boolean value) |
100 |
{ |
|
101 | 2 |
append(attributeName, String.valueOf(value)); |
102 |
} |
|
103 |
|
|
104 | 1 |
public void append(String attributeName, byte value) |
105 |
{ |
|
106 | 1 |
append(attributeName, String.valueOf(value)); |
107 |
|
|
108 |
} |
|
109 | 1 |
public void append(String attributeName, short value) |
110 |
{ |
|
111 | 1 |
append(attributeName, String.valueOf(value)); |
112 |
} |
|
113 |
|
|
114 | 1 |
public void append(String attributeName, int value) |
115 |
{ |
|
116 | 1 |
append(attributeName, String.valueOf(value)); |
117 |
} |
|
118 |
|
|
119 | 555 |
public void append(String attributeName, Object value) |
120 |
{ |
|
121 | 555 |
append(attributeName, String.valueOf(value)); |
122 |
} |
|
123 |
|
|
124 | 2032 |
public void append(String attributeName, String value) |
125 |
{ |
|
126 | 2032 |
if (_attributeCount++ == 0)
|
127 | 746 |
_buffer.append('['); |
128 |
|
|
129 |
else
|
|
130 | 1286 |
_buffer.append(' '); |
131 |
|
|
132 | 2032 |
_buffer.append(attributeName); |
133 |
|
|
134 | 2032 |
_buffer.append('='); |
135 |
|
|
136 | 2032 |
_buffer.append(value); |
137 |
} |
|
138 |
} |
|
139 |
|
|