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

org.unlaxer.tinyexpression.evaluator.javacode.ExpressionOrLiteral Maven / Gradle / Ivy

There is a newer version: 1.4.4
Show newest version
package org.unlaxer.tinyexpression.evaluator.javacode;

import java.util.Optional;
import java.util.function.Function;

import org.unlaxer.tinyexpression.evaluator.javacode.SimpleJavaCodeBuilder.Kind;
import org.unlaxer.util.Either;

public class ExpressionOrLiteral extends Either {

  SimpleJavaCodeBuilder returning;
  
	private ExpressionOrLiteral(String raw, String mustWrap) {
		super(raw, mustWrap);
	}

	public static ExpressionOrLiteral literalOf(String literal) {
		if (literal == null) {
			throw new IllegalArgumentException("must be not null");
		}
		return new ExpressionOrLiteral(null, literal);
	}

	public static ExpressionOrLiteral expressionOf(String expresion) {
		if (expresion == null) {
			throw new IllegalArgumentException("must be not null");
		}
		return new ExpressionOrLiteral(expresion, null);
	}

	public String toString() {
		return apply(Function.identity(), word -> "\"" + word + "\"");
//		return apply(Function.identity(),Function.identity());
	}
	
	public ExpressionOrLiteral setReturning(SimpleJavaCodeBuilder returning) {
	  this.returning = returning;
	  return this;
	}
	
	public Optional getReturning(){
	  return Optional.ofNullable(returning);
	}
	
	public void populateTo(SimpleJavaCodeBuilder destinationBuilder,Kind... kinds ) {
	  if(returning == null) {
	    return;
	  }
	  
	  for (Kind kind : kinds) {
      StringBuilder builder = returning.getBuilder(kind);
      if(builder.length()>0) {
        destinationBuilder.getBuilder(kind).append(builder.toString());
      }
    }
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy