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

org.fulib.scenarios.ast.scope.GroupScope Maven / Gradle / Ivy

package org.fulib.scenarios.ast.scope;

import org.fulib.scenarios.ast.ScenarioGroup;
import org.fulib.scenarios.ast.decl.ClassDecl;
import org.fulib.scenarios.ast.decl.Decl;

import java.util.function.BiConsumer;
import java.util.function.Function;

public class GroupScope extends DelegatingScope
{
   // =============== Fields ===============

   private final ScenarioGroup group;

   // =============== Constructors ===============

   public GroupScope(Scope scope, ScenarioGroup group)
   {
      super(scope);
      this.group = group;
   }

   // =============== Methods ===============

   @Override
   public Decl resolve(String name)
   {
      final ClassDecl classDecl = this.group.getClasses().get(name);
      return classDecl != null ? classDecl : super.resolve(name);
   }

   @Override
   public void list(BiConsumer consumer)
   {
      this.group.getClasses().forEach(consumer);
      super.list(consumer);
   }

   @Override
   public  T resolve(String name, Class type, Function create)
   {
      if (!ClassDecl.class.isAssignableFrom(type) || name.indexOf('/') >= 0)
      {
         return super.resolve(name, type, create);
      }

      final Decl superDecl = super.resolve(name);
      if (superDecl instanceof ClassDecl)
      {
         return (T) superDecl;
      }

      return (T) this.group.getClasses().computeIfAbsent(name, n -> {
         final ClassDecl classDecl = (ClassDecl) create.apply(name);
         classDecl.setGroup(this.group);
         return classDecl;
      });
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy