001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixxml.util;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.SortedMap;
027: import java.util.SortedSet;
028: import java.util.Map.Entry;
029:
030: /**
031: * Utility class that provides methods for converting collections.
032: *
033: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
034: */
035: public class Generics {
036: private Generics() {
037: };
038:
039: @SuppressWarnings("unchecked")
040: public static <T> Collection<T> convertCollection(
041: Collection<?> collection) {
042: for (Iterator<?> i = collection.iterator(); i.hasNext();) {
043: T element = (T) i.next();
044: if (element == null) {
045: // Do nothing, just to remove the warning
046: }
047: }
048: return (Collection<T>) collection;
049: }
050:
051: @SuppressWarnings("unchecked")
052: public static <T> Set<T> convertSet(Set<?> set) {
053: for (Iterator<?> i = set.iterator(); i.hasNext();) {
054: T element = (T) i.next();
055: if (element == null) {
056: // Do nothing, just to remove the warning
057: }
058: }
059: return (Set<T>) set;
060: }
061:
062: @SuppressWarnings("unchecked")
063: public static <T> SortedSet<T> convertSortedSet(SortedSet<?> set) {
064: for (Iterator<?> i = set.iterator(); i.hasNext();) {
065: T element = (T) i.next();
066: if (element == null) {
067: // Do nothing, just to remove the warning
068: }
069: }
070: return (SortedSet<T>) set;
071: }
072:
073: @SuppressWarnings("unchecked")
074: public static <T> List<T> convertList(List<?> list) {
075: for (Iterator<?> i = list.iterator(); i.hasNext();) {
076: T element = (T) i.next();
077: if (element == null) {
078: // Do nothing, just to remove the warning
079: }
080: }
081: return (List<T>) list;
082: }
083:
084: @SuppressWarnings("unchecked")
085: public static <T1, T2> Map<T1, T2> convertMap(Map<?, ?> map) {
086: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
087: Entry entry = (Entry) i.next();
088: T1 key = (T1) entry.getKey();
089: T2 value = (T2) entry.getValue();
090: if (key == null && value == null) {
091: // Do nothing, just to remove the warning
092: }
093: }
094: return (Map<T1, T2>) map;
095: }
096:
097: @SuppressWarnings("unchecked")
098: public static <T1, T2> SortedMap<T1, T2> convertSortedMap(
099: SortedMap<?, ?> map) {
100: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
101: Entry entry = (Entry) i.next();
102: T1 key = (T1) entry.getKey();
103: T2 value = (T2) entry.getValue();
104: if (key == null && value == null) {
105: // Do nothing, just to remove the warning
106: }
107: }
108: return (SortedMap<T1, T2>) map;
109: }
110: }
|