<commit_before>/*
 * MARID, the visual component programming environment.
 * Copyright (C) 2020 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.marid.moan

import java.lang.ref.Cleaner
import java.math.BigInteger
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicReference
import java.util.logging.Level.INFO
import kotlin.reflect.*
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.isSubtypeOf

typealias Closer = (Runnable) -> Runnable

class Context private constructor(
  val name: String,
  val parent: Context?,
  closer: Closer,
  val properties: Properties
): MoanFetcher, AutoCloseable {

  private val queue = ConcurrentLinkedDeque<MoanHolder<*>>()
  private val typedMap = ConcurrentHashMap<KClassifier, ConcurrentLinkedQueue<MoanHolder<*>>>()
  private val namedMap = ConcurrentHashMap<String, MoanHolder<*>>()
  private val closeListeners = ConcurrentLinkedDeque<() -> Unit>()
  private val uid = UIDS.getAndUpdate(BigInteger::inc)

  val path: String = generateSequence(this, { it.parent }).map { it.name }.reduce { a, b -> "$b/$a" }

  init {
    listOf<String>().iterator().asSequence()
    val name = this.path
    val queue = this.queue
    val typedMap = this.typedMap
    val namedMap = this.namedMap
    val closeListeners = this.closeListeners
    val closeTask = { uid: BigInteger ->
      closer(Runnable {
        name.asLogger.log(INFO, "Cleaning")
        close(uid, name, queue, typedMap, namedMap, closeListeners)
      })
    }
    if (parent != null) {
      val uid = this.uid
      val listener = { closeTask(uid).run() }
      val parent = this.parent
      parent.addCloseListener(listener)
      addCloseListener { parent.removeCloseListener(listener) }
    }
    CLEANABLES.computeIfAbsent(uid) { uid -> CLEANER.register(this, closeTask(uid)) }
  }

  fun addCloseListener(listener: () -> Unit) {
    closeListeners.addFirst(listener)
  }
<commit_msg>Fix ResolverResult.kt<commit_after>/*
 * MARID, the visual component programming environment.
 * Copyright (C) 2020 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.marid.moan

import java.lang.ref.Cleaner
import java.math.BigInteger
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicReference
import java.util.logging.Level.INFO
import kotlin.reflect.*
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.isSubtypeOf

typealias Closer = (Runnable) -> Runnable

class Context private constructor(
  val name: String,
  val parent: Context?,
  closer: Closer,
  val properties: Properties
): MoanFetcher, AutoCloseable {

  private val queue = ConcurrentLinkedDeque<MoanHolder<*>>()
  private val typedMap = ConcurrentHashMap<KClassifier, ConcurrentLinkedQueue<MoanHolder<*>>>()
  private val namedMap = ConcurrentHashMap<String, MoanHolder<*>>()
  private val closeListeners = ConcurrentLinkedDeque<() -> Unit>()
  private val uid = UIDS.getAndUpdate(BigInteger::inc)

  val path: String = generateSequence(this, { it.parent }).map { it.name }.reduce { a, b -> "$b/$a" }

  init {
    val name = this.path
    val queue = this.queue
    val typedMap = this.typedMap
    val namedMap = this.namedMap
    val closeListeners = this.closeListeners
    val closeTask = { uid: BigInteger ->
      closer(Runnable {
        name.asLogger.log(INFO, "Cleaning")
        close(uid, name, queue, typedMap, namedMap, closeListeners)
      })
    }
    if (parent != null) {
      val uid = this.uid
      val listener = { closeTask(uid).run() }
      val parent = this.parent
      parent.addCloseListener(listener)
      addCloseListener { parent.removeCloseListener(listener) }
    }
    CLEANABLES.computeIfAbsent(uid) { uid -> CLEANER.register(this, closeTask(uid)) }
  }

  fun addCloseListener(listener: () -> Unit) {
    closeListeners.addFirst(listener)
  }

{'fraction_non_alphanumeric': 0.09793718639658056, 'fraction_numerical': 0.001858390633711206, 'mean_word_length': 3.9649446494464944, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 36, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4935177', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1544, 'n_words': 490}