<commit_before>package com.ataraxer.zooowner

import org.apache.zookeeper.data.Stat
import org.apache.zookeeper.KeeperException._
import org.apache.zookeeper.AsyncCallback
import org.apache.zookeeper.AsyncCallback._

import com.ataraxer.zooowner.Zooowner.{Reaction, default}
import com.ataraxer.zooowner.message._
import com.ataraxer.zooowner.common.ZKNodeMeta.StatConverter

import java.util.{List => JavaList}

import scala.collection.JavaConversions._


object Callback {
  def serialize(data: ZKData) =
    Option(data) map { new String(_) }
}


/**
 * Callback class represents abstract callback for asynchronous ZooKeeper
 * operation.
 */
sealed abstract class Callback
  (reaction: Reaction[Response])
    extends AsyncCallback
{
  protected val reactOn = reaction orElse default[Response]

  protected def processCode(code: Int, path: String)
                           (process: PartialFunction[Code, Response]) =
  {
    val codeNumber = code
    Code.get(codeNumber) match {
      case code if process.isDefinedAt(code) => reactOn {
        process(code)
      }

      // default context-free codes reactions
      case Code.NOTREADONLY => reactOn(ReadOnly)
      case Code.BADVERSION => reactOn(BadVersion)
      case Code.CONNECTIONLOSS => reactOn(Disconnected)
      case Code.SESSIONEXPIRED => reactOn(Expired)

      case Code.NONODE => reactOn(NoNode(path))
      case Code.NODEEXISTS => reactOn(NodeExists(path))
      case Code.NOTEMPTY => reactOn(NotEmpty(path))
      case Code.NOCHILDRENFOREPHEMERALS => reactOn(NodeIsEphemeral(path))

      case unexpectedCode => reactOn {
        Error(unexpectedCode)
      }
    }
  }<commit_msg>Reduce boilerplate in asynchronous callbacks<commit_after>package com.ataraxer.zooowner

import org.apache.zookeeper.data.Stat
import org.apache.zookeeper.KeeperException._
import org.apache.zookeeper.AsyncCallback
import org.apache.zookeeper.AsyncCallback._

import com.ataraxer.zooowner.Zooowner.{Reaction, default}
import com.ataraxer.zooowner.message._
import com.ataraxer.zooowner.common.ZKNodeMeta.StatConverter

import java.util.{List => JavaList}

import scala.collection.JavaConversions._


object Callback {
  def serialize(data: ZKData) =
    Option(data) map { new String(_) }
}


/**
 * Callback class represents abstract callback for asynchronous ZooKeeper
 * operation.
 */
sealed abstract class Callback
  (reaction: Reaction[Response])
    extends AsyncCallback
{
  import Code._

  protected val reactOn = reaction orElse default[Response]


  protected def processCode(code: Int, path: String)
                           (process: PartialFunction[Code, Response]) =
  {
    reactOn {
      Code.get(code) match {
        case codeEnum if process.isDefinedAt(codeEnum) => process(codeEnum)

        case NONODE => NoNode(path)
        case NODEEXISTS => NodeExists(path)
        case NOTEMPTY => NotEmpty(path)
        case NOCHILDRENFOREPHEMERALS => NodeIsEphemeral(path)

        // default context-free reactions
        case NOTREADONLY => ReadOnly
        case BADVERSION => BadVersion
        case CONNECTIONLOSS => Disconnected
        case SESSIONEXPIRED => Expired

        case unexpectedCode => Error(unexpectedCode)
      }
    }
  }
{'fraction_non_alphanumeric': 0.09127600124185035, 'fraction_numerical': 0.0, 'mean_word_length': 3.5252808988764044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4929141', 'n_tokens_mistral': 997, 'n_tokens_neox': 954, 'n_words': 213}