/*
* 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 String
into
* an array of int
, splitting the base along the
* given separator pattern.
* Note: this method assumes the input is a string containing
* only decimal integers, signed or unsigned, that are parsable
* by java.lang.Integer.parseInt(String)
. If this
* is not the case, the resulting NumberFormatException
* will have to be handled by the caller.
*
* @param baseString the base string
* @param separatorPattern the pattern separating the integer values
* (if this is null
, the baseString is parsed as one
* integer value)
* @return an array of int
whose size is equal to the number
* values in the input string; null
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 String
into
* an array of double
, splitting the base along the
* given separator pattern.
* Note: this method assumes the input is a string containing
* only decimal doubles, signed or unsigned, that are parsable
* by java.lang.Double.parseDouble(String)
. If this
* is not the case, the resulting NumberFormatException
* will have to be handled by the caller.
*
* @param baseString the base string
* @param separatorPattern the pattern separating the integer values
* (if this is null
, the baseString is parsed as one
* double value)
* @return an array of double
whose size is equal to the number
* values in the input string; null
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;
}
}