crazyboblee | b8cf1e5 | 2007-02-02 21:48:16 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 | */ |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 16 | |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 17 | package com.google.inject.internal.util; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 18 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 19 | import java.util.LinkedHashMap; |
kevinb9n | 2394ca6 | 2007-04-20 14:33:40 +0000 | [diff] [blame] | 20 | import java.util.Map; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Helps with {@code toString()} methods. |
| 24 | * |
| 25 | * @author crazybob@google.com (Bob Lee) |
| 26 | */ |
| 27 | public class ToStringBuilder { |
| 28 | |
| 29 | // Linked hash map ensures ordering. |
| 30 | final Map<String, Object> map = new LinkedHashMap<String, Object>(); |
| 31 | |
| 32 | final String name; |
| 33 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 34 | public ToStringBuilder(Class type) { |
| 35 | this.name = type.getSimpleName(); |
| 36 | } |
| 37 | |
| 38 | public ToStringBuilder add(String name, Object value) { |
| 39 | if (map.put(name, value) != null) { |
| 40 | throw new RuntimeException("Duplicate names: " + name); |
| 41 | } |
| 42 | return this; |
| 43 | } |
| 44 | |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame] | 45 | @Override public String toString() { |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 46 | return name + map.toString().replace('{', '[').replace('}', ']'); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 47 | } |
| 48 | } |