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

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

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

import org.khasanof.annotation.methods.HandleAudio;
import org.khasanof.enums.scopes.AudioScope;
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.Audio;
import org.telegram.telegrambots.meta.api.objects.Message;

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

/**
 * @author Nurislom
 * @see org.khasanof.executors.matcher
 * @since 09.07.2023 16:04
 */
@Component
public class SimpleAudioMatcher extends GenericMatcher {

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

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

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

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

    void fillMatcherProperties() {
        matcherProperties.add(new MatcherProperty(AudioScope.TITLE, audioPropertyFunction(Audio::getTitle)));
        matcherProperties.add(new MatcherProperty(AudioScope.DURATION, audioPropertyFunction(Audio::getDuration)));
        matcherProperties.add(new MatcherProperty(AudioScope.FILE_NAME, audioPropertyFunction(Audio::getFileName)));
        matcherProperties.add(new MatcherProperty(AudioScope.FILE_SIZE, audioPropertyFunction(Audio::getFileSize)));
        matcherProperties.add(new MatcherProperty(AudioScope.MIME_TYPE, audioPropertyFunction(Audio::getMimeType)));
        matcherProperties.add(new MatcherProperty(AudioScope.PERFORMER, audioPropertyFunction(Audio::getPerformer)));
        matcherProperties.add(new MatcherProperty(AudioScope.CAPTION, (PropertyFunction) Message::getCaption));
    }

    private PropertyFunction audioPropertyFunction(Function function) {
        return message -> function.apply(message.getAudio());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy