001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.util; 019 020 import java.util.ArrayList; 021 import java.util.List; 022 import java.util.Map; 023 024 /** 025 * A number of helper methods for working with collections 026 * 027 * @version $Revision: 1.1 $ 028 */ 029 public class CollectionHelper { 030 /** 031 * Sets the value of the entry in the map for the given key, though if the map already contains a value for the 032 * given key then the value is appended to a list of values. 033 * 034 * @param map the map to add the entry to 035 * @param key the key in the map 036 * @param value the value to put in the map 037 */ 038 public static void appendValue(Map map, Object key, Object value) { 039 040 Object oldValue = map.get(key); 041 if (oldValue != null) { 042 List list; 043 if (oldValue instanceof List) { 044 list = (List) oldValue; 045 } 046 else { 047 list = new ArrayList(); 048 list.add(oldValue); 049 } 050 list.add(value); 051 } 052 else { 053 map.put(key, value); 054 } 055 } 056 }