001: /*
002: * Created on 07-Oct-2004
003: *
004: */
005: package com.jofti.btree;
006:
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.List;
010:
011: import com.jofti.exception.JoftiException;
012: import com.jofti.oswego.concurrent.ReadWriteLock;
013:
014: /**
015: * The implementation class for the IResultNode.
016: *
017: * @author Steve Woodcock
018: * @version 1.0<br>
019: */
020: public class ResultNode implements INode, IResultNode {
021:
022: private Object[] entries = null;
023: private Comparable rightValue;
024: private int entryNumber;
025: boolean isDeleted;
026: NodeLink link;
027: private INode node;
028:
029: ResultNode(INode node) {
030: entries = node.getEntries();
031: if (entries == null) {
032: throw new RuntimeException("entries are null for "
033: + node.getRightValue());
034: }
035:
036: entryNumber = node.getEntryNumber();
037: rightValue = node.getRightValue();
038: isDeleted = node.isDeleted();
039: link = node.getLinkNode();
040: this .node = node;
041: }
042:
043: /* (non-Javadoc)
044: * @see com.jofti.btree.Node#getRightValue()
045: */
046: public Comparable getRightValue() {
047: return rightValue;
048: }
049:
050: /* (non-Javadoc)
051: * @see com.jofti.btree.Node#setRightValue(java.lang.Comparable)
052: */
053: public void setRightValue(Comparable value) {
054: throw new UnsupportedOperationException(
055: "Modification of node not supported");
056:
057: }
058:
059: /* (non-Javadoc)
060: * @see com.jofti.btree.Node#contains(java.lang.Comparable)
061: */
062: public boolean contains(Comparable value) {
063: if (value == null) {
064: return false;
065: }
066:
067: return rightValue.compareTo(value) >= 0;
068: }
069:
070: /* (non-Javadoc)
071: * @see com.jofti.btree.Node#insertEntry(com.jofti.btree.NodeEntry)
072: */
073: public Object[] insertEntry(NodeEntry entry) throws JoftiException {
074: throw new UnsupportedOperationException(
075: "Modification of node not supported");
076:
077: }
078:
079: /* (non-Javadoc)
080: * @see com.jofti.btree.Node#deleteEntry(com.jofti.btree.NodeEntry)
081: */
082: public boolean deleteEntry(NodeEntry entry) {
083: throw new UnsupportedOperationException(
084: "Modification of node not supported");
085:
086: }
087:
088: /* (non-Javadoc)
089: * @see com.jofti.btree.Node#split()
090: */
091: public List split() {
092: throw new UnsupportedOperationException(
093: "Modification of node not supported");
094:
095: }
096:
097: /* (non-Javadoc)
098: * @see com.jofti.btree.Node#splitNode()
099: */
100: public Node splitNode(Object[] temp) {
101:
102: throw new UnsupportedOperationException(
103: "Modification of node not supported");
104:
105: }
106:
107: /* (non-Javadoc)
108: * @see com.jofti.btree.Node#getEntryNumber()
109: */
110: public int getEntryNumber() {
111:
112: return entryNumber;
113: }
114:
115: /* (non-Javadoc)
116: * @see com.jofti.btree.Node#isUnderFull()
117: */
118: public boolean isUnderFull() {
119:
120: throw new UnsupportedOperationException(
121: "Modification of node not supported");
122:
123: }
124:
125: /* (non-Javadoc)
126: * @see com.jofti.btree.Node#isEmpty()
127: */
128: public boolean isEmpty() {
129:
130: return entryNumber == 0;
131: }
132:
133: /* (non-Javadoc)
134: * @see com.jofti.btree.Node#isDeleted()
135: */
136: public boolean isDeleted() {
137:
138: return isDeleted;
139: }
140:
141: /* (non-Javadoc)
142: * @see com.jofti.btree.Node#setDeleted(boolean)
143: */
144: public void setDeleted(boolean deleted) {
145: throw new UnsupportedOperationException(
146: "Modification of node not supported");
147:
148: }
149:
150: /* (non-Javadoc)
151: * @see com.jofti.btree.Node#getEntries()
152: */
153: public Object[] getEntries() {
154:
155: return entries;
156:
157: }
158:
159: /* (non-Javadoc)
160: * @see com.jofti.btree.Node#setEntries(java.util.List)
161: */
162: public void setEntries(List entries) {
163: throw new UnsupportedOperationException(
164: "Modification of node not supported");
165:
166: }
167:
168: /* (non-Javadoc)
169: * @see com.jofti.btree.Node#getLinkNode()
170: */
171: public NodeLink getLinkNode() {
172:
173: return new ResultNodeLink(link);
174: }
175:
176: public IResultNode getNextNode() {
177: return new ResultNode(link.getNode());
178: }
179:
180: /* (non-Javadoc)
181: * @see com.jofti.btree.Node#setLinkNode(com.jofti.btree.NodeLink)
182: */
183: public void setLinkNode(NodeLink node) {
184: throw new UnsupportedOperationException(
185: "Modification of node not supported");
186:
187: }
188:
189: /* (non-Javadoc)
190: * @see com.jofti.btree.INode#getNodeLock()
191: */
192: public ReadWriteLock getNodeLock() {
193:
194: throw new UnsupportedOperationException(
195: "Modification of node not supported");
196:
197: }
198:
199: /* (non-Javadoc)
200: * @see com.jofti.btree.IResultNode#getEntry(java.lang.Comparable)
201: */
202: public LeafNodeEntry getEntry(Comparable value) {
203:
204: if (entries.length == 0) {
205: return null;
206: }
207: // look through list and see if we have a match
208:
209: return indexedBinaryRetrieve(entries, value);
210:
211: }
212:
213: INode getDelegate() {
214: return node;
215: }
216:
217: protected int indexedBinarySearch(Object[] arr1, Object obj) {
218: int low = 0;
219: int high = entryNumber - 1;
220:
221: LeafNodeEntry entry = null;
222: while (low <= high) {
223: int mid = (low + high) >> 1;
224:
225: entry = (LeafNodeEntry) arr1[mid];
226: int cmp = entry.getValue().compareTo(obj);
227:
228: if (cmp < 0)
229: low = mid + 1;
230: else if (cmp > 0)
231: high = mid - 1;
232: else
233: return mid; // key found
234: }
235: return -(low + 1); // key not found
236: }
237:
238: protected LeafNodeEntry indexedBinaryRetrieve(Object[] list1,
239: Object obj) {
240: int i = 0;
241: int size = entryNumber;
242: for (int j = size - 1; i <= j;) {
243: int k = i + j >> 1;
244: LeafNodeEntry obj1 = (LeafNodeEntry) list1[k];
245: int l = obj1.getValue().compareTo(obj);
246: if (l < 0)
247: i = k + 1;
248: else if (l > 0)
249: j = k - 1;
250: else
251: return obj1;
252: }
253:
254: return null;
255: }
256:
257: public int hashCode() {
258: return node.hashCode();
259: }
260:
261: /* (non-Javadoc)
262: * @see com.jofti.btree.IResultNode#getEntriesBefore(java.lang.Comparable)
263: */
264: public Collection getEntriesBefore(Comparable value) {
265:
266: return null;
267: }
268:
269: public boolean equals(Object obj) {
270: if (obj instanceof IResultNode) {
271: return getDelegate() == ((ResultNode) obj).getDelegate();
272: }
273: return false;
274: }
275:
276: public void setEntries(Object[] entries) {
277: throw new UnsupportedOperationException(
278: "Modification of node not supported");
279:
280: }
281:
282: public boolean isLeaf() {
283: return true;
284: }
285: }
|