Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ObjectConverter |
|
| 0.0;0 |
1 | /** |
|
2 | * |
|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 | * contributor license agreements. See the NOTICE file distributed with |
|
5 | * this work for additional information regarding copyright ownership. |
|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 | * (the "License"); you may not use this file except in compliance with |
|
8 | * the License. You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | package org.apache.camel.converter; |
|
19 | ||
20 | import org.apache.camel.Converter; |
|
21 | ||
22 | import java.util.Arrays; |
|
23 | import java.util.Collection; |
|
24 | import java.util.Collections; |
|
25 | import java.util.Iterator; |
|
26 | ||
27 | /** |
|
28 | * Some core java.lang based |
|
29 | * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> |
|
30 | * |
|
31 | * @version $Revision: 544115 $ |
|
32 | */ |
|
33 | @Converter |
|
34 | 0 | public class ObjectConverter { |
35 | public static boolean isCollection(Object value) { |
|
36 | // TODO we should handle primitive array types? |
|
37 | 0 | return value instanceof Collection || (value != null && value.getClass().isArray()); |
38 | } |
|
39 | ||
40 | /** |
|
41 | * Creates an iterator over the value if the value is a collection, an Object[] or a primitive type array; otherwise |
|
42 | * to simplify the caller's code, we just create a singleton collection iterator over a single value |
|
43 | */ |
|
44 | @Converter |
|
45 | public static Iterator iterator(Object value) { |
|
46 | 2 | if (value == null) { |
47 | 0 | return Collections.EMPTY_LIST.iterator(); |
48 | } |
|
49 | 2 | else if (value instanceof Collection) { |
50 | 2 | Collection collection = (Collection) value; |
51 | 2 | return collection.iterator(); |
52 | } |
|
53 | 0 | else if (value.getClass().isArray()) { |
54 | // TODO we should handle primitive array types? |
|
55 | 0 | return Arrays.asList(value).iterator(); |
56 | } |
|
57 | else { |
|
58 | 0 | return Collections.singletonList(value).iterator(); |
59 | } |
|
60 | } |
|
61 | ||
62 | /** |
|
63 | * Converts the given value to a boolean, handling strings or Boolean objects; |
|
64 | * otherwise returning false if the value could not be converted to a boolean |
|
65 | */ |
|
66 | @Converter |
|
67 | public static boolean toBool(Object value) { |
|
68 | 0 | Boolean answer = toBoolean(value); |
69 | 0 | if (answer != null) { |
70 | 0 | return answer.booleanValue(); |
71 | } |
|
72 | 0 | return false; |
73 | } |
|
74 | ||
75 | /** |
|
76 | * Converts the given value to a Boolean, handling strings or Boolean objects; |
|
77 | * otherwise returning null if the value cannot be converted to a boolean |
|
78 | */ |
|
79 | @Converter |
|
80 | public static Boolean toBoolean(Object value) { |
|
81 | 16 | if (value instanceof Boolean) { |
82 | 16 | return (Boolean) value; |
83 | } |
|
84 | 0 | if (value instanceof String) { |
85 | 0 | return "true".equalsIgnoreCase(value.toString()) ? Boolean.TRUE : Boolean.FALSE; |
86 | } |
|
87 | 0 | return null; |
88 | } |
|
89 | } |