2 * Copyright (C) 2007 Robin Rosenberg
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
19 import java
.io
.IOException
;
20 import java
.util
.Collection
;
21 import java
.util
.Comparator
;
22 import java
.util
.Date
;
23 import java
.util
.HashMap
;
26 public class TopologicalWalker
extends Walker
{
28 @SuppressWarnings("unchecked")
29 Map
<ObjectId
, ObjectId
> collected
= new ObjectIdMap(new HashMap
<ObjectId
,ObjectId
>());
30 Map
<ObjectId
, Date
> commitTime
= new ObjectIdMap(new HashMap
<ObjectId
,ObjectId
>());
32 TopologicalSorter
<ObjectId
> topoSorter
;
33 final boolean returnAll
;
35 public boolean isReturnAll() {
39 protected TopologicalSorter
<ObjectId
>.Lane
getLane(ObjectId id
) {
40 return topoSorter
.lane
.get(id
);
43 protected TopologicalWalker(final Repository repostory
, Commit
[] starts
,
44 String
[] relativeResourceName
, boolean leafIsBlob
,
45 boolean followMainOnly
, Boolean merges
, ObjectId activeDiffLeafId
, final boolean returnAll
) {
46 super(repostory
, starts
, relativeResourceName
, leafIsBlob
,
47 followMainOnly
, merges
, activeDiffLeafId
);
48 this.returnAll
= returnAll
;
49 topoSorter
= new TopologicalSorter
<ObjectId
>() {;
51 protected boolean filter(ObjectId element
) {
52 return returnAll ?
true : collected
.containsKey(element
);
57 return returnAll ?
super.size() : collected
.size();
60 topoSorter
.setComparator(new Comparator
<ObjectId
>() {
61 public int compare(ObjectId i1
, ObjectId i2
) {
72 Date when1
= commitTime
.get(i1
);
73 Date when2
= commitTime
.get(i2
);
76 return i1
.compareTo(i2
);
81 int c
= when2
.compareTo(when1
);
83 return i1
.compareTo(i2
);
90 protected void record(ObjectId pred
, ObjectId succ
) {
93 topoSorter
.put(new TopologicalSorter
.Edge
<ObjectId
>(pred
, succ
));
94 // else topoSorter.put(pred);
98 collectSortOrder(succ
, repository
.mapCommit(succ
));
99 } catch (IOException e
) {
100 // TODO Auto-generated catch block
105 collectSortOrder(pred
, null);
107 collectSortOrder(succ
, null);
110 protected void collect(Commit commit
, int count
, int breadth
) {
111 // System.out.println("Got: "+count+" "+commit.getCommitId());
112 ObjectId commitId
= commit
.getCommitId();
113 if (commitId
== null)
114 commitId
= ObjectId
.zeroId();
115 collected
.put(commitId
, commitId
);
116 collectSortOrder(commitId
, commit
);
119 private void collectSortOrder(ObjectId commitId
, Commit commit
) {
120 if (commitId
.equals(ObjectId
.zeroId()))
121 commitTime
.put(commitId
, new Date(Long
.MAX_VALUE
));
123 if (commitId
.equals(starts
[0].getCommitId()))
124 commitTime
.put(commitId
, new Date(Long
.MAX_VALUE
-1));
127 commitTime
.put(commitId
, commit
.getAuthor().getWhen());
130 protected boolean isCancelled() {
134 public Collection
collectHistory() {
135 super.collectHistory();
136 return topoSorter
.getEntries();