001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.util; 018 019import java.util.Collection; 020import java.util.Deque; 021import java.util.Enumeration; 022import java.util.Hashtable; 023import java.util.Iterator; 024import java.util.List; 025import java.util.Map; 026import java.util.Queue; 027import java.util.Set; 028import java.util.concurrent.Future; 029import javax.naming.NamingEnumeration; 030 031/** 032 * Utility methods for type casting. 033 */ 034@SuppressWarnings("unchecked") 035public final class CastUtils { 036 037 private CastUtils() { 038 //utility class, never constructed 039 } 040 041 public static <T, U> Map<T, U> cast(Map<?, ?> p) { 042 return (Map<T, U>) p; 043 } 044 045 public static <T, U> Map<T, U> cast(Map<?, ?> p, Class<T> t, Class<U> u) { 046 return (Map<T, U>) p; 047 } 048 049 public static <T> Collection<T> cast(Collection<?> p) { 050 return (Collection<T>) p; 051 } 052 053 public static <T> Collection<T> cast(Collection<?> p, Class<T> cls) { 054 return (Collection<T>) p; 055 } 056 057 public static <T> List<T> cast(List<?> p) { 058 return (List<T>) p; 059 } 060 061 public static <T> List<T> cast(List<?> p, Class<T> cls) { 062 return (List<T>) p; 063 } 064 065 public static <T> Iterator<T> cast(Iterator<?> p) { 066 return (Iterator<T>) p; 067 } 068 069 public static <T> Iterator<T> cast(Iterator<?> p, Class<T> cls) { 070 return (Iterator<T>) p; 071 } 072 073 public static <T> Set<T> cast(Set<?> p) { 074 return (Set<T>) p; 075 } 076 077 public static <T> Set<T> cast(Set<?> p, Class<T> cls) { 078 return (Set<T>) p; 079 } 080 081 public static <T> Queue<T> cast(Queue<?> p) { 082 return (Queue<T>) p; 083 } 084 085 public static <T> Queue<T> cast(Queue<?> p, Class<T> cls) { 086 return (Queue<T>) p; 087 } 088 089 public static <T> Deque<T> cast(Deque<?> p) { 090 return (Deque<T>) p; 091 } 092 093 public static <T> Deque<T> cast(Deque<?> p, Class<T> cls) { 094 return (Deque<T>) p; 095 } 096 097 public static <T, U> Hashtable<T, U> cast(Hashtable<?, ?> p) { 098 return (Hashtable<T, U>) p; 099 } 100 101 public static <T, U> Hashtable<T, U> cast(Hashtable<?, ?> p, Class<T> pc, Class<U> uc) { 102 return (Hashtable<T, U>) p; 103 } 104 105 public static <T, U> Map.Entry<T, U> cast(Map.Entry<?, ?> p) { 106 return (Map.Entry<T, U>) p; 107 } 108 109 public static <T, U> Map.Entry<T, U> cast(Map.Entry<?, ?> p, Class<T> pc, Class<U> uc) { 110 return (Map.Entry<T, U>) p; 111 } 112 113 public static <T> Enumeration<T> cast(Enumeration<?> p) { 114 return (Enumeration<T>) p; 115 } 116 117 public static <T> NamingEnumeration<T> cast(NamingEnumeration<?> p) { 118 return (NamingEnumeration<T>) p; 119 } 120 121 public static <T> Class<T> cast(Class<?> p) { 122 return (Class<T>) p; 123 } 124 125 public static <T> Class<T> cast(Class<?> p, Class<T> cls) { 126 return (Class<T>) p; 127 } 128 129 public static <T> Future<T> cast(Future<?> p) { 130 return (Future<T>) p; 131 } 132}