简体   繁体   中英

Filtering a set by enum type

I have created a deadline tracker to manage how much work someone has for one period of time.

The deadline tracker separates issues into "workload periods", these are collections of issues that share the same deadline. The status of the periods are type enum and can be either UNSCHEDULED, FUTURE, FREEZE, or OVERDUE.

However, I am having a problem where WorkloadPeriods are being defined as "in freeze" if only some of the issues are in freeze. Each issue has a freezeLength, deadlineDate and deadlineStatus.

I want to separate these "in freeze" issues into a separate WorkloadPeriod. How do I filter the issues by both deadlineDate and deadlineStatus to stop this problem occurring?

public static class WorkloadPeriod implements Comparable<WorkloadPeriod>
{
    private final LocalDate start;

    @Nullable
    private final LocalDate deadline;

    private final SortedSet<Issue> issues = new TreeSet<>();

    private DeadlineStatus deadlineStatus;

    private Stream inFreeze;

    public WorkloadPeriod(final LocalDate start, final Iterable<Issue> issues)
    {
        this.start = start;
        deadline = StreamSupport.stream(issues.spliterator(), false)
                                .map(Issue::getDeadline)
                                .filter(Optional::isPresent)
                                .map(Optional::get)
                                .max(Ordering.natural())
                                .orElse(null);
        inFreeze = StreamSupport.stream(issues.spliterator(), false)
                                .map(Issue::getDeadlineStatus)
                                .filter(deadlineStatus1 -> deadlineStatus1 == DeadlineStatus.FREEZE);
        Iterables.addAll(this.issues, issues);

        final int defaultFreezeLengthDays = StreamSupport.stream(issues.spliterator(), false)
                                                  .filter(i -> i.getFields() != null
                                                               && i.getFields().getProject() != null
                                                               && i.getFields().getProject().getFreezeLengthDays() != 0)
                                                  .mapToInt(i -> i.getFields().getProject().getFreezeLengthDays())
                                                         .max().orElse(0);
        final LocalDate freezeDate = deadline == null ? null : deadline.minus(defaultFreezeLengthDays, ChronoUnit.DAYS);

        deadlineStatus = DeadlineStatus.getDeadlineStatus(LocalDate.now(), freezeDate, deadline);
    }

    @Override
    public int compareTo(@Nonnull final WorkloadPeriod o)
    {
        return new CompareToBuilder()
            .append(getStart(), o.getStart())
            .append(getDeadline(), o.getDeadline(), new OptionalComparator())
            .append(getIssues().size(), o.getIssues().size())
            .append(hashCode(), o.hashCode())
            .toComparison();
    }

    @Override
    public boolean equals(final Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }
        final WorkloadPeriod workload = (WorkloadPeriod) o;
        return Objects.equals(getStart(), workload.getStart()) &&
               Objects.equals(getDeadline(), workload.getDeadline()) &&
               Objects.equals(getIssues(), workload.getIssues());
    }

    public Optional<LocalDate> getDeadline()
    {
        return Optional.ofNullable(deadline);
    }

    public DeadlineStatus getDeadlineStatus()
    {
        return deadlineStatus;
    }

    public SortedSet<Issue> getIssues()
    {
        return issues;
    }
}

Can you reframe your question so it's easier to understand? How does the Issue class look like? I can only give a high level answer here since I don't know the actual problem.

I'd suggest using the Java 8 groupingBy clause to create a Map< Issue.Type, Set< type>>. Check it out and if you have issues with this approach just use an EnumMap instead.

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