com.feilong.core.lang.ObjectUtilTemp.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.lang.ObjectUtilTemp.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.core.lang;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.iterators.ArrayIterator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.tools.jsonlib.JsonUtil;

import static com.feilong.core.bean.ConvertUtil.toList;

/**
 * The Class ObjectUtilTest.
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 */
public class ObjectUtilTemp {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(ObjectUtilTemp.class);

    @Test
    public void test() {
        //Object[] objects =  { "a", "b" }; //?  
        Object[] objects = new String[] { "a", "b" };
        String[] strings = (String[]) objects;

        LOGGER.debug(JsonUtil.format(strings));
    }

    @Test
    public void testIsBoolean() {
        assertEquals(false, isBoolean(null));
        assertEquals(true, isBoolean(false));
    }

    /**
     * Checks if is integer.
     */
    @Test
    public void testIsInteger() {
        assertEquals(false, isInteger(null));
        assertEquals(false, isInteger(false));
        assertEquals(true, isInteger(1));
        assertEquals(false, isInteger(5.56));
    }

    /**
     *  <code>object</code>? {@link Boolean} ?.
     * 
     * @param object
     *            
     * @return true <br>
     *          <code>object</code> null,false
     */
    public static boolean isBoolean(Object object) {
        return object instanceof Boolean;
    }

    /**
     *  <code>object</code>?{@link Integer}.
     * 
     * @param object
     *            
     * @return true <br>
     *          <code>object</code> null,false
     */
    public static boolean isInteger(Object object) {
        return object instanceof Integer;
    }

    /**
     * To iterator.
     */
    @Test
    public void toIterator() {
        String[] array = { "1", "223" };
        LOGGER.debug(JsonUtil.format(toIterator(array)));

        int[] arrays = { 1, 2 };
        LOGGER.debug(JsonUtil.format(toIterator(arrays)));
        LOGGER.debug(JsonUtil.format(new ArrayIterator(arrays)));
        LOGGER.debug(JsonUtil.format(new ArrayIterator(array)));
        //LOGGER.debug(JsonUtil.format(new ArrayIterator(null)));
    }

    /**
     * ?? {@link java.util.Iterator}.
     * <p>
     * ??,,???with no copying<br>
     * ?, ClassCastException  ,Rats -- ,arrayList ?arrayList.iterator()
     * </p>
     * <p>
     * <b>:</b>{@link Arrays#asList(Object...)} list {@link Array}  ArrayList,
     * {@link java.util.AbstractList#add(int, Object)} ,<br>
     * listadd?, {@link java.lang.UnsupportedOperationException}
     * </p>
     * 
     * @param <T>
     *            the generic type
     * @param arrays
     *            ,? , 
     * @return  (null == arrays)  null;<br>
     *         ?arrays?Object[], {@link Arrays#asList(Object...)}?list,? {@link List#iterator()
     *         t}<br>
     *         ,? Object[],?, {@link Array#getLength(Object)}?, {@link Array#get(Object, int)} list
     * @see Arrays#asList(Object...)
     * @see Array#getLength(Object)
     * @see Array#get(Object, int)
     * @see List#iterator()
     * @deprecated
     */
    @Deprecated
    @SuppressWarnings({ "unchecked" })
    public static <T> Iterator<T> toIterator(Object arrays) {
        if (null == arrays) {
            return null;
        }
        List<T> list = null;
        try {
            // ??,,???with no copying
            Object[] objArrays = (Object[]) arrays;
            list = (List<T>) toList(objArrays);
        } catch (ClassCastException e) {
            LOGGER.debug("arrays can not cast to Object[],maybe primitive type,values is:{},{}", arrays,
                    e.getMessage());
            // Rats -- 
            int length = Array.getLength(arrays);
            list = new ArrayList<T>(length);
            for (int i = 0; i < length; ++i) {
                Object object = Array.get(arrays, i);
                list.add((T) object);
            }
        }
        return list.iterator();
    }
}