Unverified Commit 19f15380 authored by samz406's avatar samz406 Committed by GitHub
Browse files

Merge pull request #7 from apache/dev

update
parents b8e4ae3b 039f02a6
Loading
Loading
Loading
Loading
+21 −21
Original line number Diff line number Diff line
@@ -86,21 +86,20 @@ public class CollectionUtils {
     * @return string to map
     */
    public static Map<String, String> stringToMap(String str, String separator, String keyPrefix) {
        if (null == str || "".equals(str)) {
            return null;
        Map<String, String> emptyMap = new HashMap<>(0);
        if (StringUtils.isEmpty(str)) {
            return emptyMap;
        }
        if (null == separator || "".equals(separator)) {
            return null;
        if (StringUtils.isEmpty(separator)) {
            return emptyMap;
        }
        String[] strings = str.split(separator);
        int mapLength = strings.length;
        if ((strings.length % 2) != 0) {
            mapLength = mapLength + 1;
        }

        Map<String, String> map = new HashMap<>(mapLength);
        Map<String, String> map = new HashMap<>(strings.length);
        for (int i = 0; i < strings.length; i++) {
            String[] strArray = strings[i].split("=");
            if (strArray.length != 2) {
                return emptyMap;
            }
            //strArray[0] KEY  strArray[1] VALUE
            if (StringUtils.isEmpty(keyPrefix)) {
                map.put(strArray[0], strArray[1]);
@@ -146,7 +145,7 @@ public class CollectionUtils {
         * @param obj the object
         * @return the maximum frequency of the object
         */
        public final int max(final Object obj) {
        private int max(final Object obj) {
            return Math.max(freqA(obj), freqB(obj));
        }

@@ -156,7 +155,7 @@ public class CollectionUtils {
         * @param obj the object
         * @return the minimum frequency of the object
         */
        public final int min(final Object obj) {
        private int min(final Object obj) {
            return Math.min(freqA(obj), freqB(obj));
        }

@@ -180,10 +179,10 @@ public class CollectionUtils {
            return getFreq(obj, cardinalityB);
        }

        private final int getFreq(final Object obj, final Map<?, Integer> freqMap) {
        private int getFreq(final Object obj, final Map<?, Integer> freqMap) {
            final Integer count = freqMap.get(obj);
            if (count != null) {
                return count.intValue();
                return count;
            }
            return 0;
        }
@@ -203,7 +202,7 @@ public class CollectionUtils {
            return true;
        }

        if ((a == null && b != null) || a != null && b == null) {
        if (a == null || b == null) {
            return false;
        }

@@ -253,12 +252,7 @@ public class CollectionUtils {
    public static <O> Map<O, Integer> getCardinalityMap(final Iterable<? extends O> coll) {
        final Map<O, Integer> count = new HashMap<O, Integer>();
        for (final O obj : coll) {
            final Integer c = count.get(obj);
            if (c == null) {
                count.put(obj, Integer.valueOf(1));
            } else {
                count.put(obj, Integer.valueOf(c.intValue() + 1));
            }
            count.put(obj, count.getOrDefault(obj, 0) + 1);
        }
        return count;
    }
@@ -273,6 +267,12 @@ public class CollectionUtils {
     */
    public static <T extends Object> List<Map<String, Object>> getListByExclusion(List<T> originList, Set<String> exclusionSet) {
        List<Map<String, Object>> instanceList = new ArrayList<>();
        if (exclusionSet == null) {
            exclusionSet = new HashSet<>();
        }
        if (originList == null) {
            return instanceList;
        }
        Map<String, Object> instanceMap;
        for (T instance : originList) {
            Map<String, Object> dataMap = new BeanMap(instance);
+3 −3
Original line number Diff line number Diff line
@@ -291,14 +291,14 @@ public class DateUtils {
     * get some hour of day
     *
     * @param date date
     * @param hours hours
     * @param offsetHour hours
     * @return some hour of day
     * */
    public static Date getSomeHourOfDay(Date date, int hours) {
    public static Date getSomeHourOfDay(Date date, int offsetHour) {
        Calendar cal = Calendar.getInstance();

        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - hours);
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + offsetHour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ public class DependentDateUtils {
    public static List<DateInterval> getLastHoursInterval(Date businessDate, int hourNumber){
        List<DateInterval> dateIntervals = new ArrayList<>();
        for(int index = hourNumber; index > 0; index--){
            Date lastHour = DateUtils.getSomeHourOfDay(businessDate, index);
            Date lastHour = DateUtils.getSomeHourOfDay(businessDate, -index);
            Date beginTime = DateUtils.getStartOfHour(lastHour);
            Date endTime = DateUtils.getEndOfHour(lastHour);
            dateIntervals.add(new DateInterval(beginTime, endTime));
+4 −3
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ package org.apache.dolphinscheduler.common.queue;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.IpUtils;
import org.apache.dolphinscheduler.common.utils.OSUtils;
import org.apache.dolphinscheduler.common.zk.StandaloneZKServerForTest;
import org.apache.dolphinscheduler.common.zk.ZKServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
@@ -35,7 +35,7 @@ import static org.junit.Assert.assertEquals;
/**
 * task queue test
 */
public class TaskQueueImplTest extends StandaloneZKServerForTest {
public class TaskQueueImplTest {

    private static final Logger logger = LoggerFactory.getLogger(TaskQueueImplTest.class);

@@ -43,7 +43,7 @@ public class TaskQueueImplTest extends StandaloneZKServerForTest {

    @Before
    public void before(){
        super.before();
        ZKServer.start();

        tasksQueue = TaskQueueFactory.getTaskQueueInstance();

@@ -57,6 +57,7 @@ public class TaskQueueImplTest extends StandaloneZKServerForTest {
    public void after(){
        //clear all data
        tasksQueue.delete();
        ZKServer.stop();
    }


+58 −8
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
 */
package org.apache.dolphinscheduler.common.utils;

import org.apache.dolphinscheduler.common.Constants;
import org.junit.Assert;
import org.junit.Test;

@@ -26,19 +27,26 @@ public class CollectionUtilsTest {

    @Test
    public void equalLists() {
        Assert.assertTrue(CollectionUtils.equalLists(null,null));
        Assert.assertTrue(CollectionUtils.equalLists(new ArrayList<Integer>(),new ArrayList<Integer>()));
        List<Integer> a = new ArrayList<Integer>();
        a.add(1);
        a.add(2);
        a.add(3);
        List<Integer> b = new ArrayList<Integer>();
        b.add(3);
        b.add(2);
        b.add(1);
        b.add(2);
        Assert.assertTrue(CollectionUtils.equalLists(a, b));
        Assert.assertTrue(CollectionUtils.equalLists(null,null));
        List<Integer> c = new ArrayList<Integer>();
        Assert.assertFalse(CollectionUtils.equalLists(c,null));
        Assert.assertFalse(CollectionUtils.equalLists(c,a));
        a.add(1);
        Assert.assertFalse(CollectionUtils.equalLists(a, b));
        b.add(2);
        Assert.assertFalse(CollectionUtils.equalLists(a, b));
        a.add(2);
        b.add(1);
        a.add(4);
        b.add(2);
        Assert.assertFalse(CollectionUtils.equalLists(a, b));
        Assert.assertFalse(CollectionUtils.equalLists(null, new ArrayList<Integer>()));
        Assert.assertFalse(CollectionUtils.equalLists(new ArrayList<Integer>(), null));
    }

    @Test
@@ -56,7 +64,49 @@ public class CollectionUtilsTest {

    @Test
    public void stringToMap() {
        Map<String, String> a = CollectionUtils.stringToMap("a=b;c=d", ";", "");
        Map<String, String> a = CollectionUtils.stringToMap("a=b;c=d;", ";");
        Assert.assertNotNull(a);
        Assert.assertTrue(a.size() == 2);
        a = CollectionUtils.stringToMap(null, ";");
        Assert.assertTrue(a.isEmpty());
        a = CollectionUtils.stringToMap("", ";");
        Assert.assertTrue(a.isEmpty());
        a = CollectionUtils.stringToMap("a=b;c=d", "");
        Assert.assertTrue(a.isEmpty());
        a = CollectionUtils.stringToMap("a=b;c=d", null);
        Assert.assertTrue(a.isEmpty());
        a = CollectionUtils.stringToMap("a=b;c=d;e=f", ";");
        Assert.assertEquals(a.size(), 3);
        a = CollectionUtils.stringToMap("a;b=f", ";");
        Assert.assertTrue(a.isEmpty());
        a = CollectionUtils.stringToMap("a=b;c=d;e=f;", ";", "test");
        Assert.assertEquals(a.size(), 3);
        Assert.assertNotNull(a.get("testa"));
    }

    @Test
    public void getListByExclusion() {
        Assert.assertNotNull(CollectionUtils.getListByExclusion(null, null));
        List<Integer> originList = new ArrayList<>();
        originList.add(1);
        originList.add(2);
        List<Map<String, Object>> ret = CollectionUtils.getListByExclusion(originList, null);
        Assert.assertEquals(ret.size(), 2);
        ret = CollectionUtils.getListByExclusion(originList, new HashSet<>());
        Assert.assertEquals(ret.size(), 2);
        Assert.assertFalse(ret.get(0).isEmpty());
        Set<String> exclusion = new HashSet<>();
        exclusion.add(Constants.CLASS);
        ret = CollectionUtils.getListByExclusion(originList, exclusion);
        Assert.assertEquals(ret.size(), 2);
        Assert.assertTrue(ret.get(0).isEmpty());
    }

    @Test
    public void isNotEmpty() {
        List<Integer> list = new ArrayList<>();
        Assert.assertFalse(CollectionUtils.isNotEmpty(list));
        Assert.assertFalse(CollectionUtils.isNotEmpty(null));
    }

}
Loading