Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.differ; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.bnd.service.diff.*; |
| 6 | |
| 7 | /** |
| 8 | * An element can be compared to another element of the same type. Elements with |
| 9 | * the same name and same place in the hierarchy should have the same type. The |
| 10 | * idea is that for a certain resource type you create an element (Structured or |
| 11 | * Leaf). This process is done for the newer and older resource. |
| 12 | * <p> |
| 13 | * A Leaf type has a value, comparison is rather simple in this case. |
| 14 | * <p> |
| 15 | * A Structured type has named children. The comparison between the newer and |
| 16 | * older child elements is then done on their name. Two elements with the same |
| 17 | * name are then matched. |
| 18 | * <p> |
| 19 | * The classes are prepared for extension but so far it turned out to be |
| 20 | * unnecessary. |
| 21 | */ |
| 22 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | class Element implements Tree { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | final static Element[] EMPTY = new Element[0]; |
| 25 | final Type type; |
| 26 | final String name; |
| 27 | final Delta add; |
| 28 | final Delta remove; |
| 29 | final String comment; |
| 30 | final Element[] children; |
| 31 | |
| 32 | Element(Type type, String name) { |
| 33 | this(type, name, null, Delta.MINOR, Delta.MAJOR, null); |
| 34 | } |
| 35 | |
| 36 | Element(Type type, String name, Element... children) { |
| 37 | this(type, name, Arrays.asList(children), Delta.MINOR, Delta.MAJOR, null); |
| 38 | } |
| 39 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 40 | Element(Type type, String name, Collection< ? extends Element> children, Delta add, Delta remove, String comment) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 41 | this.type = type; |
| 42 | this.name = name; |
| 43 | this.add = add; |
| 44 | this.remove = remove; |
| 45 | this.comment = comment; |
| 46 | if (children != null && children.size() > 0) { |
| 47 | this.children = children.toArray(new Element[children.size()]); |
| 48 | Arrays.sort(this.children); |
| 49 | } else |
| 50 | this.children = EMPTY; |
| 51 | } |
| 52 | |
| 53 | public Element(Data data) { |
| 54 | this.name = data.name; |
| 55 | this.type = data.type; |
| 56 | this.comment = data.comment; |
| 57 | this.add = data.add; |
| 58 | this.remove = data.rem; |
| 59 | if (data.children == null) |
| 60 | children = EMPTY; |
| 61 | else { |
| 62 | this.children = new Element[data.children.length]; |
| 63 | for (int i = 0; i < children.length; i++) |
| 64 | children[i] = new Element(data.children[i]); |
| 65 | Arrays.sort(this.children); |
| 66 | } |
| 67 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | public Data serialize() { |
| 70 | Data data = new Data(); |
| 71 | data.type = this.type; |
| 72 | data.name = this.name; |
| 73 | data.add = this.add; |
| 74 | data.rem = this.remove; |
| 75 | data.comment = this.comment; |
| 76 | if (children.length != 0) { |
| 77 | data.children = new Data[children.length]; |
| 78 | for (int i = 0; i < children.length; i++) { |
| 79 | data.children[i] = children[i].serialize(); |
| 80 | } |
| 81 | } |
| 82 | return data; |
| 83 | } |
| 84 | |
| 85 | public Type getType() { |
| 86 | return type; |
| 87 | } |
| 88 | |
| 89 | public String getName() { |
| 90 | return name; |
| 91 | } |
| 92 | |
| 93 | String getComment() { |
| 94 | return comment; |
| 95 | } |
| 96 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 97 | public int compareTo(Tree other) { |
| 98 | if (type == other.getType()) |
| 99 | return name.compareTo(other.getName()); |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 100 | return type.compareTo(other.getType()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | public boolean equals(Object other) { |
| 104 | if (other == null || getClass() != other.getClass()) |
| 105 | return false; |
| 106 | |
| 107 | return compareTo((Element) other) == 0; |
| 108 | } |
| 109 | |
| 110 | public int hashCode() { |
| 111 | return type.hashCode() ^ name.hashCode(); |
| 112 | } |
| 113 | |
| 114 | public Tree[] getChildren() { |
| 115 | return children; |
| 116 | } |
| 117 | |
| 118 | public Delta ifAdded() { |
| 119 | return add; |
| 120 | } |
| 121 | |
| 122 | public Delta ifRemoved() { |
| 123 | return remove; |
| 124 | } |
| 125 | |
| 126 | public Diff diff(Tree older) { |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 127 | return new DiffImpl(this, older); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | public Element get(String name) { |
| 131 | for (Element e : children) { |
| 132 | if (e.name.equals(name)) |
| 133 | return e; |
| 134 | } |
| 135 | return null; |
| 136 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 137 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 138 | public String toString() { |
| 139 | return type + " " + name + " (" + add + "/" + remove + ")"; |
| 140 | } |
| 141 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 142 | } |