/* MIT License Copyright (c) 2018-2019 Gang ZHANG Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package depends.format.json; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import depends.format.FileAttributes; import depends.matrix.core.DependencyDetail; import depends.matrix.core.DependencyMatrix; import depends.matrix.core.DependencyPair; import depends.matrix.core.DependencyValue; public class JDataBuilder { public JDepObject build(DependencyMatrix dependencyMatrix, FileAttributes attribute) { ArrayList files = dependencyMatrix.getNodes(); Collection dependencyPairs = dependencyMatrix.getDependencyPairs(); ArrayList cellObjects = buildCellObjects(dependencyPairs); // transform finalRes into cellObjects JDepObject depObject = new JDepObject(); depObject.setVariables(files); depObject.setName(attribute.getAttributeName()); depObject.setSchemaVersion(attribute.getSchemaVersion()); depObject.setCells(cellObjects); return depObject; } private ArrayList buildCellObjects(Collection dependencyPairs) { ArrayList cellObjects = new ArrayList(); for (DependencyPair dependencyPair : dependencyPairs) { Map valueObject = buildValueObject(dependencyPair.getDependencies()); List details = buildDetails(dependencyPair.getDependencies()); JCellObject cellObject = new JCellObject(); cellObject.setSrc(dependencyPair.getFrom()); cellObject.setDest(dependencyPair.getTo()); cellObject.setValues(valueObject); cellObject.setDetails(details); cellObjects.add(cellObject); } return cellObjects; } private List buildDetails(Collection dependencies) { List r = new ArrayList<>(); for (DependencyValue dependency : dependencies) { for (DependencyDetail detail:dependency.getDetails()) { r.add(new DetailItem(detail.getSrc(),detail.getDest(),dependency.getType())); } } if (r.size()==0) return null; return r; } private Map buildValueObject(Collection dependencies) { Map valueObject = new HashMap(); for (DependencyValue dependency : dependencies) { valueObject.put(dependency.getType(), (float) dependency.getWeight()); } return valueObject; } }