All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.util.OAArray Maven / Gradle / Ivy

The newest version!
/*  Copyright 1999-2015 Vince Via [email protected]
    Licensed 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 com.viaoa.util;

import java.lang.reflect.Array;
import java.util.Arrays;


public class OAArray {

    public static boolean contains(Object[] array, Object searchValue) {
    	if (array == null || array.length == 0) return false;
    	for (int i=0; i= array.length) return -1;
        
        for (int i=startPos; i= x) return array;

        if (x == 1) {
            return (Object[]) Array.newInstance(c, 0);
        }
        
        if (pos == x-1) {
            // remove last element
            Object[] newArray = (Object[]) Arrays.copyOf(array, x-1);
            return newArray;
        }
        
        Object[] newArray = (Object[]) Array.newInstance(c, x-1);
        if (pos == 0) {
            System.arraycopy(array, 1, newArray, 0, x-1);
        }
        else {
            System.arraycopy(array, 0, newArray, 0, pos);
            System.arraycopy(array, pos+1, newArray, pos, (x-pos)-1);
        }
        return newArray;
    }


    public static Object[] insert(Class c, Object[] array, Object value, int atPos) {
        int x = (array == null) ? 0 : array.length;
        
        if (atPos >= x) {
            return add(c, array, value);
        }
        
        Object[] newArray = (Object[]) Array.newInstance(c, x+1);

        if (atPos == 0) {
            System.arraycopy(array, 0, newArray, 1, x);
        }
        else {
            System.arraycopy(array, 0, newArray, 0, atPos);
            System.arraycopy(array, atPos, newArray, atPos+1, x-atPos);
        }
        newArray[atPos] = value;
        return newArray;
    }
    
    
    
    public static int[] removeValue(int[] array, int searchValue) {
        if (array == null || array.length == 0) return array;

        int x = array.length;
        int pos = -1;
        for (int i=0; pos<0 && i= array.length) return array;

        int x = array.length;
        if (x == 1) {
            return new int[0];
        }
        
        if (pos == x-1) {
            // remove last element
            int[] newArray = (int[]) Arrays.copyOf(array, x-1);
            return newArray;
        }
        
        int[] newArray = new int[x-1];
        if (pos == 0) {
            System.arraycopy(array, 1, newArray, 0, x-1);
        }
        else {
            System.arraycopy(array, 0, newArray, 0, pos);
            System.arraycopy(array, pos+1, newArray, pos, (x-pos)-1);
        }
        return newArray;
    }

    public static double[] removeValue(double[] array, double searchValue) {
        if (array == null || array.length == 0) return array;

        int x = array.length;
        int pos = -1;
        for (int i=0; pos<0 && i= array.length) return array;

        int x = array.length;
        if (x == 1) {
            return new double[0];
        }
        
        if (pos == x-1) {
            // remove last element
            double[] newArray = (double[]) Arrays.copyOf(array, x-1);
            return newArray;
        }
        
        double[] newArray = new double[x-1];
        if (pos == 0) {
            System.arraycopy(array, 1, newArray, 0, x-1);
        }
        else {
            System.arraycopy(array, 0, newArray, 0, pos);
            System.arraycopy(array, pos+1, newArray, pos, (x-pos)-1);
        }
        return newArray;
    }
    
    /**
     * reorders one array to match a second, if possible.
     */
    public static void reorderToMatch(Object[] obja, Object[] objb) {
        if (obja == null) return;
        int x = obja.length;
        if (objb == null || objb.length != x) return;

        Object[] objNew = new Object[x];
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy