Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.util.ArrayList;
   20   
   21   /**
   22    * Provides a list of LinkedNode objects. 
   23    * 
   24    * @author chirino
   25    */
   26   public class LinkedNodeList<T extends LinkedNode<T>> {
   27   
   28       T head;
   29       int size;
   30   
   31       public LinkedNodeList() {
   32       }
   33   
   34       public boolean isEmpty() {
   35           return head == null;
   36       }
   37   
   38       public void addLast(T node) {
   39           node.linkToTail(this);
   40       }
   41   
   42       public void addFirst(T node) {
   43           node.linkToHead(this);
   44       }
   45   
   46       public T getHead() {
   47           return head;
   48       }
   49   
   50       public T getTail() {
   51           return head.prev;
   52       }
   53       
   54       public void clear() {
   55           while (head != null) {
   56               head.unlink();
   57           }
   58       }
   59   
   60       public void addLast(LinkedNodeList<T> list) {
   61           if (list.isEmpty()) {
   62               return;
   63           }
   64           if (head == null) {
   65               head = list.head;
   66               reparent(list);
   67           } else {
   68               getTail().linkAfter(list);
   69           }
   70       }
   71   
   72       public void addFirst(LinkedNodeList<T> list) {
   73           if (list.isEmpty()) {
   74               return;
   75           }
   76           if (head == null) {
   77               reparent(list);
   78               head = list.head;
   79               list.head = null;
   80           } else {
   81               getHead().linkBefore(list);
   82           }
   83       }
   84   
   85       public T reparent(LinkedNodeList<T> list) {
   86           size += list.size;
   87           T n = list.head;
   88           do {
   89               n.list = this;
   90               n = n.next;
   91           } while (n != list.head);
   92           list.head = null;
   93           list.size = 0;
   94           return n;
   95       }
   96   
   97       /**
   98        * Move the head to the tail and returns the new head node.
   99        * 
  100        * @return
  101        */
  102       public T rotate() {
  103       	if( head ==null )
  104       		return null;
  105           return head = head.getNextCircular();
  106       }
  107   
  108       /**
  109        * Move the head to the tail and returns the new head node.
  110        * 
  111        * @return
  112        */
  113       public void rotateTo(T head) {
  114       	assert head!=null: "Cannot rotate to a null head";
  115       	assert head.list == this : "Cannot rotate to a node not linked to this list";
  116           this.head = head;
  117       }
  118   
  119       public int size() {
  120           return size;
  121       }
  122   
  123       @Override
  124       public String toString() {
  125           StringBuilder sb = new StringBuilder();
  126           sb.append("[");
  127           boolean first=true;
  128           T cur = getHead();
  129           while( cur!=null ) {
  130               if( !first ) {
  131                   sb.append(", ");
  132               }
  133               sb.append(cur);
  134               first=false;
  135               cur = cur.getNext();
  136           }
  137           sb.append("]");
  138           return sb.toString();
  139       }
  140       
  141       /**
  142        * Copies the nodes of the LinkedNodeList to an ArrayList.
  143        * @return
  144        */
  145       public ArrayList<T> toArrayList() {
  146       	ArrayList<T> rc = new ArrayList<T>(size);
  147       	T cur = head;
  148       	while( cur!=null ) {
  149       		rc.add(cur);
  150       		cur = cur.getNext();
  151       	}
  152       	return rc;
  153       }
  154   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]