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

Java tutorial

Introduction

Here is the source code for com.feilong.core.lang.ArrayUtilTemp.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 java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.util.CollectionsUtil;
import com.feilong.core.util.MapUtil;
import com.feilong.test.User;
import com.feilong.tools.jsonlib.JsonUtil;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.core.bean.ConvertUtil.toList;
import static com.feilong.core.date.DateExtensionUtil.getIntervalForView;
import static com.feilong.core.util.MapUtil.newLinkedHashMap;

/**
 * The Class ArrayUtilTest.
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @since 1.0
 */
public class ArrayUtilTemp {

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

    /**
     * ?
     * 11,
     * 22,1
     * 33,
     * 44,1
     * 55,1
     * 66,3
     * 77,
     * 88,1
     * 99,
     * 
     * ?.
     *
     * @since 1.5.5
     */
    @Test
    public void testArrayUtilTest2() {
        Date beginDate = new Date();

        int j = 1;
        int z = 7 * 9;

        int total = 10000;
        List<Integer> list = new ArrayList<Integer>(total / z / 5);
        for (int i = z; i < total; i = z * j) {
            //LOGGER.debug("i:{},j={}", i, j);
            if (i % 5 == 1 && i % 4 == 1 && i % 6 == 3 && i % 8 == 1) {
                list.add(i);
            }
            ++j;
        }
        LOGGER.debug("loop count j=[{}],use time:[{}],\nlist:{}", j, getIntervalForView(beginDate), list);
    }

    /**
     * TestArrayUtilTest.
     */
    @Test
    public void testGroup() {
        Integer[] array = { 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8 };
        Map<Integer, List<Integer>> group = group(array);
        LOGGER.debug(JsonUtil.format(group));

    }

    /**
     * TestArrayUtilTest.
     */
    @Test
    public void testGroup1() {
        String[] array1 = { "", "feilong", "", "?", "" };

        Map<String, List<String>> group1 = group(array1);
        LOGGER.debug(JsonUtil.format(group1));

        Collection<List<String>> values1 = group1.values();
        LOGGER.debug(JsonUtil.format(values1));
    }

    /**
     * Test group object.
     */
    @Test
    public void testGroupObject() {
        User[] users = { new User("", 18), new User("?", 28), new User("", 38),
                new User("", 18), new User("", 28), new User("", 58) };

        Map<Integer, List<User>> group = group(users, "age");
        LOGGER.debug(JsonUtil.format(group));
    }

    /**
     * <code>array</code> .
     * 
     * <p>
     *  {@link LinkedHashMap} ,key,value;key?? <code>array</code>???
     * </p>
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * Integer[] array = { 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8 };
     * Map{@code <Integer, List<Integer>>} group = ArrayUtil.group(array);
     * LOGGER.debug(JsonUtil.format(group));
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     {
     "1":         [
         1,
         1,
         1
     ],
     "2":         [
         2,
         2
     ],
     "3": [3],
     "4": [4],
     "5":         [
         5,
         5
     ],
     "6": [6],
     "7": [7],
     "8":         [
         8,
         8
     ]
     }
     * </pre>
     * 
     * </blockquote>
     *
     * @param <T>
     *            the generic type
     * @param array
     *            the array
     * @return  <code>array</code> null, {@link Collections#emptyMap()}<br>
     * @since 1.0.8
     */
    public static <T> Map<T, List<T>> group(T[] array) {
        if (null == array) {
            return Collections.emptyMap();
        }
        Map<T, List<T>> map = newLinkedHashMap(array.length);
        for (T t : array) {
            MapUtil.putMultiValue(map, t, t);
        }
        return map;
    }

    /**
     *  <code>array</code>, <code>propertyName</code> .
     * 
     * <p>
     * map {@link LinkedHashMap},key??,value??list,?????
     * </p>
     * 
     * <h3>:</h3>
     * <blockquote>
     * 
     * <pre class="code">
     * User[] users = {
     *                  new User("", 18),
     *                  new User("?", 28),
     *                  new User("", 38),
     *                  new User("", 18),
     *                  new User("", 28),
     *                  new User("", 58) };
     * 
     * Map{@code <Integer, List<User>>} group = ArrayUtil.group(users, "age");
     * LOGGER.debug(JsonUtil.format(group));
     * </pre>
     * 
     * <b>:</b>
     * 
     * <pre class="code">
     {
     "18": [{
             "age": 18,
             "name": ""
         },{
             "age": 18,
             "name": ""
         }
     ],
     "28": [{
             "age": 28,
             "name": "?"
         },{
             "age": 28,
             "name": ""
         }
     ],
     "38": [{
         "age": 38,
         "name": ""
     }],
     "58": [{
         "age": 58,
         "name": ""
     }]
     }
     * </pre>
     * 
     * </blockquote>
     *
     * @param <O>
     *            the generic type
     * @param <T>
     *            the generic type
     * @param array
     *            
     * @param propertyName
     *            O??,Possibly indexed and/or nested name of the property to be modified,??
     *            <a href="../bean/BeanUtil.html#propertyName">propertyName</a>
     * @return  <code>objectArray</code>  nullempty, {@link java.util.Collections#emptyMap()} <br>
     *          <code>propertyName</code> null  {@link NullPointerException}<br>
     *          <code>propertyName</code> blank  {@link IllegalArgumentException}<br>
     * @see com.feilong.core.bean.ConvertUtil#toList(Object...)
     * @see com.feilong.core.util.CollectionsUtil#group(java.util.Collection, String)
     * @since 1.0.8
     */
    public static <O, T> Map<T, List<O>> group(O[] array, String propertyName) {
        if (isNullOrEmpty(array)) {
            return Collections.emptyMap();
        }
        Validate.notBlank(propertyName, "propertyName can't be null/empty!");
        return CollectionsUtil.group(toList(array), propertyName);
    }
}