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

org.hidetake.groovy.ssh.interaction.Rule.groovy Maven / Gradle / Ivy

There is a newer version: 2.11.2
Show newest version
package org.hidetake.groovy.ssh.interaction

import groovy.transform.EqualsAndHashCode

/**
 * A rule of interaction with the stream.
 *
 * @author Hidetake Iwata
 */
@EqualsAndHashCode
class Rule {

    final Map condition

    final StreamRule streamRule
    final BufferRule bufferRule

    final Closure action

    def Rule(Map condition1, Closure action1) {
        condition = condition1
        action = action1

        streamRule = StreamRule.Factory.create(condition.from)
        bufferRule = BufferRule.Factory.create(condition)
    }

    MatchResult match(Stream stream, Buffer buffer) {
        if (streamRule.matches(stream)) {
            def matchResult = bufferRule.match(buffer)
            if (matchResult != null) {
                new MatchResult(this, matchResult)
            } else {
                null
            }
        } else {
            null
        }
    }

    String toString() {
        "${Rule.simpleName}${condition}"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy