001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.settings.support;
017    
018    import java.util.Arrays;
019    
020    /**
021     * Utility class for converting a String array to an int array
022     * 
023     * @author Peter De Bruycker
024     */
025    public class ArrayUtil {
026    
027        private ArrayUtil() {
028    
029        }
030    
031        public static int[] toIntArray(String[] stringArray) throws IllegalArgumentException {
032            int result[] = new int[stringArray.length];
033            for (int i = 0; i < stringArray.length; i++) {
034                result[i] = Integer.parseInt(stringArray[i]);
035            }
036            return result;
037        }
038    
039        public static String asIntervalString(int[] array) {
040            Arrays.sort(array);
041            StringBuffer sb = new StringBuffer();
042            int i = 0;
043            while (i < array.length) {
044                sb.append(array[i]);
045                if (i < array.length - 1) {
046                    if (array[i] == array[i + 1] - 1) {
047                        while (i < array.length - 1 && array[i] == array[i + 1] - 1) {
048                            i++;
049                        }
050                        sb.append("-");
051                        sb.append(array[i]);
052                    }
053                    if (i < array.length - 1) {
054                        sb.append(",");
055                    }
056                }
057                i++;
058            }
059            return sb.toString();
060        }
061    
062    }