You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConversionUtils.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. /**
  20. * This class contains utility methods for conversions, like
  21. * a java.lang.String to an array of int or double.
  22. */
  23. public final class ConversionUtils {
  24. private ConversionUtils() {
  25. }
  26. /**
  27. * Converts the given base <code>String</code> into
  28. * an array of <code>int</code>, splitting the base along the
  29. * given separator pattern.
  30. * <em>Note: this method assumes the input is a string containing
  31. * only decimal integers, signed or unsigned, that are parsable
  32. * by <code>java.lang.Integer.parseInt(String)</code>. If this
  33. * is not the case, the resulting <code>NumberFormatException</code>
  34. * will have to be handled by the caller.</em>
  35. *
  36. * @param baseString the base string
  37. * @param separatorPattern the pattern separating the integer values
  38. * (if this is <code>null</code>, the baseString is parsed as one
  39. * integer value)
  40. * @return an array of <code>int</code> whose size is equal to the number
  41. * values in the input string; <code>null</code> if this number
  42. * is equal to zero.
  43. */
  44. public static int[] toIntArray(String baseString, String separatorPattern) {
  45. if (baseString == null || "".equals(baseString)) {
  46. return null;
  47. }
  48. if (separatorPattern == null || "".equals(separatorPattern)) {
  49. return new int[] {Integer.parseInt(baseString)};
  50. }
  51. String[] values = baseString.split(separatorPattern);
  52. int numValues = values.length;
  53. if (numValues == 0) {
  54. return null;
  55. }
  56. int[] returnArray = new int[numValues];
  57. for (int i = 0; i < numValues; ++i) {
  58. returnArray[i] = Integer.parseInt(values[i]);
  59. }
  60. return returnArray;
  61. }
  62. /**
  63. * Converts the given base <code>String</code> into
  64. * an array of <code>double</code>, splitting the base along the
  65. * given separator pattern.
  66. * <em>Note: this method assumes the input is a string containing
  67. * only decimal doubles, signed or unsigned, that are parsable
  68. * by <code>java.lang.Double.parseDouble(String)</code>. If this
  69. * is not the case, the resulting <code>NumberFormatException</code>
  70. * will have to be handled by the caller.</em>
  71. *
  72. * @param baseString the base string
  73. * @param separatorPattern the pattern separating the integer values
  74. * (if this is <code>null</code>, the baseString is parsed as one
  75. * double value)
  76. * @return an array of <code>double</code> whose size is equal to the number
  77. * values in the input string; <code>null</code> if this number
  78. * is equal to zero.
  79. */
  80. public static double[] toDoubleArray(String baseString, String separatorPattern) {
  81. if (baseString == null || "".equals(baseString)) {
  82. return null;
  83. }
  84. if (separatorPattern == null || "".equals(separatorPattern)) {
  85. return new double[] {Double.parseDouble(baseString)};
  86. }
  87. String[] values = baseString.split(separatorPattern);
  88. int numValues = values.length;
  89. if (numValues == 0) {
  90. return null;
  91. }
  92. double[] returnArray = new double[numValues];
  93. for (int i = 0; i < numValues; ++i) {
  94. returnArray[i] = Double.parseDouble(values[i]);
  95. }
  96. return returnArray;
  97. }
  98. }