001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.formatting.template.lexer; 003 004import static java.util.Objects.requireNonNull; 005import java.lang.reflect.InvocationTargetException; 006import java.util.EnumSet; 007import io.cui.tools.formatting.template.FormatterSupport; 008import io.cui.tools.formatting.template.lexer.BracketLexer.Brackets; 009import io.cui.tools.formatting.template.lexer.Lexer.ExpressionLanguage; 010 011/** 012 * @author Eugen Fischer 013 */ 014public final class LexerBuilder { 015 static final EnumSet<ExpressionLanguage> SIMPLE = EnumSet.of(ExpressionLanguage.SIMPLE_SQUARED_BRACKTES, ExpressionLanguage.SIMPLE_CURLY_BRACKETS, ExpressionLanguage.SIMPLE_ANGLE_BRACKET); 016 017 /** 018 * @return the builder with {@link ExpressionLanguage#SIMPLE_SQUARED_BRACKTES} 019 */ 020 public static Builder useSimpleElWithSquaredBrackets() { 021 return new Builder(ExpressionLanguage.SIMPLE_SQUARED_BRACKTES); 022 } 023 024 /** 025 * @param expLanguage {@link ExpressionLanguage} must not be null 026 * 027 * @return builder with the given {@link ExpressionLanguage} 028 */ 029 public static Builder withExpressionLanguage(final ExpressionLanguage expLanguage) { 030 requireNonNull(expLanguage, "ExpressionLanguage must not be null."); 031 return new Builder(expLanguage); 032 } 033 034 035 /** 036 * The builder class 037 */ 038 public static final class Builder { 039 private final ExpressionLanguage expl; 040 private boolean strict = false; 041 042 private Builder(final ExpressionLanguage expLanguage) { 043 expl = expLanguage; 044 } 045 046 private static Brackets getBracketsTypeFor(final ExpressionLanguage expl) { 047 final Brackets type; 048 switch (expl) { 049 case SIMPLE_SQUARED_BRACKTES: 050 type = Brackets.SQUARED_BRACKETS; 051 break; 052 case SIMPLE_CURLY_BRACKETS: 053 type = Brackets.CURLY_BRACKETS; 054 break; 055 case SIMPLE_ANGLE_BRACKET: 056 type = Brackets.ANGLE_BRACKET; 057 break; 058 // $CASES-OMITTED$ 059 default: 060 throw new IllegalArgumentException(expl + " doesn\'t belongs to Simple expression language."); 061 } 062 return type; 063 } 064 065 /** 066 * @param strict enabling the strict mode for pattern matching (only match exact name) 067 * instead of best fitting 068 * @return The concrete {@link Builder} 069 */ 070 public Builder strict(boolean strict) { 071 this.strict = strict; 072 return this; 073 } 074 075 /** 076 * Build the formatter for {@link FormatterSupport} type 077 * 078 * @param source {@link FormatterSupport} type 079 * 080 * @return created formatter 081 */ 082 public <F extends FormatterSupport> Lexer<F> build(final F source) { 083 if (SIMPLE.contains(expl)) { 084 return new BracketLexer<>(source, getBracketsTypeFor(expl), strict); 085 } 086 throw new IllegalArgumentException(expl + " doesn\'t belongs to Simple expression language."); 087 } 088 089 /** 090 * @param sourceType bean type 091 * 092 * @return Lexer for classType 093 * @throws IllegalStateException if access to the class constructor fails or class isn't 094 * public 095 */ 096 public <F extends FormatterSupport> Lexer<F> build(final Class<F> sourceType) { 097 try { 098 return build(sourceType.getDeclaredConstructor().newInstance()); 099 } catch (final RuntimeException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { 100 throw new IllegalStateException("Class \'" + sourceType + "\' should provide an accessible default constructor.", e); 101 } 102 } 103 } 104 105 @java.lang.SuppressWarnings("all") 106 @lombok.Generated 107 private LexerBuilder() { 108 } 109}