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

it.auties.whatsapp.model.signal.session.Session Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
package it.auties.whatsapp.model.signal.session;

import lombok.Builder;
import lombok.NonNull;
import lombok.extern.jackson.Jacksonized;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Builder
@Jacksonized
public record Session(Set<@NonNull SessionState> states) {
    public Session(Set states) {
        this.states = Objects.requireNonNullElseGet(states, ConcurrentHashMap::newKeySet);
    }

    public Session() {
        this(ConcurrentHashMap.newKeySet());
    }

    public Session closeCurrentState() {
        var currentState = currentState();
        currentState.ifPresent(value -> value.closed(true));
        return this;
    }

    public Optional currentState() {
        return states.stream()
                .filter(state -> !state.closed())
                .findFirst();
    }

    public boolean hasState(int version, byte[] baseKey) {
        return states.stream().anyMatch(state -> state.contentEquals(version, baseKey));
    }

    public Optional findState(int version, byte[] baseKey) {
        return states.stream().filter(state -> state.contentEquals(version, baseKey)).findFirst();
    }

    public void addState(SessionState state) {
        states.add(state);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy