001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with 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,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.lang;
021
022import java.lang.reflect.Method;
023import java.util.Collection;
024import java.util.Enumeration;
025import java.util.Iterator;
026import java.util.List;
027import java.util.Map;
028import java.util.Set;
029import java.util.SortedMap;
030import java.util.SortedSet;
031import java.util.Vector;
032
033/**
034 * Helpers to co-erce non-generic values into type-safe generics without
035 * having to suppress compiler warnings all over the place.
036 */
037public final class ObjectExtensions {
038
039    private ObjectExtensions() {
040    }
041
042    @SuppressWarnings("unchecked")
043    public static <T> T asT(final Object extendee) {
044        return (T) extendee;
045    }
046
047    @SuppressWarnings("unchecked")
048    public static <T> Enumeration<T> asEnumerationT(final Object extendee, final Class<T> castTo) {
049        return (Enumeration<T>) extendee;
050    }
051
052    @SuppressWarnings("unchecked")
053    public static <T> Iterator<T> asIteratorT(final Object extendee, final Class<T> castTo) {
054        return (Iterator<T>) extendee;
055    }
056
057    @SuppressWarnings("unchecked")
058    public static <T> Collection<T> asCollectionT(final Object extendee, final Class<T> castTo) {
059        return (Collection<T>) extendee;
060    }
061
062    @SuppressWarnings("unchecked")
063    public static <T> List<T> asListT(final Object extendee, final Class<T> castTo) {
064        return (List<T>) extendee;
065    }
066
067    @SuppressWarnings("unchecked")
068    public static <T> Vector<T> asVectorT(final Object extendee, final Class<T> castTo) {
069        return (Vector<T>) extendee;
070    }
071
072    @SuppressWarnings("unchecked")
073    public static <T> Set<T> asSetT(final Object extendee, final Class<T> castTo) {
074        return (Set<T>) extendee;
075    }
076
077    @SuppressWarnings("unchecked")
078    public static <T> SortedSet<T> asSortedSetT(final Object extendee, final Class<T> castTo) {
079        return (SortedSet<T>) extendee;
080    }
081
082    @SuppressWarnings("unchecked")
083    public static <K, V> Map<K, V> asMapKV(final Object extendee, final Class<K> keyCastTo, final Class<V> valueCastTo) {
084        return (Map<K, V>) extendee;
085    }
086
087    @SuppressWarnings("unchecked")
088    public static <K, V> SortedMap<K, V> asSortedMapKV(final Object extendee, final Class<K> keyCastTo, final Class<V> valueCastTo) {
089        return (SortedMap<K, V>) extendee;
090    }
091
092    public static Object[] asArray(final Object extendee) {
093        final Class<?> arrayType = extendee.getClass().getComponentType();
094        if (!arrayType.isPrimitive()) {
095            return (Object[]) extendee;
096        }
097        if (arrayType == char.class) {
098            return ArrayExtensions.asCharToCharacterArray(extendee);
099        } else {
100            return ArrayExtensions.convertPrimitiveToObjectArray(extendee, arrayType);
101        }
102    }
103
104    public static Method getMethod(final Object object, final String methodName, final Class<?>... parameterClass) throws NoSuchMethodException {
105        return ClassExtensions.getMethod(object.getClass(), methodName, parameterClass);
106    }
107
108    public static Method getMethod(final Object object, final String methodName) throws NoSuchMethodException {
109        return ClassExtensions.getMethod(object.getClass(), methodName, new Class[0]);
110    }
111
112    public static String classBaseName(final Object forObject) {
113        final String name = forObject.getClass().getName();
114        return name.substring(name.lastIndexOf('.') + 1);
115    }
116
117    public static void appendToString(final Object extendee, final StringBuffer buf) {
118        buf.append(classBaseName(extendee));
119        buf.append("@");
120        buf.append(Integer.toHexString(extendee.hashCode()));
121    }
122
123}