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

net.jbock.context.ReadOptionArgumentMethod Maven / Gradle / Ivy

There is a newer version: 5.18
Show newest version
package net.jbock.context;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import net.jbock.util.ErrTokenType;
import net.jbock.util.ExToken;

import javax.inject.Inject;
import java.util.List;

import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static com.squareup.javapoet.ParameterSpec.builder;
import static com.squareup.javapoet.TypeName.BOOLEAN;
import static net.jbock.common.Constants.STRING;
import static net.jbock.common.Constants.STRING_ITERATOR;

@ContextScope
public class ReadOptionArgumentMethod extends CachedMethod {

    @Inject
    ReadOptionArgumentMethod() {
    }

    @Override
    MethodSpec define() {
        ParameterSpec token = builder(STRING, "token").build();
        ParameterSpec it = builder(STRING_ITERATOR, "it").build();
        CodeBlock.Builder code = CodeBlock.builder();
        ParameterSpec unix = builder(BOOLEAN, "unix").build();
        code.addStatement("$T $N = !$N.startsWith($S)", BOOLEAN, unix, token, "--");

        code.add("if ($N && $N.length() > 2)\n", unix, token).indent()
                .addStatement("return $N.substring(2)", token).unindent();

        code.add("if (!$N && $N.contains($S))\n", unix, token, "=").indent()
                .addStatement("return $1N.substring($1N.indexOf('=') + 1)", token).unindent();

        code.add("if ($N.hasNext())\n", it).indent()
                .addStatement("return $N.next()", it).unindent();

        code.addStatement("throw new $T($T.$L, $N)", ExToken.class,
                ErrTokenType.class, ErrTokenType.MISSING_ARGUMENT, token);

        return methodBuilder("readOptionArgument")
                .addException(ExToken.class)
                .addCode(code.build())
                .addParameters(List.of(token, it))
                .returns(STRING)
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy