/** * Creates device tree from list of Packs * * @param packs collection of packs to use * @return device tree as root IRteDeviceItem */ public static IRteDeviceItem createTree(Collection<ICpPack> packs) { IRteDeviceItem root = new RteDeviceItem(); if (packs == null || packs.isEmpty()) return root; for (ICpPack pack : packs) { Collection<? extends ICpItem> devices = pack.getChildren("devices"); if (devices == null) continue; for (ICpItem item : devices) { if (!(item instanceof ICpDeviceItem)) continue; ICpDeviceItem deviceItem = (ICpDeviceItem) item; root.addDevice(deviceItem); } } return root; }
@Override public void addDevice(ICpDeviceItem item) { if (item == null) return; EDeviceHierarchyLevel eLevel = item.getLevel(); int level = eLevel.ordinal(); if (fLevel == level || fLevel == EDeviceHierarchyLevel.PROCESSOR.ordinal()) { ICpPack pack = item.getPack(); String packId = pack.getId(); if (fDevices == null) fDevices = new TreeMap<String, ICpDeviceItem>(new AlnumComparator()); ICpDeviceItem device = fDevices.get(packId); if (device == null) { fDevices.put(packId, item); } if (fLevel == EDeviceHierarchyLevel.PROCESSOR.ordinal()) return; Collection<ICpDeviceItem> subItems = item.getDeviceItems(); if (subItems != null && !subItems.isEmpty()) { for (ICpDeviceItem i : subItems) { addDevice(i); } } else if (level >= EDeviceHierarchyLevel.DEVICE.ordinal() && item.getProcessorCount() > 1) { // add processor leaves Map<String, ICpItem> processors = item.getProcessors(); for (Entry<String, ICpItem> e : processors.entrySet()) { String procName = item.getName() + ":" + e.getKey(); addDeviceItem(item, procName, EDeviceHierarchyLevel.PROCESSOR.ordinal()); } } return; } else if (fLevel == EDeviceHierarchyLevel.ROOT.ordinal()) { String vendorName = Vendor.getOfficialVendorName(item.getVendor()); addDeviceItem(item, vendorName, EDeviceHierarchyLevel.VENDOR.ordinal()); return; } else if (fLevel > level) { // should not happen if algorithm is correct return; } // other cases addDeviceItem(item, item.getName(), level); }