1 19 20 package org.netbeans.modules.editor.lib2.highlighting; 21 22 28 public final class OffsetGapList<E extends OffsetGapList.Offset> extends AbstractOffsetGapList<E> { 29 30 31 public OffsetGapList() { 32 } 33 34 protected int elementRawOffset(E elem) { 35 return elem.getRawOffset(); 36 } 37 38 protected void setElementRawOffset(E elem, int rawOffset) { 39 elem.setRawOffset(rawOffset); 40 } 41 42 protected int attachElement(E elem) { 43 return elem.attach(this); 44 } 45 46 protected void detachElement(E elem) { 47 elem.detach(this); 48 } 49 50 protected E getAttachedElement(Object o) { 51 if ((o instanceof Offset) && ((Offset) o).checkOwner(this)) { 52 @SuppressWarnings ("unchecked") E element = (E) o; 54 return element; 55 } else { 56 return null; 57 } 58 } 59 60 64 public static class Offset { 65 66 private int originalOrRawOffset; 67 private OffsetGapList list; 68 69 75 public Offset(int offset) { 76 this.originalOrRawOffset = offset; 77 } 78 79 89 public final int getOffset() { 90 if (list == null) { 91 return originalOrRawOffset; 92 } else { 93 return list.raw2Offset(getRawOffset()); 94 } 95 } 96 97 private int attach(OffsetGapList list) { 98 assert this.list == null : "Offset instances can only be added to one OffsetGapList."; this.list = list; 100 return originalOrRawOffset; 101 } 102 103 private void detach(OffsetGapList list) { 104 assert this.list == list : "Can't detach from a foreign list."; this.list = null; 106 } 107 108 private boolean checkOwner(OffsetGapList list) { 109 return this.list == list; 110 } 111 112 private int getRawOffset() { 113 return originalOrRawOffset; 114 } 115 116 private void setRawOffset(int rawOffset) { 117 this.originalOrRawOffset = rawOffset; 118 } 119 } } 121 | Popular Tags |