View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.util.codingsupport;
19  
20  import org.springframework.util.StringUtils;
21  
22  
23  /**
24   * This class supports methods to handle with numbers. It covers only caps
25   * of class <code>org.springframework.util.NumberUtils</code>.
26   *
27   * @svnLink $Revision: 3881 $;$Date: 2009-08-04 15:22:05 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/codingsupport/NumberUtils.java $
28   *
29   * @author Martin Zeltner (MZE)
30   */
31  public final class NumberUtils {
32  	
33  	/**
34  	 * Hidden constructor.
35  	 */
36  	private NumberUtils() { }
37  
38  	/**
39  	 * Method to test if a given value is inside the given boundaries.
40  	 *
41  	 * @param value
42  	 *            Is the given value to test.
43  	 * @param min
44  	 *            Is the minimum that the value can be.
45  	 * @param max
46  	 *            Is the maximum that the value can be.
47  	 * @return Returns true if <code>value</code> is greater or equals than
48  	 *         minimum and is less or equals than maximum.
49  	 */
50  	public static boolean isNumberInsideBoundaries(
51  		int value, int min, int max) {
52  		return value >= min && value <= max;
53  	}
54  
55  	/**
56  	 * Method to test if a given value is inside the given boundaries.
57  	 *
58  	 * @param value
59  	 *            Is the given value to test.
60  	 * @param min
61  	 *            Is the minimum that the value can be.
62  	 * @param max
63  	 *            Is the maximum that the value can be.
64  	 * @return Returns true if <code>value</code> is greater or equals than
65  	 *         minimum and is less or equals than maximum.
66  	 */
67  	public static boolean isNumberInsideBoundaries(
68  		long value, long min, long max) {
69  		return value >= min && value <= max;
70  	}
71  
72  	/**
73  	 * Method to test if a given value is inside the given boundaries.
74  	 *
75  	 * @param value
76  	 *            Is the given value to test.
77  	 * @param min
78  	 *            Is the minimum that the value can be.
79  	 * @param max
80  	 *            Is the maximum that the value can be.
81  	 * @return Returns true if <code>value</code> is greater or equals than
82  	 *         minimum and is less or equals than maximum.
83  	 */
84  	public static boolean isNumberInsideBoundaries(
85  		double value, double min, double max) {
86  		return value >= min && value <= max;
87  	}
88  	
89  	/**
90  	 * Method to parse a string to an integer.
91  	 *
92  	 * @param s
93  	 *            Is the string to parse.
94  	 * @return Return the <code>Integer</code> object if success else
95  	 *         <code>null</code>.
96  	 */
97  	public static Integer parseToInteger(String s) {
98  		Integer result = null;
99  		if (StringUtils.hasText(s)) {
100 			try {
101 				result = Integer.valueOf(s.trim());
102 			} catch (NumberFormatException e) {
103 				result = null;
104 			}
105 		}
106 		return result;
107 	}
108 	
109 	/**
110 	 * Method to parse a string to a long.
111 	 *
112 	 * @param s
113 	 *            Is the string to parse.
114 	 * @return Return the <code>Long</code> object if success else
115 	 *         <code>null</code>.
116 	 */
117 	public static Long parseToLong(String s) {
118 		Long result = null;
119 		if (StringUtils.hasText(s)) {
120 			try {
121 				result = Long.valueOf(s.trim());
122 			} catch (NumberFormatException e) {
123 				result = null;
124 			}
125 		}
126 		return result;
127 	}
128 
129 	/**
130 	 * Method to parse a string to a double.
131 	 *
132 	 * @param s
133 	 *            Is the string to parse.
134 	 * @return Return the <code>Double</code> object if success else
135 	 *         <code>null</code>.
136 	 */
137 	public static Double parseToDouble(String s) {
138 		Double result = null;
139 		if (StringUtils.hasText(s)) {
140 			try {
141 				double number = Double.parseDouble(s.trim());
142 				result = new Double(number);
143 			} catch (NumberFormatException e) {
144 				result = null;
145 			}
146 		}
147 		return result;
148 	}
149 }