org.faster.util.NestParam.java Source code

Java tutorial

Introduction

Here is the source code for org.faster.util.NestParam.java

Source

/*
 * Copyright (c) 2013 @iSQWEN. All rights reserved.
 *
 * 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 org.faster.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ?
 * <p>
 * 1. ???
 * <p>
 * 2. ????????
 * <p>
 * 3. ???????
 * <p>
 * zcxq,lhxq[gyx,gdh],zxq[l1dh[avg],l2dh[sum]]
 * <p>
 * 
 * <p>
 * ?zcxq,lhxq,zxq
 * <p>
 * ?zcxq?lhxq?:gyx,ydh,zxq?l1dh,l2dh
 * <p>
 * ?zcxq?lhxq?,zxq?avg(l1dh)avg(l2dh)
 *
 * @author sqwen
 * @version 1.0, 2012-5-13
 */
public final class NestParam implements Serializable {

    private static final long serialVersionUID = 8228473978708403349L;

    private String name;

    private List<NestParam> childParams;

    // --------------------
    // feature methods
    // --------------------

    public NestParam() {
    }

    public NestParam(String name) {
        this.name = name;
    }

    public NestParam(String name, List<NestParam> childParams) {
        this.name = name;
        this.childParams = childParams;
    }

    public boolean existChildParams() {
        return childParams != null && !childParams.isEmpty();
    }

    public void addChildParam(NestParam childParam) {
        if (childParams == null) {
            childParams = new ArrayList<NestParam>();
        }
        childParams.add(childParam);
    }

    public List<String> getChildParamNames() {
        if (!existChildParams()) {
            return Collections.emptyList();
        }

        List<String> ret = new ArrayList<String>(childParams.size());
        for (NestParam param : childParams) {
            ret.add(param.getName());
        }
        return ret;
    }

    public String getChildParamNames(String delimiter) {
        List<String> names = getChildParamNames();
        return StringUtils.join(names, delimiter);
    }

    @Override
    public String toString() {
        ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE);
        tsb.append("name", name);
        if (existChildParams()) {
            tsb.append("childParams", childParams);
        }
        return tsb.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (childParams == null ? 0 : childParams.hashCode());
        result = prime * result + (name == null ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        NestParam other = (NestParam) obj;
        if (childParams == null) {
            if (other.childParams != null) {
                return false;
            }
        } else if (!childParams.equals(other.childParams)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

    // --------------------
    // getter/setter
    // --------------------

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<NestParam> getChildParams() {
        return childParams;
    }

    public void setChildParams(List<NestParam> childParams) {
        this.childParams = childParams;
    }

}