|
9 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | 10 | * you may not use this file except in compliance with the License. |
11 | 11 | * You may obtain a copy of the License at |
12 | | - * |
| 12 | + * |
13 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | - * |
| 14 | + * |
15 | 15 | * Unless required by applicable law or agreed to in writing, software |
16 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
28 | 28 | import java.util.Map; |
29 | 29 | import java.util.Optional; |
30 | 30 |
|
31 | | - |
32 | | -/** |
33 | | - * |
34 | | - * |
35 | | - * @author pbartolo |
36 | | - * |
37 | | - */ |
| 31 | +/** @author pbartolo */ |
38 | 32 | @SuppressWarnings("serial") |
39 | | -public class OrgChartItem implements Serializable{ |
40 | | - |
41 | | - private String name; |
42 | | - |
43 | | - private String title; |
44 | | - |
45 | | - private String className; |
46 | | - |
47 | | - private Integer id; |
48 | | - |
49 | | - private List<OrgChartItem> children = new ArrayList<>(); |
50 | | - |
51 | | - private Map<String,String> data; |
52 | | - |
53 | | - public OrgChartItem(Integer id, String name, String title) { |
54 | | - super(); |
55 | | - this.id = id; |
56 | | - this.name = name; |
57 | | - this.title = title; |
58 | | - } |
59 | | - |
60 | | - public OrgChartItem(OrgChartItem original) { |
61 | | - OrgChartItem orgChartCopy = new OrgChartItem(original.getId(), original.getName(), original.getTitle()); |
62 | | - for(OrgChartItem child : original.getChildren()) { |
63 | | - orgChartCopy.addChildren(new OrgChartItem(child)); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - /**Return the map of {@linkplain #setData(String, String) custom properties}.*/ |
68 | | - public Map<String, String> getData() { |
69 | | - return Optional.ofNullable(data).map(Collections::unmodifiableMap).orElse(Collections.emptyMap()); |
70 | | - } |
71 | | - |
72 | | - /**Add or remove a custom property. |
73 | | - * @param name the name of the custom property |
74 | | - * @param value the value of the custom property */ |
75 | | - public void setData(String name, String value) { |
76 | | - if (data==null) { |
77 | | - data = new HashMap<>(); |
78 | | - } |
79 | | - if (value!=null) { |
80 | | - data.put(name, value); |
81 | | - } else { |
82 | | - data.remove(name); |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - public String getName() { |
87 | | - return name; |
88 | | - } |
89 | | - |
90 | | - public void setName(String name) { |
91 | | - this.name = name; |
92 | | - } |
93 | | - |
94 | | - public Integer getId() { |
95 | | - return id; |
96 | | - } |
97 | | - |
98 | | - public void setId(Integer id) { |
99 | | - this.id = id; |
100 | | - } |
101 | | - |
102 | | - public String getTitle() { |
103 | | - return title; |
104 | | - } |
105 | | - |
106 | | - public void setTitle(String title) { |
107 | | - this.title = title; |
108 | | - } |
109 | | - |
110 | | - public String getClassName() { |
111 | | - return className; |
112 | | - } |
113 | | - |
114 | | - public void setClassName(String className) { |
115 | | - this.className = className; |
116 | | - } |
117 | | - |
118 | | - public List<OrgChartItem> getChildren() { |
119 | | - return children; |
120 | | - } |
121 | | - |
122 | | - public void setChildren(List<OrgChartItem> children) { |
123 | | - if (this.children!=children) { |
124 | | - this.children = new ArrayList<>(children); |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - public void addChildren(OrgChartItem item) { |
129 | | - this.children.add(item); |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - public int hashCode() { |
134 | | - final int prime = 31; |
135 | | - int result = 1; |
136 | | - result = prime * result + ((id == null) ? 0 : id.hashCode()); |
137 | | - return result; |
138 | | - } |
139 | | - |
140 | | - @Override |
141 | | - public boolean equals(Object obj) { |
142 | | - if (this == obj) |
143 | | - return true; |
144 | | - if (obj == null) |
145 | | - return false; |
146 | | - if (getClass() != obj.getClass()) |
147 | | - return false; |
148 | | - OrgChartItem other = (OrgChartItem) obj; |
149 | | - if (id == null) { |
150 | | - if (other.id != null) |
151 | | - return false; |
152 | | - } else if (!id.equals(other.id)) |
153 | | - return false; |
154 | | - return true; |
155 | | - } |
156 | | - |
157 | | - @Override |
158 | | - public String toString() { |
159 | | - StringBuilder sb = new StringBuilder(); |
160 | | - printChildren(this, sb, 0); |
161 | | - return sb.toString(); |
162 | | - } |
163 | | - |
164 | | - private void printChildren(OrgChartItem item, StringBuilder sb, int count) { |
165 | | - String tabs = count > 0 ? String.format("%-" + count + "s", "").replace(' ', '\t') : ""; |
166 | | - sb.append(tabs + item.getName() + "\n"); |
167 | | - count++; |
168 | | - for (int i = 0; i < item.getChildren().size(); i++) { |
169 | | - printChildren(item.getChildren().get(i), sb, count); |
170 | | - } |
171 | | - } |
172 | | - |
| 33 | +public class OrgChartItem implements Serializable { |
| 34 | + |
| 35 | + private String name; |
| 36 | + |
| 37 | + private String title; |
| 38 | + |
| 39 | + private String className; |
| 40 | + |
| 41 | + private Integer id; |
| 42 | + |
| 43 | + private List<OrgChartItem> children = new ArrayList<>(); |
| 44 | + |
| 45 | + private Map<String, String> data; |
| 46 | + |
| 47 | + public OrgChartItem(Integer id, String name, String title) { |
| 48 | + super(); |
| 49 | + this.id = id; |
| 50 | + this.name = name; |
| 51 | + this.title = title; |
| 52 | + } |
| 53 | + |
| 54 | + public OrgChartItem(OrgChartItem original) { |
| 55 | + OrgChartItem orgChartCopy = |
| 56 | + new OrgChartItem(original.getId(), original.getName(), original.getTitle()); |
| 57 | + for (OrgChartItem child : original.getChildren()) { |
| 58 | + orgChartCopy.addChildren(new OrgChartItem(child)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** Return the map of {@linkplain #setData(String, String) custom properties}. */ |
| 63 | + public Map<String, String> getData() { |
| 64 | + return Optional.ofNullable(data) |
| 65 | + .map(Collections::unmodifiableMap) |
| 66 | + .orElse(Collections.emptyMap()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Add or remove a custom property. |
| 71 | + * |
| 72 | + * @param name the name of the custom property |
| 73 | + * @param value the value of the custom property |
| 74 | + */ |
| 75 | + public void setData(String name, String value) { |
| 76 | + if (data == null) { |
| 77 | + data = new HashMap<>(); |
| 78 | + } |
| 79 | + if (value != null) { |
| 80 | + data.put(name, value); |
| 81 | + } else { |
| 82 | + data.remove(name); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public String getName() { |
| 87 | + return name; |
| 88 | + } |
| 89 | + |
| 90 | + public void setName(String name) { |
| 91 | + this.name = name; |
| 92 | + } |
| 93 | + |
| 94 | + public Integer getId() { |
| 95 | + return id; |
| 96 | + } |
| 97 | + |
| 98 | + public void setId(Integer id) { |
| 99 | + this.id = id; |
| 100 | + } |
| 101 | + |
| 102 | + public String getTitle() { |
| 103 | + return title; |
| 104 | + } |
| 105 | + |
| 106 | + public void setTitle(String title) { |
| 107 | + this.title = title; |
| 108 | + } |
| 109 | + |
| 110 | + public String getClassName() { |
| 111 | + return className; |
| 112 | + } |
| 113 | + |
| 114 | + public void setClassName(String className) { |
| 115 | + this.className = className; |
| 116 | + } |
| 117 | + |
| 118 | + public List<OrgChartItem> getChildren() { |
| 119 | + return children; |
| 120 | + } |
| 121 | + |
| 122 | + public void setChildren(List<OrgChartItem> children) { |
| 123 | + if (this.children != children) { |
| 124 | + this.children = new ArrayList<>(children); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void addChildren(OrgChartItem item) { |
| 129 | + this.children.add(item); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int hashCode() { |
| 134 | + final int prime = 31; |
| 135 | + int result = 1; |
| 136 | + result = prime * result + ((id == null) ? 0 : id.hashCode()); |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public boolean equals(Object obj) { |
| 142 | + if (this == obj) return true; |
| 143 | + if (obj == null) return false; |
| 144 | + if (getClass() != obj.getClass()) return false; |
| 145 | + OrgChartItem other = (OrgChartItem) obj; |
| 146 | + if (id == null) { |
| 147 | + if (other.id != null) return false; |
| 148 | + } else if (!id.equals(other.id)) return false; |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String toString() { |
| 154 | + StringBuilder sb = new StringBuilder(); |
| 155 | + printChildren(this, sb, 0); |
| 156 | + return sb.toString(); |
| 157 | + } |
| 158 | + |
| 159 | + private void printChildren(OrgChartItem item, StringBuilder sb, int count) { |
| 160 | + String tabs = count > 0 ? String.format("%-" + count + "s", "").replace(' ', '\t') : ""; |
| 161 | + sb.append(tabs + item.getName() + "\n"); |
| 162 | + count++; |
| 163 | + for (int i = 0; i < item.getChildren().size(); i++) { |
| 164 | + printChildren(item.getChildren().get(i), sb, count); |
| 165 | + } |
| 166 | + } |
173 | 167 | } |
0 commit comments