001    package de.mospace.lang;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: Array.java 12 2007-07-27 09:48:53Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.util.Arrays;
023     
024    /**
025     * Array utilities.
026     * @author Moritz Ringler
027     * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
028     */
029    public class Array{
030        private Array(){
031        }
032        /**Joins the elements of an Object array to a character-delimited String.
033         * No warning will
034         * be issued if one of the array elements contains the delimiter.
035         * @param arr the array to join
036         * @param delim the delimiter to use
037         * @return a string made up of the string representations of the array elements separated by the delimiter character
038         */
039        public static String join(Object[] arr, char delim){
040            if (arr.length == 0){
041                return "";
042            }
043            StringBuffer jsb = new StringBuffer(arr.length*3);
044            jsb.append(arr[0]);
045            for (int i=1; i<arr.length; i++){
046                jsb.append(delim);
047                jsb.append(arr[i]);
048            }
049            return jsb.toString();
050        }
051    
052        /**Joins the elements of an int array to a character-delimited String.
053         * No warning will
054         * be issued if one of the array elements contains the delimiter.
055         * @param arr the array to join
056         * @param delim the delimiter to use
057         * @return a string made up of the string representations of the
058         * array elements separated by the delimiter character
059         */
060        public static String join(int[] arr, char delim){
061            if (arr.length == 0){
062                return "";
063            }
064            StringBuffer jsb = new StringBuffer(arr.length*3);
065            jsb.append(arr[0]);
066            for (int i=1; i<arr.length; i++){
067                jsb.append(delim);
068                jsb.append(arr[i]);
069            }
070            return jsb.toString();
071        }
072    
073        /**Converts a byte array to a Hex String.
074        * @param b a non-null byte array 
075        * @return the input array as a sequence of Hex digits
076        */
077        public static String byteArrayToString(byte[] b){
078            StringBuffer sb = new StringBuffer(b.length * 2);
079            for (int i=0; i<b.length; i++){
080                //sb.append('$');
081                String s = Integer.toHexString((new Byte(b[i])).intValue() & 0xff);
082                for(int j=0; j<(2-s.length()); j++){
083                  sb.append('0');
084                }
085                sb.append(s);
086            }
087            return sb.toString();
088        }
089    
090        /** Returns the continuous sequences of integers contained in the input
091        * array. The input array is sorted and for each continous sequence
092        * of integers that it contains a 2-element array made up of its lower
093        * and upper bounds is added to the result.
094        * @param numbers the set of integers to analyze
095        * @return <code>null</code> if <code>numbers</code> is <code>null</code>
096        * or empty. Otherwise an ordered array of pairs of integers, which are
097        * the lower and upper bounds of the continuous sequences of integers
098        * contained in the input.
099        */
100        public static int[][] getIntervals(int[] numbers){
101            if(numbers == null || numbers.length == 0){
102                return null;
103            }
104            Arrays.sort(numbers);
105            int[][] rawresult = new int[numbers.length][2];
106            rawresult[0] = new int[]{numbers[0],numbers[0]};
107            int j = 0;
108            for (int i=1; i<numbers.length; i++){
109                if(++rawresult[j][1] != numbers[i]){
110                    rawresult[j][1]--;
111                    rawresult[++j] = new int[]{numbers[i], numbers[i]};
112                }
113            }
114            /* truncate array */
115            if (++j<rawresult.length){
116                int[][] result = new int[j][2];
117                System.arraycopy(rawresult,0,result,0,j);
118                return result;
119            } else {
120                return rawresult;
121            }
122        }
123    
124        /** Extracts a sub-array from a double array.
125        * @param src a non-null double array
126        * @param begin the start of the sub-array to extract
127        * @param length the length of the sub-array to extract
128        * @return a new double array 
129        * <code>{src[begin], ..., src[begin + length - 1]}</code> 
130        */
131        public static double[] slice(double[] src, int begin, int length){
132            double[] result = new double[length];
133            System.arraycopy(src,begin,result,0,length);
134            return result;
135        }
136    }