net.awairo.favdler.twitter.FavoriteListItems.java Source code

Java tutorial

Introduction

Here is the source code for net.awairo.favdler.twitter.FavoriteListItems.java

Source

/*
 * FavDownloader
 *
 * (c) 2014 alalwww
 * https://github.com/alalwww
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */

package net.awairo.favdler.twitter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import lombok.Getter;
import lombok.experimental.Accessors;
import lombok.extern.log4j.Log4j2;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Status;

/**
 * ???.
 *
 * @author alalwww
 */
@Log4j2
@Getter
@Accessors(fluent = true)
public final class FavoriteListItems {

    private final ObservableList<Tweet> list;
    /** . */
    private int remaining;
    /** API?. */
    private int limit;
    /** ?. */
    private int resetTimeInSeconds;
    /** ???. */
    private int secondsUntilReset;

    /**
     * Constructor.
     *
     * @param responseList ?
     */
    public FavoriteListItems(ResponseList<Status> responseList) {
        this(responseList, responseList.getRateLimitStatus());
    }

    /**
     * Constructor.
     *
     * @param rateLimitStatus API
     */
    public FavoriteListItems(RateLimitStatus rateLimitStatus) {
        this(Collections.emptyList(), rateLimitStatus);
    }

    private FavoriteListItems(List<Status> list, RateLimitStatus rateLimitStatus) {

        //????????????
        this.list = FXCollections.observableArrayList(list.stream().map(Tweet::new).collect(Collectors.toList()));

        updateRateLimitStatus(rateLimitStatus);

        log.debug("???{} API{}/{} ??{}", list.size(), remaining, limit,
                secondsUntilReset);
    }

    private void updateRateLimitStatus(RateLimitStatus status) {
        limit = status.getLimit();
        remaining = status.getRemaining();
        resetTimeInSeconds = status.getResetTimeInSeconds();
        secondsUntilReset = status.getSecondsUntilReset();
    }

    /**
     * @param result ??????????
     */
    public void update(ResponseList<Status> responseList) {

        list.setAll(Stream.concat(list.stream(), responseList.stream().map(Tweet::new))
                .sorted(Comparator.comparingLong(Tweet::getId).reversed()).collect(Collectors.toList()));

        updateRateLimitStatus(responseList.getRateLimitStatus());

        log.debug("??{} API{}/{} ??{}", responseList.size(), remaining, limit,
                secondsUntilReset);
    }

    /**
     * @return ?ID
     */
    public Optional<Long> latestId() {
        return list.stream().map(Tweet::getId).max(Long::compare);
    }

    /**
     * @return ???ID
     */
    public Optional<Long> oldestId() {
        return list.stream().map(Tweet::getId).min(Long::compare);
    }

}