|
| 1 | +package difflib; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Holds the information about the part of text involved in the diff process |
| 7 | + * |
| 8 | + * <p>Text is represented as <code>Object[]</code> because |
| 9 | + * the diff engine is capable of handling more than plain ascci. In fact, |
| 10 | + * arrays or lists of any type that implements |
| 11 | + * {@link java.lang.Object#hashCode hashCode()} and |
| 12 | + * {@link java.lang.Object#equals equals()} |
| 13 | + * correctly can be subject to differencing using this |
| 14 | + * library.</p> |
| 15 | + * |
| 16 | + * @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a> |
| 17 | + */ |
| 18 | +public class Chunk { |
| 19 | + private int position; |
| 20 | + private int size; |
| 21 | + private List<?> lines; |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates a chunk and saves a copy of affected lines |
| 25 | + * |
| 26 | + * @param position the start position |
| 27 | + * @param size the size of a Chunk |
| 28 | + * @param lines the affected lines |
| 29 | + */ |
| 30 | + public Chunk(int position, int size, List<?> lines) { |
| 31 | + this.position = position; |
| 32 | + this.size = size; |
| 33 | + this.lines = lines; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a chunk and saves a copy of affected lines |
| 38 | + * |
| 39 | + * @param position the start position |
| 40 | + * @param size the size of a Chunk |
| 41 | + * @param lines the affected lines |
| 42 | + */ |
| 43 | + public Chunk(int position, int size, Object[] lines) { |
| 44 | + this.position = position; |
| 45 | + this.size = size; |
| 46 | + this.lines = Arrays.asList(lines); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return the start position of chunk in the text |
| 51 | + */ |
| 52 | + public int getPosition() { |
| 53 | + return position; |
| 54 | + } |
| 55 | + /** |
| 56 | + * @param position the start position to set |
| 57 | + */ |
| 58 | + public void setPosition(int position) { |
| 59 | + this.position = position; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return the size of Chunk (size of affected lines) |
| 64 | + */ |
| 65 | + public int getSize() { |
| 66 | + return size; |
| 67 | + } |
| 68 | + /** |
| 69 | + * @param size the size of affected lines to set |
| 70 | + */ |
| 71 | + public void setSize(int size) { |
| 72 | + this.size = size; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return the affected lines |
| 77 | + */ |
| 78 | + public List<?> getLines() { |
| 79 | + return lines; |
| 80 | + } |
| 81 | + /** |
| 82 | + * @param lines the affected lines to set |
| 83 | + */ |
| 84 | + public void setLines(List<?> lines) { |
| 85 | + this.lines = lines; |
| 86 | + } |
| 87 | + |
| 88 | + /* (non-Javadoc) |
| 89 | + * @see java.lang.Object#hashCode() |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public int hashCode() { |
| 93 | + final int prime = 31; |
| 94 | + int result = 1; |
| 95 | + result = prime * result + ((lines == null) ? 0 : lines.hashCode()); |
| 96 | + result = prime * result + position; |
| 97 | + result = prime * result + size; |
| 98 | + return result; |
| 99 | + } |
| 100 | + |
| 101 | + /* (non-Javadoc) |
| 102 | + * @see java.lang.Object#equals(java.lang.Object) |
| 103 | + */ |
| 104 | + @Override |
| 105 | + public boolean equals(Object obj) { |
| 106 | + if (this == obj) |
| 107 | + return true; |
| 108 | + if (obj == null) |
| 109 | + return false; |
| 110 | + if (getClass() != obj.getClass()) |
| 111 | + return false; |
| 112 | + Chunk other = (Chunk) obj; |
| 113 | + if (lines == null) { |
| 114 | + if (other.lines != null) |
| 115 | + return false; |
| 116 | + } else if (!lines.equals(other.lines)) |
| 117 | + return false; |
| 118 | + if (position != other.position) |
| 119 | + return false; |
| 120 | + if (size != other.size) |
| 121 | + return false; |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return "[position: " + position + ", size: " + size + ", lines: " + lines + "]"; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments