FactoryUtils.java
01 /*
02  * Copyright 2010-2012 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.codehaus.griffon.factory;
17 
18 import java.util.List;
19 
20 /**
21  @author Andres Almiray
22  */
23 public final class FactoryUtils {
24     private FactoryUtils() {
25     }
26 
27     public static double toDouble(Object value) {
28         return toDouble(value, 0.0d);
29     }
30 
31     public static double toDouble(Object value, double defaultValue) {
32         if (value instanceof Number) {
33             return ((Numbervalue).doubleValue();
34         else {
35             try {
36                 return Double.parseDouble(String.valueOf(value));
37             catch (NumberFormatException nfe) {
38                 return defaultValue;
39             }
40         }
41     }
42 
43     public static float toFloat(Object value) {
44         return toFloat(value, 0.0f);
45     }
46 
47     public static float toFloat(Object value, float defaultValue) {
48         if (value instanceof Number) {
49             return ((Numbervalue).floatValue();
50         else {
51             try {
52                 return Float.parseFloat(String.valueOf(value));
53             catch (NumberFormatException nfe) {
54                 return defaultValue;
55             }
56         }
57     }
58 
59     public static int toInt(Object value) {
60         return toInt(value, 0);
61     }
62 
63     public static int toInt(Object value, int defaultValue) {
64         if (value instanceof Number) {
65             return ((Numbervalue).intValue();
66         else {
67             try {
68                 return Integer.parseInt(String.valueOf(value));
69             catch (NumberFormatException nfe) {
70                 return defaultValue;
71             }
72         }
73     }
74 
75     public static Long toLong(Object value) {
76         return toLong(value, 0L);
77     }
78 
79     public static Long toLong(Object value, Long defaultValue) {
80         if (value instanceof Number) {
81             return ((Numbervalue).longValue();
82         else {
83             try {
84                 return Long.parseLong(String.valueOf(value));
85             catch (NumberFormatException nfe) {
86                 return defaultValue;
87             }
88         }
89     }
90 
91     public static String parseSize(String size, List<String> sizes) {
92         if (sizes.contains(size)) {
93             return size + "x" + size;
94         }
95         return sizes.get(0"x" + sizes.get(0);
96     }
97 }