com.ms.commons.lang.RangeBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.commons.lang.RangeBuilder.java

Source

/*
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with ZXC.com.
 */
package com.ms.commons.lang;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.ms.commons.collection.Wrapper;

/**
 * <pre>
 * ?
 *    ?Comparable?
 *    NumberString??
 * 
 *  RangeBuilder.data(data).property("name").keyName("newName").range();
 *  RangeBuilder.data(data).property("salary").desc().range();
 *  RangeBuilder.data(data).property("id").asc().range();
 * </pre>
 * 
 * @author zxc Apr 12, 2013 2:34:08 PM
 */
public class RangeBuilder {

    public static class Range {

        public Range(String keyName, Object min, Object max) {
            this.setKey(keyName);
            this.setMin(min);
            this.setMax(max);
        }

        public Object getMin() {
            return min;
        }

        public void setMin(Object min) {
            this.min = min;
        }

        public Object getMax() {
            return max;
        }

        public void setMax(Object max) {
            this.max = max;
        }

        private String key;
        private Object min;
        private Object max;

        @Override
        public String toString() {
            return getKey() + ":[" + min + "," + max + "]";
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

    }

    @SuppressWarnings("rawtypes")
    private Collection data;
    private String property;
    private String keyName;
    private List<Object> values = new ArrayList<Object>();
    private boolean asc = true;

    public static RangeBuilder data(Collection<?> data) {
        RangeBuilder rangeBuilder = new RangeBuilder();
        rangeBuilder.data = data;
        return rangeBuilder;
    }

    public RangeBuilder property(String property) {
        this.property = property;
        if (this.keyName == null) {
            this.keyName = property;
        }
        return this;
    }

    public RangeBuilder keyName(String keyName) {
        this.keyName = keyName;
        return this;
    }

    public RangeBuilder defaultValue(Object... defaultValues) {
        values.addAll(Wrapper.collection(ArrayUtils.removeNullElement(defaultValues)));
        return this;
    }

    public RangeBuilder asc() {
        this.asc = true;
        return this;
    }

    public RangeBuilder desc() {
        this.asc = false;
        return this;
    }

    private Object[] _range(boolean asc, List<Object> values) {
        Object min = null, max = null;
        for (Object value : values) {
            if (biggerThan(value, max)) {
                max = value;
            }
            if (smallThan(value, min)) {
                min = value;
            }
        }
        return asc ? new Object[] { min, max } : new Object[] { max, min };
    }

    public Range range() {
        for (Object obj : data) {
            try {
                // 
                Object value = PropertyUtils.getSimpleProperty(obj, property);
                values.add(value);
            } catch (Exception e) {
                throw new RuntimeException(
                        ToStringBuilder.reflectionToString(obj) + "has not property named " + property, e);
            }
        }
        Object[] range = _range(asc, values);
        return new Range(keyName, range[0], range[1]);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private boolean smallThan(Object value, Object min) {
        if (min == null) {
            return value != null;
        }
        if (value == null) {
            return false;
        }
        if (value instanceof Comparable) {
            return ((Comparable) value).compareTo(min) < 0;
        } else {
            // 
            throw new RuntimeException("RangeBuilder??Camparable");
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private boolean biggerThan(Object value, Object max) {
        if (max == null) {
            return value != null;
        }
        if (value == null) {
            return false;
        }
        if (value instanceof Comparable) {
            return ((Comparable) value).compareTo(max) > 0;
        } else {
            // 
            throw new RuntimeException("RangeBuilder??Camparable");
        }
    }

}