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

org.aya.literate.LiterateConsumer Maven / Gradle / Ivy

There is a newer version: 0.35.0
Show newest version
// Copyright (c) 2020-2023 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.literate;

import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableList;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public interface LiterateConsumer extends Consumer {
  @MustBeInvokedByOverriders
  default void accept(@NotNull Literate literate) {
    switch (literate) {
      case Literate.Many many -> many.children().forEach(this);
      case Literate.List items -> items.items().forEach(this);
      case Literate.Unsupported(var children) -> children.forEach(this);
      default -> {}
    }
  }

  interface LiterateExtractinator extends LiterateConsumer {
    @NotNull MutableList result();

    default @NotNull ImmutableSeq extract(Literate literate) {
      accept(literate);
      return result().toImmutableSeq();
    }
  }

  record InstanceExtractinator(
    @NotNull MutableList result, @NotNull Class clazz
  ) implements LiterateExtractinator {

    public InstanceExtractinator(@NotNull Class clazz) {
      this(MutableList.create(), clazz);
    }

    @Override public void accept(@NotNull Literate literate) {
      if (clazz.isInstance(literate)) result.append(clazz.cast(literate));
      LiterateExtractinator.super.accept(literate);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy