简体   繁体   中英

Building a Sort object based on Map<Enum, Enum>

I would like to build a Sort object based on Map<Column, Direction> . I have a problem with the fact that the Sort class only has a private constructor, it just has to be created by the static method by() or and() , therefore I have a problem with initialising the sort object with the first element from the map.

private Sort buildSort(Map<WorklistColumn, Direction> columnsDirectionsmap){
        Sort sort = by("wartość inicjalna której nie chcemy", Direction.Ascending);
        for (Map.Entry<WorklistColumn, Direction> columnWithDirection : columnsDirectionsmap.entrySet()) {
            sort.and(columnWithDirection.getKey().toString(), columnWithDirection.getValue());
        }
        return sort;
    }

public class Sort {
    private List<Column> columns = new ArrayList();

    private Sort() {
    }

    public static Sort by(String column) {
        return (new Sort()).and(column);
    }

    public static Sort by(String column, Direction direction) {
        return (new Sort()).and(column, direction);
    }
    public Sort and(String name) {
        this.columns.add(new Column(name));
        return this;
    }

    public Sort and(String name, Direction direction) {
        this.columns.add(new Column(name, direction));
        return this;
    }

Build a Sort object from a map

I think the question is about the fact that to fully configure a Sort object from your map, you need to use the first map entry in conjunction with Sort.by() , and then use all the other entries in conjunction with Sort.and() . That is, the first entry requires different handling than the rest.

There are lots of ways of dealing with that, but the one I'm going to suggest is to work directly with the iterator of the map's entry set. Something like this:

private Sort buildSort(Map<WorklistColumn, Direction> columnsDirectionsMap) {
    if (columnsDirectionsMap.isEmpty()) {
        throw new NoCriteriaException();  // or whatever
    }

    Iterator<Map.Entry<WorklistColumn, Direction>> criterionIterator =
            columnsDirectionsMap.entrySet().iterator();
    Map.Entry<WorklistColumn, Direction> criterion = criterionIterator.next();
    Sort sort = Sort.by(criterion.key().toString(), criterion.value());

    while (criterionIterator.hasNext()) {
        criterion = criterionIterator.next();
        sort.and(criterion.key().toString(), criterion.value());
    }

    return sort;
}

Do note that depending on the Map implementation involved, the order of the entries may not be easily predictable. I assume that you need control of that order for this approach to work as desired, so it's on you to choose a Map implementation that provides that. A LinkedHashMap might be suitable, for example, but probably not a HashMap .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM