使用可变参数数组过滤对象集合(仅限普通Java)

时间:2015-04-18 20:40:51

标签: java

我有代码

    List<Tour> list = new ArrayList<>();
    list.add(new Tour("Cruise", "Aircraft", "FB+", 7));
    list.add(new Tour("Shopping", "Train", "FB", 5));
    list.add(new Tour("Shopping", "BUS", "HB", 6));
    list.add(new Tour("Cure", "BUS", "HB", 6));
    list.add(new Tour("Cure", "BUS", "HB", 6));
    list.add(new Tour("Cure", "Aircraft", "HB", 6));
    list.add(new Tour("Cure", "Aircraft", "HB", 6));


    IParameter meal = TourMeal.HB;
    IParameter type = TourType.CURE;
    IParameter transport = TourTransport.AIRCRAFT;

    IParameter[] params = new IParameter[]{meal, type, transport};

    filter(list, params);   /* how to implement this filter? */

我需要的所有内容 - 使用可变参数过滤此列表(以params[]表示)。

不同参数的例子:

{meal, type}; {type, transport}; {meal, transport}

或可能是单身

{meal}, {type}, {transport}

过滤器必须返回带有对象的List<Tour>,来自params[]

的满意参数

这是Tour

/**
 * Object representation of tour. Uses ACL for correct implementation of         hashCode(), equals(), toString()
 * 

 */
public class Tour {

/**
 * All mandatory parameters of Tour
 */
TourBasicParameters tourParams;

/**
 * Constructor for object Tour. Used by parser
 * @param type 
 * @param transport
 * @param meal
 * @param days 
 */
public Tour(String type, String transport, String meal, int days) {
    tourParams = new TourBasicParameters();
    tourParams.setType(type);

    tourParams.setTransport(transport);
    tourParams.setMeal(meal);

    if (days < TourBasicParameters.MINDAYS || days > TourBasicParameters.MAXDAYS) {
        System.out.println("Bad duration. Only [" + TourBasicParameters.MINDAYS + ".." + TourBasicParameters.MAXDAYS + "] allowed");
        System.exit(0);
    }
    tourParams.setDuration(String.valueOf(days));
}

public TourBasicParameters getTourParams() {
    return tourParams;
}

@Override
public int hashCode() {
    // you pick a hard-coded, randomly chosen, non-zero, odd number
    // ideally different for each class
    return new HashCodeBuilder(41, 61).
            append(tourParams).
            toHashCode();
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (obj == this) {
        return true;
    }
    if (obj.getClass() != getClass()) {
        return false;
    }
    Tour rhs = (Tour) obj;
    return new EqualsBuilder()
            .append(tourParams, rhs.tourParams)
            .isEquals();
}

@Override
public String toString() {
    return new ToStringBuilder(this)
            .append("tourParams", tourParams)
            .toString();
    }

}

这是一个TourBasicParameters:

public class TourBasicParameters {

public final static int MAXDAYS = 12;
public final static int MINDAYS = 4;

private IParameter type;
private IParameter transport;
private IParameter meal;
private IParameter duration;


public TourBasicParameters() {
    this.type = null;
    this.transport = null;
    this.meal = null;
    this.duration = null;
}


private IParameter getType() {
    return type;
}

private IParameter getTransport() {
    return transport;
}

private IParameter getMeal() {
    return meal;
}

private IParameter getDuration() {
    return duration;
}

public IParameter[] getParameters()
{
    return new IParameter[] {getType(), getTransport(), getMeal(), getDuration()};
}


public void setType(String type) {

    this.type = TourType.getParameterFromString(type);

}

public void setTransport(String transport) {
    this.transport = TourTransport.getParameterFromString(transport);
}

public void setMeal(String meal) {
    this.meal = TourMeal.getParameterFromString(meal);
}

public void setDuration(String duration) {

    this.duration = TourDuration.fromString(String.valueOf(duration));
}


@Override
public int hashCode() {
    // you pick a hard-coded, randomly chosen, non-zero, odd number
    // ideally different for each class
    return new HashCodeBuilder(41, 61).
            append(type).
            append(transport).
            append(meal).
            append(duration).
            toHashCode();
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (obj == this) {
        return true;
    }
    if (obj.getClass() != getClass()) {
        return false;
    }
    TourBasicParameters rhs = (TourBasicParameters) obj;
    return new EqualsBuilder()
            .append(type, rhs.type)
            .append(transport, rhs)
            .append(meal, rhs.meal)
            .append(duration, rhs.duration)
            .isEquals();
}

@Override
public String toString() {
    return new ToStringBuilder(this)
            .append("Type", type)
            .append("Transport", transport)
            .append("Meal", meal)
            .append("Duration", duration)
            .toString();
}

}

1 个答案:

答案 0 :(得分:0)

免责声明:保证性能不佳。

List<Tour> filter(List<Tour> list, IParameter[] params) {
    List<Tour> filteredList = new ArrayList<>();
    List<IParameter> requiredParameters = Arrays.asList(params);

    for (Tour t : list) {
        if (Arrays.asList(t.getParameters()).containsAll(requiredParameters)) {
            filteredList.add(t);
        }
    }
    return filteredList;
}