Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ObjectConverter |
|
| 0.0;0 |
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 | package org.apache.camel.converter; |
|
18 | ||
19 | import java.util.Arrays; |
|
20 | import java.util.Collection; |
|
21 | import java.util.Collections; |
|
22 | import java.util.Iterator; |
|
23 | ||
24 | import org.apache.camel.Converter; |
|
25 | ||
26 | /** |
|
27 | * Some core java.lang based <a |
|
28 | * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> |
|
29 | * |
|
30 | * @version $Revision: 563607 $ |
|
31 | */ |
|
32 | @Converter |
|
33 | public class ObjectConverter { |
|
34 | ||
35 | /** |
|
36 | * Utility classes should not have a public constructor. |
|
37 | */ |
|
38 | 0 | private ObjectConverter() { |
39 | 0 | } |
40 | ||
41 | public static boolean isCollection(Object value) { |
|
42 | // TODO we should handle primitive array types? |
|
43 | 0 | return value instanceof Collection || (value != null && value.getClass().isArray()); |
44 | } |
|
45 | ||
46 | /** |
|
47 | * Creates an iterator over the value if the value is a collection, an |
|
48 | * Object[] or a primitive type array; otherwise to simplify the caller's |
|
49 | * code, we just create a singleton collection iterator over a single value |
|
50 | */ |
|
51 | @Converter |
|
52 | public static Iterator iterator(Object value) { |
|
53 | 6 | if (value == null) { |
54 | 0 | return Collections.EMPTY_LIST.iterator(); |
55 | 6 | } else if (value instanceof Collection) { |
56 | 6 | Collection collection = (Collection)value; |
57 | 6 | return collection.iterator(); |
58 | 0 | } else if (value.getClass().isArray()) { |
59 | // TODO we should handle primitive array types? |
|
60 | 0 | return Arrays.asList(value).iterator(); |
61 | } else { |
|
62 | 0 | return Collections.singletonList(value).iterator(); |
63 | } |
|
64 | } |
|
65 | ||
66 | /** |
|
67 | * Converts the given value to a boolean, handling strings or Boolean |
|
68 | * objects; otherwise returning false if the value could not be converted to |
|
69 | * a boolean |
|
70 | */ |
|
71 | @Converter |
|
72 | public static boolean toBool(Object value) { |
|
73 | 0 | Boolean answer = toBoolean(value); |
74 | 0 | if (answer != null) { |
75 | 0 | return answer.booleanValue(); |
76 | } |
|
77 | 0 | return false; |
78 | } |
|
79 | ||
80 | /** |
|
81 | * Converts the given value to a Boolean, handling strings or Boolean |
|
82 | * objects; otherwise returning null if the value cannot be converted to a |
|
83 | * boolean |
|
84 | */ |
|
85 | @Converter |
|
86 | public static Boolean toBoolean(Object value) { |
|
87 | 63 | if (value instanceof Boolean) { |
88 | 63 | return (Boolean)value; |
89 | } |
|
90 | 0 | if (value instanceof String) { |
91 | 0 | return "true".equalsIgnoreCase(value.toString()) ? Boolean.TRUE : Boolean.FALSE; |
92 | } |
|
93 | 0 | return null; |
94 | } |
|
95 | ||
96 | /** |
|
97 | * Returns the boolean value, or null if the value is null |
|
98 | */ |
|
99 | @Converter |
|
100 | public static Boolean toBoolean(Boolean value) { |
|
101 | 771 | if (value != null) { |
102 | 771 | return value.booleanValue(); |
103 | } |
|
104 | 0 | return false; |
105 | } |
|
106 | ||
107 | } |