Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/RevisionSet.java @ 648:690e71d29bf6
Introduced RevisionSet to ease update of phase roots on push
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Tue, 25 Jun 2013 20:48:37 +0200 |
| parents | |
| children | e79cf9a8130b |
comparison
equal
deleted
inserted
replaced
| 647:c75297c17867 | 648:690e71d29bf6 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2013 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program 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 | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.internal; | |
| 18 | |
| 19 import java.util.Collection; | |
| 20 import java.util.Collections; | |
| 21 import java.util.HashSet; | |
| 22 import java.util.Set; | |
| 23 | |
| 24 import org.tmatesoft.hg.core.Nodeid; | |
| 25 | |
| 26 /** | |
| 27 * Unmodifiable collection of revisions with handy set operations | |
| 28 * | |
| 29 * @author Artem Tikhomirov | |
| 30 * @author TMate Software Ltd. | |
| 31 */ | |
| 32 public final class RevisionSet { | |
| 33 | |
| 34 private final Set<Nodeid> elements; | |
| 35 | |
| 36 public RevisionSet(Collection<Nodeid> revisions) { | |
| 37 this(revisions == null ? new HashSet<Nodeid>() : new HashSet<Nodeid>(revisions)); | |
| 38 } | |
| 39 | |
| 40 private RevisionSet(HashSet<Nodeid> revisions) { | |
| 41 if (revisions.isEmpty()) { | |
| 42 elements = Collections.<Nodeid>emptySet(); | |
| 43 } else { | |
| 44 elements = revisions; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 public RevisionSet roots() { | |
| 49 throw Internals.notImplemented(); | |
| 50 } | |
| 51 | |
| 52 public RevisionSet heads() { | |
| 53 throw Internals.notImplemented(); | |
| 54 } | |
| 55 | |
| 56 public RevisionSet intersect(RevisionSet other) { | |
| 57 if (isEmpty()) { | |
| 58 return this; | |
| 59 } | |
| 60 if (other.isEmpty()) { | |
| 61 return other; | |
| 62 } | |
| 63 HashSet<Nodeid> copy = new HashSet<Nodeid>(elements); | |
| 64 copy.retainAll(other.elements); | |
| 65 return copy.size() == elements.size() ? this : new RevisionSet(copy); | |
| 66 } | |
| 67 | |
| 68 public RevisionSet subtract(RevisionSet other) { | |
| 69 if (isEmpty() || other.isEmpty()) { | |
| 70 return this; | |
| 71 } | |
| 72 HashSet<Nodeid> copy = new HashSet<Nodeid>(elements); | |
| 73 copy.removeAll(other.elements); | |
| 74 return copy.size() == elements.size() ? this : new RevisionSet(copy); | |
| 75 } | |
| 76 | |
| 77 public RevisionSet union(RevisionSet other) { | |
| 78 if (isEmpty()) { | |
| 79 return other; | |
| 80 } | |
| 81 if (other.isEmpty()) { | |
| 82 return this; | |
| 83 } | |
| 84 HashSet<Nodeid> copy = new HashSet<Nodeid>(elements); | |
| 85 copy.addAll(other.elements); | |
| 86 return new RevisionSet(copy); | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * A ^ B := (A\B).union(B\A) | |
| 91 * A ^ B := A.union(B) \ A.intersect(B) | |
| 92 */ | |
| 93 public RevisionSet symmetricDifference(RevisionSet other) { | |
| 94 if (isEmpty()) { | |
| 95 return this; | |
| 96 } | |
| 97 if (other.isEmpty()) { | |
| 98 return other; | |
| 99 } | |
| 100 HashSet<Nodeid> copyA = new HashSet<Nodeid>(elements); | |
| 101 HashSet<Nodeid> copyB = new HashSet<Nodeid>(other.elements); | |
| 102 copyA.removeAll(other.elements); | |
| 103 copyB.removeAll(elements); | |
| 104 copyA.addAll(copyB); | |
| 105 return new RevisionSet(copyA); | |
| 106 } | |
| 107 | |
| 108 public boolean isEmpty() { | |
| 109 return elements.isEmpty(); | |
| 110 } | |
| 111 | |
| 112 @Override | |
| 113 public String toString() { | |
| 114 StringBuilder sb = new StringBuilder(); | |
| 115 sb.append('<'); | |
| 116 if (!isEmpty()) { | |
| 117 sb.append(elements.size()); | |
| 118 sb.append(':'); | |
| 119 } | |
| 120 for (Nodeid n : elements) { | |
| 121 sb.append(n.shortNotation()); | |
| 122 sb.append(','); | |
| 123 } | |
| 124 if (sb.length() > 1) { | |
| 125 sb.setCharAt(sb.length() - 1, '>'); | |
| 126 } else { | |
| 127 sb.append('>'); | |
| 128 } | |
| 129 return sb.toString(); | |
| 130 } | |
| 131 | |
| 132 @Override | |
| 133 public boolean equals(Object obj) { | |
| 134 if (false == obj instanceof RevisionSet) { | |
| 135 return false; | |
| 136 } | |
| 137 return elements.equals(((RevisionSet) obj).elements); | |
| 138 } | |
| 139 | |
| 140 @Override | |
| 141 public int hashCode() { | |
| 142 return elements.hashCode(); | |
| 143 } | |
| 144 } |
