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

org.khasanof.executors.matcher.SimpleVideoMatcher Maven / Gradle / Ivy

The newest version!
package org.khasanof.executors.matcher;

import org.khasanof.annotation.methods.HandleVideo;
import org.khasanof.enums.scopes.VideoScope;
import org.khasanof.models.matcher.MatcherParameters;
import org.khasanof.models.matcher.MatcherProperty;
import org.khasanof.models.matcher.function.PropertyFunction;
import org.khasanof.service.expression.ExpressionMatcherService;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Video;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

/**
 * @author Nurislom
 * @see org.khasanof.executors.matcher
 * @since 06.07.2023 23:34
 */
@Component
public class SimpleVideoMatcher extends GenericMatcher {

    private final Set matcherProperties = new HashSet<>();

    public SimpleVideoMatcher(ExpressionMatcherService expressionMatcherService) {
        super(expressionMatcherService);
        fillMatcherProperties();
    }

    @Override
    public boolean matcher(HandleVideo annotation, Message value) {
        return expressionMatcherService.match(createParameters(annotation, value));
    }

    private MatcherParameters createParameters(HandleVideo annotation, Message value) {
        return new MatcherParameters(value, annotation.property(), annotation.match(), annotation.value(), matcherProperties);
    }

    void fillMatcherProperties() {
        matcherProperties.add(new MatcherProperty(VideoScope.WIDTH, videoPropertyFunction(Video::getWidth)));
        matcherProperties.add(new MatcherProperty(VideoScope.HEIGHT, videoPropertyFunction(Video::getHeight)));
        matcherProperties.add(new MatcherProperty(VideoScope.DURATION, videoPropertyFunction(Video::getDuration)));
        matcherProperties.add(new MatcherProperty(VideoScope.FILE_NAME, videoPropertyFunction(Video::getFileName)));
        matcherProperties.add(new MatcherProperty(VideoScope.MIME_TYPE, videoPropertyFunction(Video::getMimeType)));
        matcherProperties.add(new MatcherProperty(VideoScope.FILE_SIZE, videoPropertyFunction(Video::getFileSize)));
        matcherProperties.add(new MatcherProperty(VideoScope.CAPTION, (PropertyFunction) Message::getCaption));
    }

    private PropertyFunction videoPropertyFunction(Function function) {
        return message -> function.apply(message.getVideo());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy