<commit_before>/*
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fusesource.fabric.groups.internal

import org.fusesource.fabric.groups.ChangeListener
import java.util.concurrent.TimeUnit

/**
 * <p>
 * </p>
 *
 * @author <a href="http://hiramchirino.com"><NAME></a>
 */
trait ChangeListenerSupport {

  var listeners = List[ChangeListener]()

  def connected:Boolean

  def add(listener: ChangeListener): Unit = {
    val connected = this.synchronized {
      listeners ::= listener
      this.connected
    }
    if (connected) {
      listener.connected
    }
  }

  def remove(listener: ChangeListener): Unit = this.synchronized {
    listeners = listeners.filterNot(_ == listener)
  }

  def fireConnected() = {
    val listener = this.synchronized { this.listeners }
    check_elapsed_time {
      for (listener <- listeners) {
        listener.connected
      }
    }
  }

  def fireDisconnected() = {
    val listener = this.synchronized { this.listeners }
    check_elapsed_time {
      for (listener <- listeners) {
        listener.disconnected
      }
    }
  }

  def fireChanged() = {
    val listener = this.synchronized { this.listeners }
    val start = System.nanoTime()
    check_elapsed_time {
      for (listener <- listeners) {
        listener.changed
      }
    }
  }

  def check_elapsed_time[T](func: => T):T = {
    val start = System.nanoTime()
    try {
      func
    } finally {
      val end = System.nanoTime()
      val elapsed = TimeUnit.NANOSECONDS.toMillis(end-start)
      if( elapsed > 100 ) {
        println("WARN: listerns are taking too long to process the events")
      }
    }
  }
  
}<commit_msg>Updated: /fabric/fabric-groups/src/main/scala/org/fusesource/fabric/groups/internal/ChangeListenerSupport.scala Line 84: Fixed typo in warning message: changed "listerns" to "listeners"<commit_after>/*
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fusesource.fabric.groups.internal

import org.fusesource.fabric.groups.ChangeListener
import java.util.concurrent.TimeUnit

/**
 * <p>
 * </p>
 *
 * @author <a href="http://hiramchirino.com"><NAME></a>
 */
trait ChangeListenerSupport {

  var listeners = List[ChangeListener]()

  def connected:Boolean

  def add(listener: ChangeListener): Unit = {
    val connected = this.synchronized {
      listeners ::= listener
      this.connected
    }
    if (connected) {
      listener.connected
    }
  }

  def remove(listener: ChangeListener): Unit = this.synchronized {
    listeners = listeners.filterNot(_ == listener)
  }

  def fireConnected() = {
    val listener = this.synchronized { this.listeners }
    check_elapsed_time {
      for (listener <- listeners) {
        listener.connected
      }
    }
  }

  def fireDisconnected() = {
    val listener = this.synchronized { this.listeners }
    check_elapsed_time {
      for (listener <- listeners) {
        listener.disconnected
      }
    }
  }

  def fireChanged() = {
    val listener = this.synchronized { this.listeners }
    val start = System.nanoTime()
    check_elapsed_time {
      for (listener <- listeners) {
        listener.changed
      }
    }
  }

  def check_elapsed_time[T](func: => T):T = {
    val start = System.nanoTime()
    try {
      func
    } finally {
      val end = System.nanoTime()
      val elapsed = TimeUnit.NANOSECONDS.toMillis(end-start)
      if( elapsed > 100 ) {
        println("WARN: listeners are taking too long to process the events")
      }
    }
  }
  
}
{'fraction_non_alphanumeric': 0.10580868063053336, 'fraction_numerical': 0.0034549773267112936, 'mean_word_length': 2.853577371048253, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '5112329', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1355, 'n_words': 386}