KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > ArrayUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 /**
25  * Collection of useful utilities to work with arrays.
26  *
27  * @version $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
28  * @author Peter Satury
29  * @code.reviewer Miro Halas
30  * @code.reviewed 1.5 2005/07/29 07:36:24 bastafidli
31  */

32 public final class ArrayUtils
33 {
34    /**
35     * Private constructor since this class cannot be instantiated
36     */

37    private ArrayUtils(
38    )
39    {
40       // Do nothing
41
}
42    
43    // Public methods ///////////////////////////////////////////////////////////
44

45    /**
46     * Method to exclude 2 arrays of ints so that the result contains all elements
47     * from the first array, which are not in the second array.
48     *
49     * @param arrBase - base array to exclude from
50     * @param arrExclude - array to exclude from the first one
51     * @return int[] - array which contains all elements from the first array
52     * which are not in the second array or null
53     */

54    public static int[] exclude(
55       int[] arrBase,
56       int[] arrExclude
57    )
58    {
59       int[] arrReturn = null;
60
61       if ((arrBase != null) && (arrBase.length > 0) && (arrExclude != null)
62          && (arrExclude.length > 0))
63       {
64          int[] arrHelp;
65          int iCount1;
66          int iHelp;
67          int iLength = 0;
68          
69          arrHelp = new int[arrBase.length];
70          for (iCount1 = 0; iCount1 < arrBase.length; iCount1++)
71          {
72             iHelp = arrBase[iCount1];
73             if (ArrayUtils.contains(arrExclude, iHelp) == -1)
74             {
75                // If the element is not part of the second array then it should
76
// be included in the result
77
arrHelp[iLength++] = iHelp;
78             }
79          }
80          
81          // Shrink the array
82
// TODO: Performance: Replace this with System.arraycopy
83
arrReturn = new int[iLength];
84          for (int iCount = 0; iCount < iLength; iCount++)
85          {
86             arrReturn[iCount] = arrHelp[iCount];
87          }
88       }
89       else
90       {
91          arrReturn = arrBase;
92       }
93       
94       return arrReturn;
95    }
96    
97    /**
98     * Test if specified array contains given element and if it does, find
99     * its position.
100     *
101     * @param source - array to search, can be null
102     * @param iTarget - element to find
103     * @return int - -1 if it doesn't exist there otherwise its position
104     */

105    public static int contains(
106       int[] source,
107       int iTarget
108    )
109    {
110       int iReturn = -1;
111       
112       if ((source != null) && (source.length > 0))
113       {
114          int iIndex;
115          
116          for (iIndex = 0; iIndex < source.length; iIndex++)
117          {
118             if (source[iIndex] == iTarget)
119             {
120                iReturn = iIndex;
121                break;
122             }
123          }
124       }
125       
126       return iReturn;
127    }
128
129    /**
130     * Sum all elements in the array.
131     *
132     * @param source - array to sum elements of
133     * @return long - sum of the elements in the array
134     */

135    public static long sum(
136       int[] source
137    )
138    {
139       int iReturn = 0;
140       
141       if ((source != null) && (source.length > 0))
142       {
143          int iIndex;
144          
145          for (iIndex = 0; iIndex < source.length; iIndex++)
146          {
147             iReturn += source[iIndex];
148          }
149       }
150       
151       return iReturn;
152    }
153 }
154
Popular Tags