001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.util;
021
022 /*
023 * Created 29.03.2004 at 14:58:11.
024 * $Id: RangeParser.java 1368577 2012-08-02 16:20:31Z lofwyr $
025 */
026
027 import java.util.ArrayList;
028 import java.util.List;
029 import java.util.StringTokenizer;
030
031 public class RangeParser {
032
033 public static int[] getIndices(String list) {
034 List indexList = new ArrayList();
035 StringTokenizer st = new StringTokenizer(list, ",");
036 while (st.hasMoreTokens()) {
037 String token = st.nextToken().trim();
038 int idx = token.indexOf('-');
039 if (idx == -1) {
040 indexList.add(token);
041 } else {
042 int start = Integer.parseInt(token.substring(0, idx).trim());
043 int end = Integer.parseInt(token.substring(idx + 1).trim());
044 if (start < end) {
045 for (int i = start; i < end + 1; i++) {
046 indexList.add(Integer.toString(i));
047 }
048 } else {
049 for (int i = start; i > end - 1; i--) {
050 indexList.add(Integer.toString(i));
051 }
052 }
053 }
054 }
055
056 int[] indices = new int[indexList.size()];
057 for (int i = 0; i < indices.length; i++) {
058 indices[i] = Integer.parseInt((String) indexList.get(i));
059 }
060 return indices;
061 }
062 }