1 18 package org.apache.activemq.util; 19 20 26 public class LinkedNode { 27 28 protected LinkedNode next=this; 29 protected LinkedNode prev=this; 30 protected boolean tail=true; 31 32 33 public LinkedNode getHeadNode() { 34 if( isHeadNode() ) { 35 return this; 36 } 37 if( isTailNode() ) { 38 return next; 39 } 40 LinkedNode rc = prev; 41 while(!rc.isHeadNode()) { 42 rc = rc.prev; 43 } 44 return rc; 45 } 46 47 public LinkedNode getTailNode() { 48 if( isTailNode() ) { 49 return this; 50 } 51 if( isHeadNode() ) { 52 return prev; 53 } 54 LinkedNode rc = next; 55 while(!rc.isTailNode()) { 56 rc = rc.next; 57 } 58 return rc; 59 } 60 61 public LinkedNode getNext() { 62 return tail ? null : next; 63 } 64 65 public LinkedNode getPrevious() { 66 return prev.tail ? null : prev; 67 } 68 69 public boolean isHeadNode() { 70 return prev.isTailNode(); 71 } 72 73 public boolean isTailNode() { 74 return tail; 75 } 76 77 81 public LinkedNode linkAfter(LinkedNode rightHead) { 82 83 if( rightHead == this ) { 84 throw new IllegalArgumentException ("You cannot link to yourself"); 85 } 86 if( !rightHead.isHeadNode() ) { 87 throw new IllegalArgumentException ("You only insert nodes that are the first in a list"); 88 } 89 90 LinkedNode rightTail = rightHead.prev; 91 92 if( tail ) { 93 tail = false; 94 } else { 95 rightTail.tail=false; 96 } 97 98 rightHead.prev = this; rightTail.next = next; next.prev = rightTail; next = rightHead; 103 return this; 104 } 105 106 107 112 public LinkedNode linkBefore(LinkedNode leftHead) { 113 114 115 if( leftHead == this ) { 116 throw new IllegalArgumentException ("You cannot link to yourself"); 117 } 118 if( !leftHead.isHeadNode() ) { 119 throw new IllegalArgumentException ("You only insert nodes that are the first in a list"); 120 } 121 122 LinkedNode leftTail = leftHead.prev; 124 leftTail.tail = false; 125 126 leftTail.next = this; leftHead.prev = prev; prev.next = leftHead; prev = leftTail; 131 return leftHead; 132 } 133 134 137 public void unlink() { 138 if( prev==this ) { 140 return; 141 } 142 143 if( tail ) { 144 prev.tail = true; 145 } 146 147 next.prev = prev; 149 prev.next = next; 150 151 next = this; 153 prev = this; 154 tail=true; 155 } 156 157 } 158 | Popular Tags |