1package org.testng.collections;
2
3import org.testng.util.Strings;
4
5import java.util.List;
6
7
8public final class Objects {
9
10  private Objects() {}
11
12  private static class ValueHolder {
13    private String m_name;
14    private String m_value;
15
16    public ValueHolder(String name, String value) {
17      m_name = name;
18      m_value = value;
19    }
20
21    boolean isNull() {
22      return m_value == null;
23    }
24
25    @Override
26    public String toString() {
27      return m_name + "=" + m_value;
28    }
29
30    public boolean isEmptyString() {
31      return Strings.isNullOrEmpty(m_value);
32    }
33  }
34
35  public static class ToStringHelper {
36    private String m_className;
37    private List<ValueHolder> values = Lists.newArrayList();
38    private boolean m_omitNulls = false;
39    private boolean m_omitEmptyStrings = false;
40
41    public ToStringHelper(String className) {
42      m_className = className;
43    }
44
45    public ToStringHelper omitNulls() {
46      m_omitNulls = true;
47      return this;
48    }
49
50    public ToStringHelper omitEmptyStrings() {
51      m_omitEmptyStrings = true;
52      return this;
53    }
54
55    public ToStringHelper add(String name, String value) {
56      values.add(new ValueHolder(name, s(value)));
57      return this;
58    }
59
60    public ToStringHelper add(String name, Object value) {
61      values.add(new ValueHolder(name, s(value)));
62      return this;
63    }
64
65    private String s(Object o) {
66      return o != null
67          ? (o.toString().isEmpty() ? "\"\"" : o.toString())
68          : "{null}";
69    }
70
71    @Override
72    public String toString() {
73      StringBuilder result = new StringBuilder("[" + m_className + " ");
74      for (int i = 0; i < values.size(); i++) {
75        ValueHolder vh = values.get(i);
76        if (m_omitNulls && vh.isNull()) continue;
77        if (m_omitEmptyStrings && vh.isEmptyString()) continue;
78
79        if (i > 0) {
80          result.append(" ");
81        }
82        result.append(vh.toString());
83      }
84      result.append("]");
85
86      return result.toString();
87    }
88  }
89
90  public static ToStringHelper toStringHelper(Class<?> class1) {
91    return new ToStringHelper(class1.getSimpleName());
92  }
93
94}
95