Stuart McCulloch | f317322 | 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 | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 23 | class Element implements Tree { |
Stuart McCulloch | f317322 | 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 | 4482c70 | 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 | f317322 | 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 | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 68 | |
Stuart McCulloch | f317322 | 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 | 4482c70 | 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 | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 100 | else |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 101 | return type.compareTo(other.getType()); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public boolean equals(Object other) { |
| 105 | if (other == null || getClass() != other.getClass()) |
| 106 | return false; |
| 107 | |
| 108 | return compareTo((Element) other) == 0; |
| 109 | } |
| 110 | |
| 111 | public int hashCode() { |
| 112 | return type.hashCode() ^ name.hashCode(); |
| 113 | } |
| 114 | |
| 115 | public Tree[] getChildren() { |
| 116 | return children; |
| 117 | } |
| 118 | |
| 119 | public Delta ifAdded() { |
| 120 | return add; |
| 121 | } |
| 122 | |
| 123 | public Delta ifRemoved() { |
| 124 | return remove; |
| 125 | } |
| 126 | |
| 127 | public Diff diff(Tree older) { |
| 128 | return new DiffImpl(this, (Element) older); |
| 129 | } |
| 130 | |
| 131 | public Element get(String name) { |
| 132 | for (Element e : children) { |
| 133 | if (e.name.equals(name)) |
| 134 | return e; |
| 135 | } |
| 136 | return null; |
| 137 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 138 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 139 | public String toString() { |
| 140 | return type + " " + name + " (" + add + "/" + remove + ")"; |
| 141 | } |
| 142 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 143 | } |