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; 029 030import javax.naming.NamingEnumeration; 031 032/** 033 * Utility methods for type casting. 034 */ 035@SuppressWarnings("unchecked") 036public final class CastUtils { 037 038 private CastUtils() { 039 //utility class, never constructed 040 } 041 042 public static <T, U> Map<T, U> cast(Map<?, ?> p) { 043 return (Map<T, U>) p; 044 } 045 046 public static <T, U> Map<T, U> cast(Map<?, ?> p, Class<T> t, Class<U> u) { 047 return (Map<T, U>) p; 048 } 049 050 public static <T> Collection<T> cast(Collection<?> p) { 051 return (Collection<T>) p; 052 } 053 054 public static <T> Collection<T> cast(Collection<?> p, Class<T> cls) { 055 return (Collection<T>) p; 056 } 057 058 public static <T> List<T> cast(List<?> p) { 059 return (List<T>) p; 060 } 061 062 public static <T> List<T> cast(List<?> p, Class<T> cls) { 063 return (List<T>) p; 064 } 065 066 public static <T> Iterator<T> cast(Iterator<?> p) { 067 return (Iterator<T>) p; 068 } 069 070 public static <T> Iterator<T> cast(Iterator<?> p, Class<T> cls) { 071 return (Iterator<T>) p; 072 } 073 074 public static <T> Set<T> cast(Set<?> p) { 075 return (Set<T>) p; 076 } 077 078 public static <T> Set<T> cast(Set<?> p, Class<T> cls) { 079 return (Set<T>) p; 080 } 081 082 public static <T> Queue<T> cast(Queue<?> p) { 083 return (Queue<T>) p; 084 } 085 086 public static <T> Queue<T> cast(Queue<?> p, Class<T> cls) { 087 return (Queue<T>) p; 088 } 089 090 public static <T> Deque<T> cast(Deque<?> p) { 091 return (Deque<T>) p; 092 } 093 094 public static <T> Deque<T> cast(Deque<?> p, Class<T> cls) { 095 return (Deque<T>) p; 096 } 097 098 public static <T, U> Hashtable<T, U> cast(Hashtable<?, ?> p) { 099 return (Hashtable<T, U>) p; 100 } 101 102 public static <T, U> Hashtable<T, U> cast(Hashtable<?, ?> p, Class<T> pc, Class<U> uc) { 103 return (Hashtable<T, U>) p; 104 } 105 106 public static <T, U> Map.Entry<T, U> cast(Map.Entry<?, ?> p) { 107 return (Map.Entry<T, U>) p; 108 } 109 110 public static <T, U> Map.Entry<T, U> cast(Map.Entry<?, ?> p, Class<T> pc, Class<U> uc) { 111 return (Map.Entry<T, U>) p; 112 } 113 114 public static <T> Enumeration<T> cast(Enumeration<?> p) { 115 return (Enumeration<T>) p; 116 } 117 118 public static <T> NamingEnumeration<T> cast(NamingEnumeration<?> p) { 119 return (NamingEnumeration<T>) p; 120 } 121 122 public static <T> Class<T> cast(Class<?> p) { 123 return (Class<T>) p; 124 } 125 126 public static <T> Class<T> cast(Class<?> p, Class<T> cls) { 127 return (Class<T>) p; 128 } 129 130 public static <T> Future<T> cast(Future<?> p) { 131 return (Future<T>) p; 132 } 133}