/modules/test/poshi-runner/poshi-runner/src/main/java/com/liferay/poshi/runner/util/GetterUtil.java

https://github.com/kiyoshilee/liferay-portal · Java · 1110 lines · 782 code · 312 blank · 16 comment · 164 complexity · 99fd2ffbd8a96fe16bd745f6bc0faa38 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.poshi.runner.util;
  15. import java.math.BigDecimal;
  16. import java.text.DateFormat;
  17. import java.util.Date;
  18. /**
  19. * @author Brian Wing Shun Chan
  20. */
  21. public class GetterUtil {
  22. public static final String[] BOOLEANS = {"true", "t", "y", "on", "1"};
  23. public static final boolean DEFAULT_BOOLEAN = false;
  24. public static final boolean[] DEFAULT_BOOLEAN_VALUES = new boolean[0];
  25. public static final byte DEFAULT_BYTE = 0;
  26. public static final byte[] DEFAULT_BYTE_VALUES = new byte[0];
  27. public static final Date[] DEFAULT_DATE_VALUES = new Date[0];
  28. public static final double DEFAULT_DOUBLE = 0.0;
  29. public static final double[] DEFAULT_DOUBLE_VALUES = new double[0];
  30. public static final float DEFAULT_FLOAT = 0;
  31. public static final float[] DEFAULT_FLOAT_VALUES = new float[0];
  32. public static final int DEFAULT_INTEGER = 0;
  33. public static final int[] DEFAULT_INTEGER_VALUES = new int[0];
  34. public static final long DEFAULT_LONG = 0;
  35. public static final long[] DEFAULT_LONG_VALUES = new long[0];
  36. public static final Number DEFAULT_NUMBER = 0;
  37. public static final Number[] DEFAULT_NUMBER_VALUES = new Number[0];
  38. public static final Number DEFAULT_OBJECT = null;
  39. public static final short DEFAULT_SHORT = 0;
  40. public static final short[] DEFAULT_SHORT_VALUES = new short[0];
  41. public static final String DEFAULT_STRING = StringPool.BLANK;
  42. public static final String[] DEFAULT_STRING_VALUES = new String[0];
  43. public static boolean get(Object value, boolean defaultValue) {
  44. if (value instanceof String) {
  45. return get((String)value, defaultValue);
  46. }
  47. if (value instanceof Boolean) {
  48. return (Boolean)value;
  49. }
  50. return defaultValue;
  51. }
  52. public static Date get(
  53. Object value, DateFormat dateFormat, Date defaultValue) {
  54. if (value instanceof String) {
  55. return get((String)value, dateFormat, defaultValue);
  56. }
  57. if (value instanceof Date) {
  58. return (Date)value;
  59. }
  60. return defaultValue;
  61. }
  62. public static double get(Object value, double defaultValue) {
  63. if (value instanceof String) {
  64. return get((String)value, defaultValue);
  65. }
  66. if (value instanceof Double) {
  67. return (Double)value;
  68. }
  69. if (value instanceof Number) {
  70. Number number = (Number)value;
  71. return number.doubleValue();
  72. }
  73. return defaultValue;
  74. }
  75. public static float get(Object value, float defaultValue) {
  76. if (value instanceof String) {
  77. return get((String)value, defaultValue);
  78. }
  79. if (value instanceof Float) {
  80. return (Float)value;
  81. }
  82. if (value instanceof Number) {
  83. Number number = (Number)value;
  84. return number.floatValue();
  85. }
  86. return defaultValue;
  87. }
  88. public static int get(Object value, int defaultValue) {
  89. if (value instanceof String) {
  90. return get((String)value, defaultValue);
  91. }
  92. if (value instanceof Integer) {
  93. return (Integer)value;
  94. }
  95. if (value instanceof Number) {
  96. Number number = (Number)value;
  97. return number.intValue();
  98. }
  99. return defaultValue;
  100. }
  101. public static long get(Object value, long defaultValue) {
  102. if (value instanceof String) {
  103. return get((String)value, defaultValue);
  104. }
  105. if (value instanceof Long) {
  106. return (Long)value;
  107. }
  108. if (value instanceof Number) {
  109. Number number = (Number)value;
  110. return number.longValue();
  111. }
  112. return defaultValue;
  113. }
  114. public static Number get(Object value, Number defaultValue) {
  115. if (value instanceof String) {
  116. String valueString = (String)value;
  117. if (Validator.isNull(valueString)) {
  118. return defaultValue;
  119. }
  120. try {
  121. return new BigDecimal(valueString.trim());
  122. }
  123. catch (NumberFormatException nfe) {
  124. return defaultValue;
  125. }
  126. }
  127. if (value instanceof Byte) {
  128. return (Byte)value;
  129. }
  130. if (value instanceof Double) {
  131. return (Double)value;
  132. }
  133. if (value instanceof Float) {
  134. return (Float)value;
  135. }
  136. if (value instanceof Integer) {
  137. return (Integer)value;
  138. }
  139. if (value instanceof Long) {
  140. return (Long)value;
  141. }
  142. if (value instanceof Short) {
  143. return (Short)value;
  144. }
  145. if (value instanceof Number) {
  146. return (Number)value;
  147. }
  148. return defaultValue;
  149. }
  150. public static short get(Object value, short defaultValue) {
  151. if (value instanceof String) {
  152. return get((String)value, defaultValue);
  153. }
  154. if (value instanceof Short) {
  155. return (Short)value;
  156. }
  157. if (value instanceof Number) {
  158. Number number = (Number)value;
  159. return number.shortValue();
  160. }
  161. return defaultValue;
  162. }
  163. public static String get(Object value, String defaultValue) {
  164. if (value instanceof String) {
  165. return get((String)value, defaultValue);
  166. }
  167. return defaultValue;
  168. }
  169. public static boolean get(String value, boolean defaultValue) {
  170. if (value == null) {
  171. return defaultValue;
  172. }
  173. value = StringUtil.toLowerCase(value);
  174. value = value.trim();
  175. if (value.equals(BOOLEANS[0]) || value.equals(BOOLEANS[1]) ||
  176. value.equals(BOOLEANS[2]) || value.equals(BOOLEANS[3]) ||
  177. value.equals(BOOLEANS[4])) {
  178. return true;
  179. }
  180. return false;
  181. }
  182. public static Date get(
  183. String value, DateFormat dateFormat, Date defaultValue) {
  184. if (value == null) {
  185. return defaultValue;
  186. }
  187. try {
  188. Date date = dateFormat.parse(value.trim());
  189. if (date != null) {
  190. return date;
  191. }
  192. }
  193. catch (Exception e) {
  194. }
  195. return defaultValue;
  196. }
  197. public static double get(String value, double defaultValue) {
  198. if (value != null) {
  199. try {
  200. return Double.parseDouble(value.trim());
  201. }
  202. catch (Exception e) {
  203. }
  204. }
  205. return defaultValue;
  206. }
  207. public static float get(String value, float defaultValue) {
  208. if (value == null) {
  209. return defaultValue;
  210. }
  211. try {
  212. return Float.parseFloat(value.trim());
  213. }
  214. catch (Exception e) {
  215. }
  216. return defaultValue;
  217. }
  218. public static int get(String value, int defaultValue) {
  219. if (value == null) {
  220. return defaultValue;
  221. }
  222. return _parseInt(value.trim(), defaultValue);
  223. }
  224. public static long get(String value, long defaultValue) {
  225. if (value == null) {
  226. return defaultValue;
  227. }
  228. return _parseLong(value.trim(), defaultValue);
  229. }
  230. public static short get(String value, short defaultValue) {
  231. if (value == null) {
  232. return defaultValue;
  233. }
  234. return _parseShort(value.trim(), defaultValue);
  235. }
  236. public static String get(String value, String defaultValue) {
  237. if (value == null) {
  238. return defaultValue;
  239. }
  240. value = value.trim();
  241. if (value.indexOf(CharPool.RETURN) != -1) {
  242. value = value.replaceAll(
  243. StringPool.RETURN_NEW_LINE, StringPool.NEW_LINE);
  244. }
  245. return value;
  246. }
  247. public static boolean getBoolean(Object value) {
  248. return getBoolean(value, DEFAULT_BOOLEAN);
  249. }
  250. public static boolean getBoolean(Object value, boolean defaultValue) {
  251. return get(value, defaultValue);
  252. }
  253. public static boolean getBoolean(String value) {
  254. return getBoolean(value, DEFAULT_BOOLEAN);
  255. }
  256. public static boolean getBoolean(String value, boolean defaultValue) {
  257. return get(value, defaultValue);
  258. }
  259. public static boolean[] getBooleanValues(Object value) {
  260. return getBooleanValues(value, DEFAULT_BOOLEAN_VALUES);
  261. }
  262. public static boolean[] getBooleanValues(
  263. Object value, boolean[] defaultValue) {
  264. if (value instanceof String[]) {
  265. return getBooleanValues((String[])value, defaultValue);
  266. }
  267. if (value instanceof boolean[]) {
  268. return (boolean[])value;
  269. }
  270. return defaultValue;
  271. }
  272. public static boolean[] getBooleanValues(String[] values) {
  273. return getBooleanValues(values, DEFAULT_BOOLEAN_VALUES);
  274. }
  275. public static boolean[] getBooleanValues(
  276. String[] values, boolean[] defaultValue) {
  277. if (values == null) {
  278. return defaultValue;
  279. }
  280. boolean[] booleanValues = new boolean[values.length];
  281. for (int i = 0; i < values.length; i++) {
  282. booleanValues[i] = getBoolean(values[i]);
  283. }
  284. return booleanValues;
  285. }
  286. public static Date getDate(Object value, DateFormat dateFormat) {
  287. return getDate(value, dateFormat, new Date());
  288. }
  289. public static Date getDate(
  290. Object value, DateFormat dateFormat, Date defaultValue) {
  291. return get(value, dateFormat, defaultValue);
  292. }
  293. public static Date getDate(String value, DateFormat dateFormat) {
  294. return getDate(value, dateFormat, new Date());
  295. }
  296. public static Date getDate(
  297. String value, DateFormat dateFormat, Date defaultValue) {
  298. return get(value, dateFormat, defaultValue);
  299. }
  300. public static Date[] getDateValues(Object value, DateFormat dateFormat) {
  301. return getDateValues(value, dateFormat, DEFAULT_DATE_VALUES);
  302. }
  303. public static Date[] getDateValues(
  304. Object value, DateFormat dateFormat, Date[] defaultValue) {
  305. if (value instanceof String[]) {
  306. return getDateValues((String[])value, dateFormat, defaultValue);
  307. }
  308. if (value instanceof Date[]) {
  309. return (Date[])value;
  310. }
  311. return defaultValue;
  312. }
  313. public static Date[] getDateValues(String[] values, DateFormat dateFormat) {
  314. return getDateValues(values, dateFormat, DEFAULT_DATE_VALUES);
  315. }
  316. public static Date[] getDateValues(
  317. String[] values, DateFormat dateFormat, Date[] defaultValue) {
  318. if (values == null) {
  319. return defaultValue;
  320. }
  321. Date[] dateValues = new Date[values.length];
  322. for (int i = 0; i < values.length; i++) {
  323. dateValues[i] = getDate(values[i], dateFormat);
  324. }
  325. return dateValues;
  326. }
  327. public static double getDouble(Object value) {
  328. return getDouble(value, DEFAULT_DOUBLE);
  329. }
  330. public static double getDouble(Object value, double defaultValue) {
  331. return get(value, defaultValue);
  332. }
  333. public static double getDouble(String value) {
  334. return getDouble(value, DEFAULT_DOUBLE);
  335. }
  336. public static double getDouble(String value, double defaultValue) {
  337. return get(value, defaultValue);
  338. }
  339. public static double[] getDoubleValues(Object value) {
  340. return getDoubleValues(value, DEFAULT_DOUBLE_VALUES);
  341. }
  342. public static double[] getDoubleValues(
  343. Object value, double[] defaultValue) {
  344. if (value instanceof String[]) {
  345. return getDoubleValues((String[])value, defaultValue);
  346. }
  347. if (value instanceof double[]) {
  348. return (double[])value;
  349. }
  350. return defaultValue;
  351. }
  352. public static double[] getDoubleValues(String[] values) {
  353. return getDoubleValues(values, DEFAULT_DOUBLE_VALUES);
  354. }
  355. public static double[] getDoubleValues(
  356. String[] values, double[] defaultValue) {
  357. if (values == null) {
  358. return defaultValue;
  359. }
  360. double[] doubleValues = new double[values.length];
  361. for (int i = 0; i < values.length; i++) {
  362. doubleValues[i] = getDouble(values[i]);
  363. }
  364. return doubleValues;
  365. }
  366. public static float getFloat(Object value) {
  367. return getFloat(value, DEFAULT_FLOAT);
  368. }
  369. public static float getFloat(Object value, float defaultValue) {
  370. return get(value, defaultValue);
  371. }
  372. public static float getFloat(String value) {
  373. return getFloat(value, DEFAULT_FLOAT);
  374. }
  375. public static float getFloat(String value, float defaultValue) {
  376. return get(value, defaultValue);
  377. }
  378. public static float[] getFloatValues(Object value) {
  379. return getFloatValues(value, DEFAULT_FLOAT_VALUES);
  380. }
  381. public static float[] getFloatValues(Object value, float[] defaultValue) {
  382. if (value instanceof String[]) {
  383. return getFloatValues((String[])value, defaultValue);
  384. }
  385. if (value instanceof float[]) {
  386. return (float[])value;
  387. }
  388. return defaultValue;
  389. }
  390. public static float[] getFloatValues(String[] values) {
  391. return getFloatValues(values, DEFAULT_FLOAT_VALUES);
  392. }
  393. public static float[] getFloatValues(
  394. String[] values, float[] defaultValue) {
  395. if (values == null) {
  396. return defaultValue;
  397. }
  398. float[] floatValues = new float[values.length];
  399. for (int i = 0; i < values.length; i++) {
  400. floatValues[i] = getFloat(values[i]);
  401. }
  402. return floatValues;
  403. }
  404. public static int getInteger(Object value) {
  405. return getInteger(value, DEFAULT_INTEGER);
  406. }
  407. public static int getInteger(Object value, int defaultValue) {
  408. return get(value, defaultValue);
  409. }
  410. public static int getInteger(String value) {
  411. return getInteger(value, DEFAULT_INTEGER);
  412. }
  413. public static int getInteger(String value, int defaultValue) {
  414. return get(value, defaultValue);
  415. }
  416. public static int getIntegerStrict(String value) {
  417. int length = value.length();
  418. if (length <= 0) {
  419. throw new NumberFormatException("Unable to parse " + value);
  420. }
  421. int index = 0;
  422. int limit = -Integer.MAX_VALUE;
  423. boolean negative = false;
  424. char c = value.charAt(0);
  425. if (c < CharPool.NUMBER_0) {
  426. if (c == CharPool.MINUS) {
  427. limit = Integer.MIN_VALUE;
  428. negative = true;
  429. }
  430. else if (c != CharPool.PLUS) {
  431. throw new NumberFormatException("Unable to parse " + value);
  432. }
  433. if (length == 1) {
  434. throw new NumberFormatException("Unable to parse " + value);
  435. }
  436. index++;
  437. }
  438. int smallLimit = limit / 10;
  439. int result = 0;
  440. while (index < length) {
  441. if (result < smallLimit) {
  442. throw new NumberFormatException("Unable to parse " + value);
  443. }
  444. c = value.charAt(index++);
  445. if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
  446. throw new NumberFormatException("Unable to parse " + value);
  447. }
  448. int number = c - CharPool.NUMBER_0;
  449. result *= 10;
  450. if (result < (limit + number)) {
  451. throw new NumberFormatException("Unable to parse " + value);
  452. }
  453. result -= number;
  454. }
  455. if (negative) {
  456. return result;
  457. }
  458. return -result;
  459. }
  460. public static int[] getIntegerValues(Object value) {
  461. return getIntegerValues(value, DEFAULT_INTEGER_VALUES);
  462. }
  463. public static int[] getIntegerValues(Object value, int[] defaultValue) {
  464. if (value instanceof String[]) {
  465. return getIntegerValues((String[])value, defaultValue);
  466. }
  467. if (value instanceof int[]) {
  468. return (int[])value;
  469. }
  470. return defaultValue;
  471. }
  472. public static int[] getIntegerValues(String[] values) {
  473. return getIntegerValues(values, DEFAULT_INTEGER_VALUES);
  474. }
  475. public static int[] getIntegerValues(String[] values, int[] defaultValue) {
  476. if (values == null) {
  477. return defaultValue;
  478. }
  479. int[] intValues = new int[values.length];
  480. for (int i = 0; i < values.length; i++) {
  481. intValues[i] = getInteger(values[i]);
  482. }
  483. return intValues;
  484. }
  485. public static long getLong(Object value) {
  486. return getLong(value, DEFAULT_LONG);
  487. }
  488. public static long getLong(Object value, long defaultValue) {
  489. return get(value, defaultValue);
  490. }
  491. public static long getLong(String value) {
  492. return getLong(value, DEFAULT_LONG);
  493. }
  494. public static long getLong(String value, long defaultValue) {
  495. return get(value, defaultValue);
  496. }
  497. public static long getLongStrict(String value) {
  498. int length = value.length();
  499. if (length <= 0) {
  500. throw new NumberFormatException("Unable to parse " + value);
  501. }
  502. int index = 0;
  503. long limit = -Long.MAX_VALUE;
  504. boolean negative = false;
  505. char c = value.charAt(0);
  506. if (c < CharPool.NUMBER_0) {
  507. if (c == CharPool.MINUS) {
  508. limit = Long.MIN_VALUE;
  509. negative = true;
  510. }
  511. else if (c != CharPool.PLUS) {
  512. throw new NumberFormatException("Unable to parse " + value);
  513. }
  514. if (length == 1) {
  515. throw new NumberFormatException("Unable to parse " + value);
  516. }
  517. index++;
  518. }
  519. long smallLimit = limit / 10;
  520. long result = 0;
  521. while (index < length) {
  522. if (result < smallLimit) {
  523. throw new NumberFormatException("Unable to parse " + value);
  524. }
  525. c = value.charAt(index++);
  526. if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
  527. throw new NumberFormatException("Unable to parse " + value);
  528. }
  529. int number = c - CharPool.NUMBER_0;
  530. result *= 10;
  531. if (result < (limit + number)) {
  532. throw new NumberFormatException("Unable to parse " + value);
  533. }
  534. result -= number;
  535. }
  536. if (negative) {
  537. return result;
  538. }
  539. return -result;
  540. }
  541. public static long[] getLongValues(Object value) {
  542. return getLongValues(value, DEFAULT_LONG_VALUES);
  543. }
  544. public static long[] getLongValues(Object value, long[] defaultValue) {
  545. if (value instanceof String[]) {
  546. return getLongValues((String[])value, defaultValue);
  547. }
  548. if (value instanceof long[]) {
  549. return (long[])value;
  550. }
  551. if (value instanceof Number[]) {
  552. Number[] numbers = (Number[])value;
  553. long[] values = new long[numbers.length];
  554. for (int i = 0; i < values.length; i++) {
  555. values[i] = numbers[i].longValue();
  556. }
  557. return values;
  558. }
  559. return defaultValue;
  560. }
  561. public static long[] getLongValues(String[] values) {
  562. return getLongValues(values, DEFAULT_LONG_VALUES);
  563. }
  564. public static long[] getLongValues(String[] values, long[] defaultValue) {
  565. if (values == null) {
  566. return defaultValue;
  567. }
  568. long[] longValues = new long[values.length];
  569. for (int i = 0; i < values.length; i++) {
  570. longValues[i] = getLong(values[i]);
  571. }
  572. return longValues;
  573. }
  574. public static Number getNumber(Object value) {
  575. return getNumber(value, DEFAULT_NUMBER);
  576. }
  577. public static Number getNumber(Object value, Number defaultValue) {
  578. return get(value, defaultValue);
  579. }
  580. public static Number getNumber(String value) {
  581. return getNumber(value, DEFAULT_NUMBER);
  582. }
  583. public static Number getNumber(String value, Number defaultValue) {
  584. return get(value, defaultValue);
  585. }
  586. public static Number[] getNumberValues(Object value) {
  587. return getNumberValues(value, DEFAULT_NUMBER_VALUES);
  588. }
  589. public static Number[] getNumberValues(
  590. Object value, Number[] defaultValue) {
  591. if (value instanceof String[]) {
  592. return getNumberValues((String[])value, defaultValue);
  593. }
  594. if (value instanceof Number[]) {
  595. return (Number[])value;
  596. }
  597. return defaultValue;
  598. }
  599. public static Number[] getNumberValues(String[] values) {
  600. return getNumberValues(values, DEFAULT_NUMBER_VALUES);
  601. }
  602. public static Number[] getNumberValues(
  603. String[] values, Number[] defaultValue) {
  604. if (values == null) {
  605. return defaultValue;
  606. }
  607. Number[] numberValues = new Number[values.length];
  608. for (int i = 0; i < values.length; i++) {
  609. numberValues[i] = getNumber(values[i]);
  610. }
  611. return numberValues;
  612. }
  613. public static Object getObject(Object value) {
  614. return getObject(value, DEFAULT_OBJECT);
  615. }
  616. public static Object getObject(Object value, Object defaultValue) {
  617. if (value == null) {
  618. return defaultValue;
  619. }
  620. return value;
  621. }
  622. public static short getShort(Object value) {
  623. return getShort(value, DEFAULT_SHORT);
  624. }
  625. public static short getShort(Object value, short defaultValue) {
  626. return get(value, defaultValue);
  627. }
  628. public static short getShort(String value) {
  629. return getShort(value, DEFAULT_SHORT);
  630. }
  631. public static short getShort(String value, short defaultValue) {
  632. return get(value, defaultValue);
  633. }
  634. public static short getShortStrict(String value) {
  635. int i = getIntegerStrict(value);
  636. if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
  637. throw new NumberFormatException("Out of range value " + value);
  638. }
  639. return (short)i;
  640. }
  641. public static short[] getShortValues(Object value) {
  642. return getShortValues(value, DEFAULT_SHORT_VALUES);
  643. }
  644. public static short[] getShortValues(Object value, short[] defaultValue) {
  645. if (value instanceof String[]) {
  646. return getShortValues((String[])value, defaultValue);
  647. }
  648. if (value instanceof short[]) {
  649. return (short[])value;
  650. }
  651. return defaultValue;
  652. }
  653. public static short[] getShortValues(String[] values) {
  654. return getShortValues(values, DEFAULT_SHORT_VALUES);
  655. }
  656. public static short[] getShortValues(
  657. String[] values, short[] defaultValue) {
  658. if (values == null) {
  659. return defaultValue;
  660. }
  661. short[] shortValues = new short[values.length];
  662. for (int i = 0; i < values.length; i++) {
  663. shortValues[i] = getShort(values[i]);
  664. }
  665. return shortValues;
  666. }
  667. public static String getString(Object value) {
  668. return getString(value, DEFAULT_STRING);
  669. }
  670. public static String getString(Object value, String defaultValue) {
  671. return get(value, defaultValue);
  672. }
  673. public static String getString(String value) {
  674. return getString(value, DEFAULT_STRING);
  675. }
  676. public static String getString(String value, String defaultValue) {
  677. return get(value, defaultValue);
  678. }
  679. public static String[] getStringValues(Object value) {
  680. return getStringValues(value, DEFAULT_STRING_VALUES);
  681. }
  682. public static String[] getStringValues(
  683. Object value, String[] defaultValue) {
  684. if (value instanceof String[]) {
  685. return getStringValues((String[])value, defaultValue);
  686. }
  687. return defaultValue;
  688. }
  689. public static String[] getStringValues(
  690. Object[] values, String[] defaultValue) {
  691. if (values == null) {
  692. return defaultValue;
  693. }
  694. String[] stringValues = new String[values.length];
  695. for (int i = 0; i < values.length; i++) {
  696. stringValues[i] = String.valueOf(values[i]);
  697. }
  698. return stringValues;
  699. }
  700. public static String[] getStringValues(String[] values) {
  701. return getStringValues(values, DEFAULT_STRING_VALUES);
  702. }
  703. private static int _parseInt(String value, int defaultValue) {
  704. int length = value.length();
  705. if (length <= 0) {
  706. return defaultValue;
  707. }
  708. int pos = 0;
  709. int limit = -Integer.MAX_VALUE;
  710. boolean negative = false;
  711. char c = value.charAt(0);
  712. if (c < CharPool.NUMBER_0) {
  713. if (c == CharPool.MINUS) {
  714. limit = Integer.MIN_VALUE;
  715. negative = true;
  716. }
  717. else if (c != CharPool.PLUS) {
  718. return defaultValue;
  719. }
  720. if (length == 1) {
  721. return defaultValue;
  722. }
  723. pos++;
  724. }
  725. int smallLimit = limit / 10;
  726. int result = 0;
  727. while (pos < length) {
  728. if (result < smallLimit) {
  729. return defaultValue;
  730. }
  731. c = value.charAt(pos++);
  732. if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
  733. return defaultValue;
  734. }
  735. int number = c - CharPool.NUMBER_0;
  736. result *= 10;
  737. if (result < (limit + number)) {
  738. return defaultValue;
  739. }
  740. result -= number;
  741. }
  742. if (negative) {
  743. return result;
  744. }
  745. return -result;
  746. }
  747. private static long _parseLong(String value, long defaultValue) {
  748. int length = value.length();
  749. if (length <= 0) {
  750. return defaultValue;
  751. }
  752. int pos = 0;
  753. long limit = -Long.MAX_VALUE;
  754. boolean negative = false;
  755. char c = value.charAt(0);
  756. if (c < CharPool.NUMBER_0) {
  757. if (c == CharPool.MINUS) {
  758. limit = Long.MIN_VALUE;
  759. negative = true;
  760. }
  761. else if (c != CharPool.PLUS) {
  762. return defaultValue;
  763. }
  764. if (length == 1) {
  765. return defaultValue;
  766. }
  767. pos++;
  768. }
  769. long smallLimit = limit / 10;
  770. long result = 0;
  771. while (pos < length) {
  772. if (result < smallLimit) {
  773. return defaultValue;
  774. }
  775. c = value.charAt(pos++);
  776. if ((c < CharPool.NUMBER_0) || (c > CharPool.NUMBER_9)) {
  777. return defaultValue;
  778. }
  779. int number = c - CharPool.NUMBER_0;
  780. result *= 10;
  781. if (result < (limit + number)) {
  782. return defaultValue;
  783. }
  784. result -= number;
  785. }
  786. if (negative) {
  787. return result;
  788. }
  789. return -result;
  790. }
  791. private static short _parseShort(String value, short defaultValue) {
  792. int i = _parseInt(value, defaultValue);
  793. if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE)) {
  794. return defaultValue;
  795. }
  796. return (short)i;
  797. }
  798. }