All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.triceo.robozonky.app.investing.SessionState Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta-5
Show newest version
/*
 * Copyright 2017 Lukáš Petrovický
 *
 * 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 com.github.triceo.robozonky.app.investing;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.github.triceo.robozonky.api.strategies.LoanDescriptor;
import com.github.triceo.robozonky.internal.api.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class SessionState {

    private static final Logger LOGGER = LoggerFactory.getLogger(SessionState.class);
    private static final State.ClassSpecificState STATE = State.INSTANCE.forClass(Session.class);
    private static final String SEEN_INVESTMENTS_ID = "seenInvestments",
            UNTOUCHABLE_INVESTMENTS_ID = "untouchableInvestments";

    private static Stream findLoanWithId(final int loanId,
                                                         final Collection knownLoans) {
        final Optional maybeLoan =
                knownLoans.stream().filter(loan -> loan.getLoan().getId() == loanId).findFirst();
        return maybeLoan.map(Stream::of).orElse(Stream.empty());
    }

    private static Collection readInvestments(final Collection knownLoans,
                                                              final String propertyName) {
        final Optional result = SessionState.STATE.getValue(propertyName);
        return result.map(s -> Stream.of(s.split(","))
                .map(Integer::parseInt)
                .distinct()
                .sorted()
                .flatMap(loanId -> SessionState.findLoanWithId(loanId, knownLoans))
                .collect(Collectors.toSet()))
                .orElse(new LinkedHashSet<>(0));
    }

    private static Collection readUntouchableInvestments(final Collection knownLoans) {
        return SessionState.readInvestments(knownLoans, SessionState.UNTOUCHABLE_INVESTMENTS_ID);
    }

    private static Collection readSeenInvestments(final Collection knownLoans) {
        return SessionState.readInvestments(knownLoans, SessionState.SEEN_INVESTMENTS_ID);
    }

    private static void writeInvestments(final String propertyName,
                                         final Collection rejectedInvestments) {
        final String result = rejectedInvestments.stream()
                .map(l -> l.getLoan().getId())
                .distinct()
                .sorted()
                .map(String::valueOf)
                .collect(Collectors.joining(","));
        SessionState.STATE.setValue(propertyName, result);
    }

    private static void writeUntouchableInvestments(final Collection rejectedInvestments) {
        SessionState.writeInvestments(SessionState.UNTOUCHABLE_INVESTMENTS_ID, rejectedInvestments);
    }

    private static void writeSeenInvestments(final Collection seenInvestments) {
        SessionState.writeInvestments(SessionState.SEEN_INVESTMENTS_ID, seenInvestments);
    }

    private final Collection discardedLoans, seenLoans;

    public SessionState(final Collection marketplace) {
        discardedLoans = SessionState.readUntouchableInvestments(marketplace);
        SessionState.LOGGER.debug("Loans previously discarded: {}", discardedLoans);
        seenLoans = SessionState.readSeenInvestments(marketplace);
        SessionState.LOGGER.debug("Loans previously seen: {}", seenLoans);
    }

    public synchronized Collection getDiscardedLoans() {
        return Collections.unmodifiableCollection(discardedLoans);
    }

    public synchronized Collection getSeenLoans() {
        return Collections.unmodifiableCollection(seenLoans);
    }

    synchronized void discard(final LoanDescriptor loan) {
        this.discardedLoans.add(loan);
        SessionState.writeUntouchableInvestments(this.discardedLoans);
    }

    synchronized void skip(final LoanDescriptor loan) {
        this.seenLoans.add(loan);
        SessionState.writeSeenInvestments(this.seenLoans);
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy