-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from cxcorp/refactorings
Refactorings ja streamaava parseri
- Loading branch information
Showing
15 changed files
with
466 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package osmparser; | ||
|
||
import java.io.File; | ||
|
||
public interface IGraphParser { | ||
void parseXml(File file, Graph outputGraph); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package osmparser; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class IdNormalizer { | ||
private Map<Long, Long> oldIdToNewId; | ||
private Map<Long, Node> newGraph; | ||
private Map<Long, Node> oldGraph; | ||
private long nextId; | ||
|
||
public List<Node> normalizeIds(Map<Long, Node> graph) { | ||
reset(); | ||
oldGraph = graph; | ||
createNodesWithNewIds(); | ||
addNewEdges(); | ||
return new ArrayList<>(newGraph.values()); | ||
} | ||
|
||
private void reset() { | ||
oldIdToNewId = new HashMap<>(); | ||
newGraph = new HashMap<>(); | ||
nextId = 0; | ||
} | ||
|
||
private void createNodesWithNewIds() { | ||
for (Map.Entry<Long, Node> entry : oldGraph.entrySet()) { | ||
Node n = entry.getValue(); | ||
long newId = newId(); | ||
newGraph.put(newId, new Node(newId, n.getLat(), n.getLon())); | ||
oldIdToNewId.put(entry.getKey(), newId); | ||
} | ||
} | ||
|
||
private void addNewEdges() { | ||
for (Map.Entry<Long, Node> entry : oldGraph.entrySet()) { | ||
Node oldNode = entry.getValue(); | ||
Node newNode = newGraph.get(oldIdToNewId.get(entry.getKey())); | ||
addEdges(oldNode, newNode); | ||
} | ||
} | ||
|
||
private void addEdges(Node oldNode, Node newNode) { | ||
Set<Weight> weights = oldNode.getEdges(); | ||
for (Weight weight : weights) { | ||
Long newTargetId = oldIdToNewId.get(weight.getId()); | ||
newNode.addEdgeTo(newTargetId, weight.getWeight()); | ||
} | ||
} | ||
|
||
private long newId() { | ||
return nextId++; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package osmparser; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
|
||
public class MapFileDiscoverer { | ||
|
||
public static File[] discover(String directory, String mapFilePrefix) { | ||
return new File(directory).listFiles(filterPrefixedOsmFiles(mapFilePrefix)); | ||
} | ||
|
||
private static FilenameFilter filterPrefixedOsmFiles(final String filePrefix) { | ||
if (filePrefix != null) { | ||
return new FilenameFilter() { | ||
@Override | ||
public boolean accept(File dir, String name) { | ||
return name.startsWith(filePrefix) && name.endsWith(".osm"); | ||
} | ||
}; | ||
} else { | ||
return new FilenameFilter() { | ||
@Override | ||
public boolean accept(File dir, String name) { | ||
return name.endsWith(".osm"); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/osmparser/NormalizedGraphDeepComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package osmparser; | ||
|
||
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class NormalizedGraphDeepComparator implements Comparator<List<Node>> { | ||
|
||
@Override | ||
public int compare(List<Node> o1, List<Node> o2) { | ||
if (o1.size() != o2.size()) return o1.size() - o2.size(); | ||
for (int i = 0; i < o1.size(); i++) { | ||
int nodeC = compareNode(o1.get(i), o2.get(i)); | ||
if (nodeC != 0) return nodeC; | ||
} | ||
return 0; | ||
} | ||
|
||
private int compareNode(Node o1, Node o2) { | ||
if (o1.getId() != o2.getId()) return (int) (o1.getId() - o2.getId()); | ||
Set<Weight> o1Edges = o1.getEdges(); | ||
Set<Weight> o2Edges = o2.getEdges(); | ||
if (o1Edges.size() != o2Edges.size()) return o1Edges.size() - o2Edges.size(); | ||
Iterator o1Iterator = o1Edges.iterator(); | ||
Iterator o2Iterator = o2Edges.iterator(); | ||
while (o1Iterator.hasNext()) { | ||
Weight o1Weight = (Weight) o1Iterator.next(); | ||
Weight o2Weight = (Weight) o2Iterator.next(); | ||
if (o1Weight.getId() != o2Weight.getId()) | ||
return (int) (o1Weight.getId() - o2Weight.getId()); | ||
if (o1Weight.getWeight() != o2Weight.getWeight()) | ||
return (int) (o1Weight.getWeight() - o2Weight.getWeight()); | ||
} | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.