aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/util/ConversionUtils.java
blob: e2d93fbd3eaec733efdbf86a94d4c25bce47c1c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
/* $Id$ */
package org.apache.fop.util;

/**
 * This class contains utility methods for conversions, like
 * a java.lang.String to an array of int or double.
 */
public final class ConversionUtils {

    /**
     * Converts the given base <code>String</code> into
     * an array of <code>int</code>, splitting the base along the
     * given separator pattern.
     * <em>Note: this method assumes the input is a string containing 
     * only decimal integers, signed or unsigned, that are parsable
     * by <code>java.lang.Integer.parseInt(String)</code>. If this
     * is not the case, the resulting <code>NumberFormatException</code>
     * will have to be handled by the caller.</em>
     * 
     * @param baseString    the base string
     * @param separatorPattern  the pattern separating the integer values
     *        (if this is <code>null</code>, the baseString is parsed as one
     *         integer value) 
     * @return  an array of <code>int</code> whose size is equal to the number
     *          values in the input string; <code>null</code> if this number
     *          is equal to zero.
     */
    public static int[] toIntArray(String baseString, String separatorPattern) {
        
        if (baseString == null || "".equals(baseString)) {
            return null;
        }
        
        if (separatorPattern == null || "".equals(separatorPattern)) {
            return new int[] { Integer.parseInt(baseString) };
        }
        
        String[] values = baseString.split(separatorPattern);
        int numValues = values.length;
        if (numValues == 0) {
            return null;
        }
        
        int[] returnArray = new int[numValues];
        for (int i = 0; i < numValues; ++i) {
            returnArray[i] = Integer.parseInt(values[i]);
        }
        return returnArray;
        
    }

    /**
     * Converts the given base <code>String</code> into
     * an array of <code>double</code>, splitting the base along the
     * given separator pattern.
     * <em>Note: this method assumes the input is a string containing 
     * only decimal doubles, signed or unsigned, that are parsable
     * by <code>java.lang.Double.parseDouble(String)</code>. If this
     * is not the case, the resulting <code>NumberFormatException</code>
     * will have to be handled by the caller.</em>
     * 
     * @param baseString    the base string
     * @param separatorPattern  the pattern separating the integer values
     *        (if this is <code>null</code>, the baseString is parsed as one
     *         double value) 
     * @return  an array of <code>double</code> whose size is equal to the number
     *          values in the input string; <code>null</code> if this number
     *          is equal to zero.
     */
    public static double[] toDoubleArray(String baseString, String separatorPattern) {
        
        if (baseString == null || "".equals(baseString)) {
            return null;
        }
        
        if (separatorPattern == null || "".equals(separatorPattern)) {
            return new double[] { Double.parseDouble(baseString) };
        }
        
        String[] values = baseString.split(separatorPattern);
        int numValues = values.length;
        if (numValues == 0) {
            return null;
        }
        
        double[] returnArray = new double[numValues];
        for (int i = 0; i < numValues; ++i) {
            returnArray[i] = Double.parseDouble(values[i]);
        }
        return returnArray;
        
    }
    
}