Skip to content

Commit d55ee1b

Browse files
committed
HBX-3197: Improve the implementation of classes Cfg2HbmTool, DocHelper and DocFolder
Signed-off-by: Koen Aers <[email protected]>
1 parent 823270d commit d55ee1b

File tree

3 files changed

+127
-193
lines changed

3 files changed

+127
-193
lines changed

orm/src/main/java/org/hibernate/tool/internal/export/doc/DocFolder.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ public class DocFolder {
4141
/**
4242
* The File instance.
4343
*/
44-
private File file;
44+
private final File file;
4545

4646
/**
4747
* Holds a list with the folders that are between this folder and root.
4848
*/
49-
private List<DocFolder> pathFolders = new ArrayList<DocFolder>();
49+
private final List<DocFolder> pathFolders = new ArrayList<>();
5050

5151
/**
5252
* Constructor for the root folder.
53-
*
54-
* @param pFileRoot the File that represents the root for the documentation.
5553
*/
5654
public DocFolder(File root) {
5755
super();
@@ -100,11 +98,8 @@ public DocFolder(String pName, DocFolder pParent) {
10098
+ file.getAbsolutePath() );
10199
}
102100
}
103-
104-
if (parent != null) {
105-
pathFolders.addAll(parent.getPathFolders() );
106-
pathFolders.add(this);
107-
}
101+
pathFolders.addAll(parent.getPathFolders() );
102+
pathFolders.add(this);
108103
}
109104

110105
/**

orm/src/main/java/org/hibernate/tool/internal/export/doc/DocHelper.java

Lines changed: 71 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,47 @@ public int compare(Property left, Property right) {
8282
* Map with Tables keyed by Schema FQN. The keys are Strings and the values
8383
* are Lists of Tables
8484
*/
85-
private Map<String, List<Table>> tablesBySchema =
85+
private final Map<String, List<Table>> tablesBySchema =
8686
new HashMap<String, List<Table>>();
8787

8888
/**
8989
* Map with classes keyed by package name. PackageName is String key and
9090
* values are List of POJOClass
9191
*/
92-
private Map<String, List<POJOClass>> classesByPackage =
92+
private final Map<String, List<POJOClass>> classesByPackage =
9393
new HashMap<String, List<POJOClass>>();
9494

9595
/**
9696
* Lits of all POJOClass
9797
*/
98-
private List<POJOClass> classes =
98+
private final List<POJOClass> classes =
9999
new ArrayList<POJOClass>();
100100

101101
/**
102102
* Map where the keys are column names (tableFQN.column) and the values are
103103
* lists with the Value instances where those columns referenced.
104104
*/
105-
private Map<String, List<Value>> valuesByColumn =
105+
private final Map<String, List<Value>> valuesByColumn =
106106
new HashMap<String, List<Value>>();
107107

108108
/**
109109
* Holds intances of Property keyed by Value objects.
110110
*/
111-
private Map<Value, List<Property>> propsByValue =
111+
private final Map<Value, List<Property>> propsByValue =
112112
new HashMap<Value, List<Property>>();
113113

114114
/**
115115
* List with all the tables.
116116
*/
117-
private List<Table> tables = new ArrayList<Table>();
117+
private final List<Table> tables = new ArrayList<Table>();
118118

119119
/**
120120
* Map that holds the Schema FQN for each Table. The keys are Table
121121
* instances and the values are Strings with the Schema FQN for that table.
122122
*/
123-
private Map<Table, String> tableSchemaNames = new HashMap<Table, String>();
123+
private final Map<Table, String> tableSchemaNames = new HashMap<Table, String>();
124124

125-
private Metadata metadata;
125+
private final Metadata metadata;
126126

127127
public DocHelper(Metadata metadata, Properties properties, Cfg2JavaTool cfg2JavaTool) {
128128

@@ -141,107 +141,80 @@ public DocHelper(Metadata metadata, Properties properties, Cfg2JavaTool cfg2Java
141141
if (defaultSchema == null) {
142142
defaultSchema = DEFAULT_NO_SCHEMA_NAME;
143143
}
144-
145-
Iterator<Table> tablesIter = metadata.collectTableMappings().iterator();
146144

147-
while (tablesIter.hasNext()) {
148-
Table table = tablesIter.next();
145+
for (Table table : metadata.collectTableMappings()) {
146+
if (!table.isPhysicalTable()) {
147+
continue;
148+
}
149+
tables.add(table);
149150

150-
if (!table.isPhysicalTable()) {
151-
continue;
152-
}
153-
tables.add(table);
151+
StringBuilder sb = new StringBuilder();
154152

155-
StringBuffer sb = new StringBuffer();
153+
String catalog = table.getCatalog();
154+
if (catalog == null) {
155+
catalog = defaultCatalog;
156+
}
157+
if (catalog != null) {
158+
sb.append(catalog).append(".");
159+
}
156160

157-
String catalog = table.getCatalog();
158-
if (catalog == null) {
159-
catalog = defaultCatalog;
160-
}
161-
if (catalog != null) {
162-
sb.append(catalog + ".");
163-
}
161+
String schema = table.getSchema();
162+
if (schema == null) {
163+
schema = defaultSchema;
164+
}
164165

165-
String schema = table.getSchema();
166-
if (schema == null) {
167-
schema = defaultSchema;
168-
}
166+
sb.append(schema);
169167

170-
sb.append(schema);
168+
String qualSchemaName = sb.toString();
171169

172-
String qualSchemaName = sb.toString();
170+
tableSchemaNames.put(table, qualSchemaName);
173171

174-
tableSchemaNames.put(table, qualSchemaName);
172+
List<Table> tableList = tablesBySchema.computeIfAbsent(qualSchemaName, k -> new ArrayList<>());
173+
tableList.add(table);
175174

176-
List<Table> tableList = tablesBySchema.get(qualSchemaName);
177-
if (tableList == null) {
178-
tableList = new ArrayList<Table>();
179-
tablesBySchema.put(qualSchemaName, tableList);
180-
}
181-
tableList.add(table);
182-
183-
for(Column column : table.getColumns()) {
184-
String columnFQN = getQualifiedColumnName(table, column);
185-
List<Value> values = valuesByColumn.get(columnFQN);
186-
if (values == null) {
187-
values = new ArrayList<Value>();
188-
valuesByColumn.put(columnFQN, values);
189-
}
190-
values.add(column.getValue());
191-
}
192-
}
175+
for (Column column : table.getColumns()) {
176+
String columnFQN = getQualifiedColumnName(table, column);
177+
List<Value> values = valuesByColumn.computeIfAbsent(columnFQN, k -> new ArrayList<>());
178+
values.add(column.getValue());
179+
}
180+
}
193181

194182
Map<String, Component> components = new HashMap<String, Component>();
195183

196-
Iterator<PersistentClass> classesItr = metadata.getEntityBindings().iterator();
197-
while (classesItr.hasNext()) {
198-
PersistentClass clazz = classesItr.next();
184+
for (PersistentClass clazz : metadata.getEntityBindings()) {
185+
POJOClass pojoClazz = cfg2JavaTool.getPOJOClass(clazz);
186+
ConfigurationNavigator.collectComponents(components, pojoClazz);
199187

200-
POJOClass pojoClazz = cfg2JavaTool.getPOJOClass(clazz);
201-
ConfigurationNavigator.collectComponents(components, pojoClazz);
188+
this.processClass(pojoClazz);
202189

203-
this.processClass(pojoClazz);
190+
for (Property property : clazz.getProperties()) {
191+
Value value = property.getValue();
192+
List<Property> props = propsByValue.computeIfAbsent(value, k -> new ArrayList<>());
193+
props.add(property);
194+
}
195+
}
204196

205-
for (Property property : clazz.getProperties()) {
206-
Value value = property.getValue();
207-
List<Property> props = propsByValue.get(value);
208-
if (props == null) {
209-
props = new ArrayList<Property>();
210-
propsByValue.put(value, props);
211-
}
212-
props.add(property);
213-
}
214-
}
215-
216-
Iterator<Component> iterator = components.values().iterator();
217-
while (iterator.hasNext()) {
218-
Component component = (Component) iterator.next();
219-
ComponentPOJOClass element =
220-
new ComponentPOJOClass(component, cfg2JavaTool);
221-
this.processClass(element);
222-
}
197+
for (Component component : components.values()) {
198+
ComponentPOJOClass element =
199+
new ComponentPOJOClass(component, cfg2JavaTool);
200+
this.processClass(element);
201+
}
223202
}
224203

225204
/**
226205
* Populate classes List and classesByPackage Map
227-
*
228-
* @param pojoClazz
229206
*/
230207
private void processClass(POJOClass pojoClazz) {
231208

232209
classes.add(pojoClazz);
233210
String packageName = pojoClazz.getPackageName();
234211

235-
if ("".equals(packageName)) {
212+
if (packageName == null || packageName.isEmpty()) {
236213
packageName = DEFAULT_NO_PACKAGE;
237214
}
238215

239-
List<POJOClass> classList = classesByPackage.get(packageName);
240-
if (classList == null) {
241-
classList = new ArrayList<POJOClass>();
242-
classesByPackage.put(packageName, classList);
243-
}
244-
classList.add(pojoClazz);
216+
List<POJOClass> classList = classesByPackage.computeIfAbsent(packageName, k -> new ArrayList<POJOClass>());
217+
classList.add(pojoClazz);
245218
}
246219

247220
/**
@@ -257,8 +230,6 @@ public Map<String, List<Table>> getTablesBySchema() {
257230
/**
258231
* return a Map which has List of POJOClass as value keyed by package name
259232
* as String.
260-
*
261-
* @return
262233
*/
263234
public Map<String, List<POJOClass>> getClassesByPackage() {
264235
return classesByPackage;
@@ -277,8 +248,6 @@ public List<String> getSchemas() {
277248

278249
/**
279250
* Return a sorted List of packages
280-
*
281-
* @return
282251
*/
283252
public List<String> getPackages() {
284253
List<String> packages = new ArrayList<String>(classesByPackage.keySet());
@@ -295,8 +264,7 @@ public List<String> getPackages() {
295264
* @return a list with all the tables.
296265
*/
297266
public List<Table> getTables(String schema) {
298-
List<Table> list = tablesBySchema.get(schema);
299-
return list;
267+
return tablesBySchema.get(schema);
300268
}
301269

302270
/**
@@ -309,7 +277,7 @@ public List<Table> getTables(String schema) {
309277
public List<POJOClass> getClasses(String packageName) {
310278
List<POJOClass> clazzes = classesByPackage.get(packageName);
311279
List<POJOClass> orderedClasses = new ArrayList<POJOClass>(clazzes);
312-
Collections.sort(orderedClasses, POJOCLASS_COMPARATOR);
280+
orderedClasses.sort(POJOCLASS_COMPARATOR);
313281
return orderedClasses;
314282
}
315283

@@ -324,12 +292,10 @@ public List<Table> getTables() {
324292

325293
/**
326294
* Return a sorted List of all POJOClass
327-
*
328-
* @return
329295
*/
330296
public List<POJOClass> getClasses() {
331297
List<POJOClass> orderedClasses = new ArrayList<POJOClass>(classes);
332-
Collections.sort(orderedClasses, POJOCLASS_COMPARATOR);
298+
orderedClasses.sort(POJOCLASS_COMPARATOR);
333299
return orderedClasses;
334300
}
335301

@@ -422,14 +388,14 @@ public int getLength(Column column) {
422388

423389
public int getPrecision(Column column) {
424390
return column.getPrecision() == null ?
425-
TypeUtils.DEFAULT_COLUMN_PRECISION :
426-
column.getPrecision().intValue();
391+
TypeUtils.DEFAULT_COLUMN_PRECISION :
392+
column.getPrecision();
427393
}
428394

429395
public int getScale(Column column) {
430396
return column.getScale() == null ?
431-
TypeUtils.DEFAULT_COLUMN_SCALE :
432-
column.getScale().intValue();
397+
TypeUtils.DEFAULT_COLUMN_SCALE :
398+
column.getScale();
433399
}
434400

435401
public Iterator<Column> getPrimaryKeyColumnIterator(Table table) {
@@ -469,14 +435,12 @@ public List<Value> getValues(Table table, Column column) {
469435
public List<Property> getProperties(Table table, Column column) {
470436

471437
List<Property> result = new ArrayList<Property>();
472-
Iterator<Value> values = getValues(table, column).iterator();
473-
while (values.hasNext()) {
474-
Value value = values.next();
475-
List<Property> props = propsByValue.get(value);
476-
if (props != null) {
477-
result.addAll(props);
478-
}
479-
}
438+
for (Value value : getValues(table, column)) {
439+
List<Property> props = propsByValue.get(value);
440+
if (props != null) {
441+
result.addAll(props);
442+
}
443+
}
480444
return result;
481445
}
482446

@@ -490,10 +454,8 @@ public List<Property> getProperties(Table table, Column column) {
490454
*/
491455
// TODO We haven't taken into account Array?
492456
public POJOClass getComponentPOJO(Property property) {
493-
if (property.getValue() instanceof Component) {
494-
Component comp = (Component) property.getValue();
495-
ComponentPOJOClass componentPOJOClass = new ComponentPOJOClass(comp, new Cfg2JavaTool());
496-
return componentPOJOClass;
457+
if (property.getValue() instanceof Component comp) {
458+
return new ComponentPOJOClass(comp, new Cfg2JavaTool());
497459
} else {
498460
return null;
499461
}
@@ -515,7 +477,7 @@ public List<POJOClass> getInheritanceHierarchy(POJOClass pc) {
515477

516478
public List<Property> getOrderedProperties(POJOClass pojoClass) {
517479
List<Property> orderedProperties = getAllProperties(pojoClass);
518-
Collections.sort(orderedProperties, PROPERTY_COMPARATOR);
480+
orderedProperties.sort(PROPERTY_COMPARATOR);
519481

520482
return orderedProperties;
521483
}
@@ -532,7 +494,7 @@ public List<Property> getSimpleProperties(POJOClass pojoClass) {
532494

533495
public List<Property> getOrderedSimpleProperties(POJOClass pojoClass) {
534496
List<Property> orderedProperties = getSimpleProperties(pojoClass);
535-
Collections.sort(orderedProperties, PROPERTY_COMPARATOR);
497+
orderedProperties.sort(PROPERTY_COMPARATOR);
536498
return orderedProperties;
537499
}
538500

0 commit comments

Comments
 (0)