Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
CollectionHelper |
|
| 3.0;3 |
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.util; |
|
19 | ||
20 | import java.util.ArrayList; |
|
21 | import java.util.List; |
|
22 | import java.util.Map; |
|
23 | ||
24 | /** |
|
25 | * A number of helper methods for working with collections |
|
26 | * |
|
27 | * @version $Revision: 1.1 $ |
|
28 | */ |
|
29 | 0 | public class CollectionHelper { |
30 | /** |
|
31 | * Sets the value of the entry in the map for the given key, though if the map already contains a value for the |
|
32 | * given key then the value is appended to a list of values. |
|
33 | * |
|
34 | * @param map the map to add the entry to |
|
35 | * @param key the key in the map |
|
36 | * @param value the value to put in the map |
|
37 | */ |
|
38 | public static void appendValue(Map map, Object key, Object value) { |
|
39 | ||
40 | 0 | Object oldValue = map.get(key); |
41 | 0 | if (oldValue != null) { |
42 | List list; |
|
43 | 0 | if (oldValue instanceof List) { |
44 | 0 | list = (List) oldValue; |
45 | 0 | } |
46 | else { |
|
47 | 0 | list = new ArrayList(); |
48 | 0 | list.add(oldValue); |
49 | } |
|
50 | 0 | list.add(value); |
51 | 0 | } |
52 | else { |
|
53 | 0 | map.put(key, value); |
54 | } |
|
55 | 0 | } |
56 | } |