Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 
 package org.apache.cayenne.modeler.util;

A circular array is an array of fixed size as objects are added it will push objects off of the end to allow space for new objects to be added. This is useful for things like a fixed history size for a navigation tool.

Since:
1.2
 
 public class CircularArray extends Object {
 
     private Object array[] = null;
     private int head = 0;
     private int tail = 0;
     private int count = 0;
     private int capacity = 0;

    
Creates an array of capacity size.

Parameters:
capacity - size of the new array
 
     public CircularArray(int capacity) {
         if (capacity <= 0) {
             throw new IllegalArgumentException("Capacity must be greater than zero");
         }
          = new Object[capacity];
         this. = capacity;
     }

    
Clears out the contents of the array.
 
     public synchronized void clear() {
          = new Object[];
          = 0;
          = 0;
          = 0;
     }

    
Returns true if the array has no elements;
 
     public boolean isEmpty() {
         return  == 0;
     }

    
Adds a new object to the array. If the array is full it will push the oldest item out of the array.

Parameters:
obj - the object to be added
 
     public synchronized void add(Object obj) {
         // we have wrapped and we have to move the head pointer
         if ( ==  &&  == ) {
              = ( + 1) % ;
         }
 
         [] = obj;
 
          = ( + 1) % ;
 
         ++;
         if ( > )
              = ;
     }

    
Returns the number of objects stored in the array.
 
     public int capacity() {
         return ;
     }
 
     /*
      * Converts the logical index into a physical index.
      */
     int convert(int index) {
        return (index + ) % ;
    }
    /*
     * Makes sure that the index is within range.
     */
    private void rangeCheck(int index) {
        if (index >=  || index < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + );
        }
    }

    
Returns true if the array contains the specified object.

Parameters:
obj the object to be checked
    public boolean contains(Object obj) {
        return indexOf(obj) >= 0;
    }

    
Gets the object at the specified index.

Parameters:
index the index of the object to be retrieved
    public synchronized Object get(int index) {
        rangeCheck(index);
        if ( == 0) {
            return null;
        }
        return [convert(index)];
    }

    
Returns the index of the specified object

Parameters:
obj the object that is being searched for
    public synchronized int indexOf(Object obj) {
        for (int i = 0; i < i++) {
            int index = convert(i);
            if ([index] == obj) {
                return i;
            }
        }
        // not found
        return -1;
    }

    
Removes the specified object from the array

Parameters:
i the index of the object to be removed
    public synchronized void remove(Object obj) {
        if ( == 0) {
            return;
        }
        
        int i = indexOf(obj);
        while (i >= 0) {
            // convert from logical to physical location
            int pos = convert(i);
            if (pos == ) {
                // move the head up one
                 = ( + 1) % ;
                [pos] = null;
                --;
            }
            else if (pos == ) {
                // move the tail back one
                 = ( - 1 + ) % ;
                [pos] = null;
                --;
            }
            else {
                // create a brand new array and start it back out at zero
                Object[] a = new Object[];
                int destPos = 0;
                int len = 0;
                if ( == ) {
                    // most likeley scenario when it is full
                    if ( < pos) {
                        // copy from head to position
                        len = pos - ;
                        System.arraycopy(adestPoslen);
                        destPos += len;
                        // copy from pos +1 to end
                        len = ( - 1) - pos;
                        if (len > 0) {
                            System.arraycopy(pos + 1, adestPoslen);
                            destPos += len;
                        }
                        // copy from zero to head
                        len = ;
                        if (len > 0) {
                            System.arraycopy(, 0, adestPoslen);
                        }
                    }
                    else if ( > pos) {
                        // copy from head to end of array
                        len =  - ;
                        if (len > 0) {
                            System.arraycopy(adestPoslen);
                            destPos += len;
                        }
                        // copy from zero to pos -1
                        len = pos;
                        if (len > 0) {
                            System.arraycopy(, 0, adestPoslen);
                            destPos += len;
                        }
                        // copy from pos + 1 to tail
                        len =  - pos - 1;
                        if (len > 0) {
                            System.arraycopy(pos + 1, adestPoslen);
                        }
                    }
                }
                else if ( < ) {
                    // copy from head to position -1
                    len = pos - ;
                    if (len > 0) {
                        System.arraycopy(adestPoslen);
                        destPos += len;
                    }
                    // copy from position + 1 to tail
                    len =  - pos;
                    if (len > 0) {
                        System.arraycopy(pos + 1, adestPoslen);
                        destPos += len;
                    }
                }
                else if ( > ) {
                    if ( < pos) {
                        // copy from head to position
                        len = pos - ;
                        System.arraycopy(adestPoslen);
                        destPos += len;
                        // copy from pos +1 to end
                        len =  - 1 - pos;
                        if (len > 0) {
                            System.arraycopy(pos + 1, adestPoslen);
                            destPos += len;
                        }
                        // copy from beginning to tail
                        len = ;
                        if (len > 0) {
                            System.arraycopy(, 0, adestPoslen);
                        }
                    }
                    else if ( > pos) {
                        // copy from head to end of array
                        len =  - ;
                        if (len > 0) {
                            System.arraycopy(adestPoslen);
                            destPos += len;
                        }
                        // copy from zero to pos -1
                        len = pos - 1;
                        if (len > 0) {
                            System.arraycopy(, 0, adestPoslen);
                            destPos += len;
                        }
                        // copy from pos+1 to tail
                        len =  - pos;
                        if (len > 0) {
                            System.arraycopy(pos + 1, adestPoslen);
                        }
                    }
                }
                --;
                 = a;
                 = 0;
                 = ;
            }
            i = indexOf(obj);
        }
    }

    
Resizes the array to the specified new size. If the new capacity is smaller than the current object count in the array, it will keep the newCapacity most recent objects.

Parameters:
newCapacity the new capacity of the array
    public synchronized void resize(int newCapacity) {
        int i = 0;
        int offset = 0;
        if (newCapacity < ) {
            // making it smaller so we want to readjust the first object
            // to be copied into the new array
            i =  - newCapacity;
            offset =  - newCapacity;
        }
        Object newArray[] = new Object[newCapacity];
        for (; i < i++) {
            newArray[i - offset] = [convert(i)];
        }
         = 0;
         = 0;
         = newCapacity;
        // adjust the count if it is more than the new capacity
        if ( < )
             = ;
         = newArray;
    }

    
Returns the number of objects stored in the array.
    public int size() {
        return ;
    }

    
Converts the array to an Object array.
    public synchronized Object[] toArray() {
        Object[] o = new Object[];
        for (int i = 0; i < i++) {
            o[i] = [convert(i)];
        }
        return o;
    }
    public synchronized String internalRep() {
        Object o = null;
        StringBuffer sb = new StringBuffer();
        sb.append("\n");
        for (int i = 0; i < .i++) {
            sb.append('(').append(i).append(")  ");
            o = [i];
            if (o == null) {
                sb.append("null");
            }
            else {
                sb.append(o.toString());
            }
            if (i ==  || i == ) {
                sb.append('<');
                if (i == )
                    sb.append("h");
                if (i == )
                    sb.append("t");
            }
            sb.append("\n");
        }
        sb.append("count = [");
        sb.append();
        sb.append("]");
        sb.append("\nhead  = [");
        sb.append();
        sb.append("]");
        sb.append("\ntail  = [");
        sb.append();
        sb.append("]");
        return sb.toString();
    }
    public String toString() {
        Object[] oa = toArray();
        Object o = null;
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < oa.lengthi++) {
            o = oa[i];
            if (i > 0) {
                sb.append(", ");
            }
            if (o == null) {
                sb.append("null");
            }
            else {
                sb.append(o.toString());
            }
        }
        sb.append("]");
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X