comparison src/org/tmatesoft/hg/internal/RevisionSet.java @ 649:e79cf9a8130b

Push: phase4 - update local and remote phase information
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 26 Jun 2013 20:52:38 +0200
parents 690e71d29bf6
children 3b275cc2d2aa
comparison
equal deleted inserted replaced
648:690e71d29bf6 649:e79cf9a8130b
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.util.ArrayList;
19 import java.util.Collection; 20 import java.util.Collection;
20 import java.util.Collections; 21 import java.util.Collections;
21 import java.util.HashSet; 22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
22 import java.util.Set; 25 import java.util.Set;
23 26
24 import org.tmatesoft.hg.core.Nodeid; 27 import org.tmatesoft.hg.core.Nodeid;
28 import org.tmatesoft.hg.repo.HgChangelog;
29 import org.tmatesoft.hg.repo.HgParentChildMap;
25 30
26 /** 31 /**
27 * Unmodifiable collection of revisions with handy set operations 32 * Unmodifiable collection of revisions with handy set operations
28 * 33 *
29 * @author Artem Tikhomirov 34 * @author Artem Tikhomirov
30 * @author TMate Software Ltd. 35 * @author TMate Software Ltd.
31 */ 36 */
32 public final class RevisionSet { 37 public final class RevisionSet implements Iterable<Nodeid> {
33 38
34 private final Set<Nodeid> elements; 39 private final Set<Nodeid> elements;
35 40
36 public RevisionSet(Collection<Nodeid> revisions) { 41 public RevisionSet(Collection<Nodeid> revisions) {
37 this(revisions == null ? new HashSet<Nodeid>() : new HashSet<Nodeid>(revisions)); 42 this(revisions == null ? new HashSet<Nodeid>() : new HashSet<Nodeid>(revisions));
43 } else { 48 } else {
44 elements = revisions; 49 elements = revisions;
45 } 50 }
46 } 51 }
47 52
48 public RevisionSet roots() { 53 /**
49 throw Internals.notImplemented(); 54 * elements of the set with no parents or parents not from the same set
50 } 55 */
51 56 public RevisionSet roots(HgParentChildMap<HgChangelog> ph) {
52 public RevisionSet heads() { 57 HashSet<Nodeid> copy = new HashSet<Nodeid>(elements);
53 throw Internals.notImplemented(); 58 for (Nodeid n : elements) {
59 assert ph.knownNode(n);
60 Nodeid p1 = ph.firstParent(n);
61 if (p1 != null && elements.contains(p1)) {
62 copy.remove(n);
63 continue;
64 }
65 Nodeid p2 = ph.secondParent(n);
66 if (p2 != null && elements.contains(p2)) {
67 copy.remove(n);
68 continue;
69 }
70 }
71 return copy.size() == elements.size() ? this : new RevisionSet(copy);
72 }
73
74 /**
75 * elements of the set that has no children in this set
76 */
77 public RevisionSet heads(HgParentChildMap<HgChangelog> ph) {
78 HashSet<Nodeid> copy = new HashSet<Nodeid>(elements);
79 // can't do copy.removeAll(ph.childrenOf(asList())); as actual heads are indeed children of some other node
80 for (Nodeid n : elements) {
81 assert ph.knownNode(n);
82 Nodeid p1 = ph.firstParent(n);
83 Nodeid p2 = ph.secondParent(n);
84 if (p1 != null && elements.contains(p1)) {
85 copy.remove(p1);
86 }
87 if (p2 != null && elements.contains(p2)) {
88 copy.remove(p2);
89 }
90 }
91 return copy.size() == elements.size() ? this : new RevisionSet(copy);
92 }
93
94 /**
95 * Immediate parents of the supplied children set found in this one.
96 */
97 public RevisionSet parentsOf(RevisionSet children, HgParentChildMap<HgChangelog> parentHelper) {
98 if (isEmpty()) {
99 return this;
100 }
101 if (children.isEmpty()) {
102 return children;
103 }
104 RevisionSet chRoots = children.roots(parentHelper);
105 HashSet<Nodeid> parents = new HashSet<Nodeid>();
106 for (Nodeid n : chRoots.elements) {
107 Nodeid p1 = parentHelper.firstParent(n);
108 Nodeid p2 = parentHelper.secondParent(n);
109 if (p1 != null && elements.contains(p1)) {
110 parents.add(p1);
111 }
112 if (p2 != null && elements.contains(p2)) {
113 parents.add(p2);
114 }
115 }
116 return new RevisionSet(parents);
54 } 117 }
55 118
56 public RevisionSet intersect(RevisionSet other) { 119 public RevisionSet intersect(RevisionSet other) {
57 if (isEmpty()) { 120 if (isEmpty()) {
58 return this; 121 return this;
107 170
108 public boolean isEmpty() { 171 public boolean isEmpty() {
109 return elements.isEmpty(); 172 return elements.isEmpty();
110 } 173 }
111 174
175
176 public List<Nodeid> asList() {
177 return new ArrayList<Nodeid>(elements);
178 }
179
180 public Iterator<Nodeid> iterator() {
181 return elements.iterator();
182 }
183
112 @Override 184 @Override
113 public String toString() { 185 public String toString() {
114 StringBuilder sb = new StringBuilder(); 186 StringBuilder sb = new StringBuilder();
115 sb.append('<'); 187 sb.append('<');
116 if (!isEmpty()) { 188 if (!isEmpty()) {