01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */package com.bm.ejb3guice.internal;
16:
17: import java.util.LinkedHashMap;
18: import java.util.Map;
19:
20: /**
21: * Helps with {@code toString()} methods.
22: *
23: * @author crazybob@google.com (Bob Lee)
24: */
25: public class ToStringBuilder {
26:
27: // Linked hash map ensures ordering.
28: final Map<String, Object> map = new LinkedHashMap<String, Object>();
29:
30: final String name;
31:
32: public ToStringBuilder(String name) {
33: this .name = name;
34: }
35:
36: public ToStringBuilder(Class type) {
37: this .name = type.getSimpleName();
38: }
39:
40: public ToStringBuilder add(String name, Object value) {
41: if (map.put(name, value) != null) {
42: throw new RuntimeException("Duplicate names: " + name);
43: }
44: return this ;
45: }
46:
47: public String toString() {
48: return name
49: + map.toString().replace('{', '[').replace('}', ']');
50: }
51: }
|