001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.collections;
018
019import java.io.Serializable;
020
021import com.fasterxml.jackson.annotation.JsonProperty;
022
023/**
024 * A class containing an int array that is managed in the sense that it can grow
025 * dynamically (using exponential growth). It is useful for cases where an
026 * ArrayList<Integer> seems like overkill due to the high memory footprint of
027 * the Integer objects and the amount of work performed for auto (un)boxing.
028 * Note however, that a subclass has full access to the internals and thus might
029 * cause chaos.
030 */
031public class ManagedIntArray implements Serializable {
032
033        private static final long serialVersionUID = 1L;
034
035        /** The current size of the array. */
036        @JsonProperty("size")
037        protected int size = 0;
038
039        /** The actual array. */
040        @JsonProperty("array")
041        protected int[] array = new int[8];
042
043        /** Add space for a single element to the end of the array. */
044        protected void addArrayElement() {
045                addArrayElements(1);
046        }
047
048        /** Add space for multiple elements to the end of the array. */
049        protected void addArrayElements(int count) {
050                if (size + count >= array.length) {
051                        int newSize = 2 * array.length;
052                        while (newSize <= size + count) {
053                                newSize *= 2;
054                        }
055
056                        int[] oldArray = array;
057                        array = new int[newSize];
058                        System.arraycopy(oldArray, 0, array, 0, size);
059                }
060                size += count;
061        }
062}