<commit_before>/*
 * Copyright (c) 2013 <NAME>
 *
 * 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 com.ckkloverdos.thrift3r.protocol
package helper

import com.ckkloverdos.thrift3r.codec.Codec
import com.ckkloverdos.thrift3r.codec.enumeration.EnumCodec
import com.ckkloverdos.thrift3r.descriptor.{StructDescriptor, FieldInfo}
import scala.annotation.tailrec
import scala.collection.{SortedMap, GenMap, GenTraversable}


/**
 *
 * @author <NAME> <<EMAIL>>
 */
object ProtocolHelpers {
  final def encodeAndFlush[T <:AnyRef](protocol: Protocol, bean: T, codec: Codec[T]) {
    codec.encode(protocol, bean)
    protocol.flush()
  }

  final def writeList[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    elements: GenTraversable[T]
  ) {
    val listProtocol = protocol.getListProtocol

    if(elements eq null) {
      listProtocol.writeEmptyList(elementCodec)
      return
    }

    val size = elements.size
    listProtocol.writeListBegin(elementCodec, size)
    for(element ← elements) {
      listProtocol.writeListElement(element, elementCodec)
    }
    listProtocol.writeListEnd()
  }

  final def readList[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    addElement: (T) ⇒ Unit
  ) {

    def readSized(listProtocol: SizedListProtocol) {
      @tailrec def readElement(index: Int, size: Int) {
        if(index < size) {
          val element = listProtocol.readListElement(elementCodec)
          addElement(element)
          readElement(index + 1, size)
        }
      }

      val size = listProtocol.readListBegin()
      readElement(0, size)
      listProtocol.readListEnd()
    }

    def readUnsized(listProtocol: UnsizedListProtocol) {
      listProtocol.readListBegin()

      while(!listProtocol.readListEnd()) {
        val element = listProtocol.readListElement(elementCodec)
        addElement(element)
      }
    }

    protocol.getListProtocol match {
      case listProtocol: SizedListProtocol ⇒
        readSized(listProtocol)

      case listProtocol: UnsizedListProtocol ⇒
        readUnsized(listProtocol)
    }
  }

  final def writeSet[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    elements: GenTraversable[T]
    ) {
    val setProtocol = protocol.getSetProtocol

    if(elements eq null) {
      setProtocol.writeEmptySet(elementCodec)
      return
    }

    val size = elements.size
    setProtocol.writeSetBegin(elementCodec, size)
    for(element ← elements) {
      setProtocol.writeSetElement(element, elementCodec)
    }
    setProtocol.writeSetEnd()
  }

  final def readSet[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    addElement: (T) ⇒ Unit
  ) {
    def readSized(setProtocol: SizedSetProtocol) {
      @tailrec def readElement(index: Int, size: Int) {
        if(index < size) {
          val element = setProtocol.readSetElement(elementCodec)
          addElement(element)
          readElement(index + 1, size)
        }
      }

      val size = setProtocol.readSetBegin()
      readElement(0, size)
      setProtocol.readSetEnd()
    }

    def readUnsized(setProtocol: UnsizedSetProtocol) {
      setProtocol.readSetBegin()

      while(!setProtocol.readSetEnd()) {
        val element = setProtocol.readSetElement(elementCodec)
        addElement(element)
      }
    }

    protocol.getSetProtocol match {
      case setProtocol: SizedSetProtocol ⇒
        readSized(setProtocol)

      case setProtocol: UnsizedSetProtocol ⇒
        readUnsized(setProtocol)
    }
  }

  final def writeMap[K, V](
    protocol: Protocol,
    keyCodec: Codec[K],
    valueCodec: Codec[V],
    elements: GenMap[K, V]
  ) {
    val mapProtocol = protocol.getMapProtocol

    if(elements eq null) {
      mapProtocol.writeEmptyMap(keyCodec, valueCodec)
      return
    }

    val size = elements.size
    mapProtocol.writeMapBegin(keyCodec, valueCodec, size)
    for((k, v) ← elements) {
      mapProtocol.writeMapElement(k, keyCodec, v, valueCodec)
    }
    mapProtocol.writeMapEnd()
  }

  final def readMap[K, V](
    protocol: Protocol,
    keyCodec: Codec[K],
    valueCodec: Codec[V],
    addElement: ((K, V)) ⇒ Unit
  ) {
    def decodeSized(mapProtocol: SizedMapProtocol) {
      @tailrec def decodeElement(index: Int, size: Int) {
        if(index < size) {
          val kv = mapProtocol.readMapElement(keyCodec, valueCodec)
          addElement(kv)
          decodeElement(index + 1, size)
        }
      }

      val size = mapProtocol.readMapBegin()
      decodeElement(0, size)
      mapProtocol.readMapEnd()
    }<commit_msg>Avoid compiler WARNing about non-existent withFilter method<commit_after>/*
 * Copyright (c) 2013 <NAME>
 *
 * 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 com.ckkloverdos.thrift3r.protocol
package helper

import com.ckkloverdos.thrift3r.codec.Codec
import com.ckkloverdos.thrift3r.codec.enumeration.EnumCodec
import com.ckkloverdos.thrift3r.descriptor.{StructDescriptor, FieldInfo}
import scala.annotation.tailrec
import scala.collection.{SortedMap, GenMap, GenTraversable}


/**
 *
 * @author <NAME> <<EMAIL>>
 */
object ProtocolHelpers {
  final def encodeAndFlush[T <:AnyRef](protocol: Protocol, bean: T, codec: Codec[T]) {
    codec.encode(protocol, bean)
    protocol.flush()
  }

  final def writeList[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    elements: GenTraversable[T]
  ) {
    val listProtocol = protocol.getListProtocol

    if(elements eq null) {
      listProtocol.writeEmptyList(elementCodec)
      return
    }

    val size = elements.size
    listProtocol.writeListBegin(elementCodec, size)
    for(element ← elements) {
      listProtocol.writeListElement(element, elementCodec)
    }
    listProtocol.writeListEnd()
  }

  final def readList[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    addElement: (T) ⇒ Unit
  ) {

    def readSized(listProtocol: SizedListProtocol) {
      @tailrec def readElement(index: Int, size: Int) {
        if(index < size) {
          val element = listProtocol.readListElement(elementCodec)
          addElement(element)
          readElement(index + 1, size)
        }
      }

      val size = listProtocol.readListBegin()
      readElement(0, size)
      listProtocol.readListEnd()
    }

    def readUnsized(listProtocol: UnsizedListProtocol) {
      listProtocol.readListBegin()

      while(!listProtocol.readListEnd()) {
        val element = listProtocol.readListElement(elementCodec)
        addElement(element)
      }
    }

    protocol.getListProtocol match {
      case listProtocol: SizedListProtocol ⇒
        readSized(listProtocol)

      case listProtocol: UnsizedListProtocol ⇒
        readUnsized(listProtocol)
    }
  }

  final def writeSet[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    elements: GenTraversable[T]
    ) {
    val setProtocol = protocol.getSetProtocol

    if(elements eq null) {
      setProtocol.writeEmptySet(elementCodec)
      return
    }

    val size = elements.size
    setProtocol.writeSetBegin(elementCodec, size)
    for(element ← elements) {
      setProtocol.writeSetElement(element, elementCodec)
    }
    setProtocol.writeSetEnd()
  }

  final def readSet[T](
    protocol: Protocol,
    elementCodec: Codec[T],
    addElement: (T) ⇒ Unit
  ) {
    def readSized(setProtocol: SizedSetProtocol) {
      @tailrec def readElement(index: Int, size: Int) {
        if(index < size) {
          val element = setProtocol.readSetElement(elementCodec)
          addElement(element)
          readElement(index + 1, size)
        }
      }

      val size = setProtocol.readSetBegin()
      readElement(0, size)
      setProtocol.readSetEnd()
    }

    def readUnsized(setProtocol: UnsizedSetProtocol) {
      setProtocol.readSetBegin()

      while(!setProtocol.readSetEnd()) {
        val element = setProtocol.readSetElement(elementCodec)
        addElement(element)
      }
    }

    protocol.getSetProtocol match {
      case setProtocol: SizedSetProtocol ⇒
        readSized(setProtocol)

      case setProtocol: UnsizedSetProtocol ⇒
        readUnsized(setProtocol)
    }
  }

  final def writeMap[K, V](
    protocol: Protocol,
    keyCodec: Codec[K],
    valueCodec: Codec[V],
    elements: GenMap[K, V]
  ) {
    val mapProtocol = protocol.getMapProtocol

    if(elements eq null) {
      mapProtocol.writeEmptyMap(keyCodec, valueCodec)
      return
    }

    val size = elements.size
    mapProtocol.writeMapBegin(keyCodec, valueCodec, size)
    elements.foreach {
      case (k, v) ⇒
        mapProtocol.writeMapElement(k, keyCodec, v, valueCodec)
    }
    mapProtocol.writeMapEnd()
  }

  final def readMap[K, V](
    protocol: Protocol,
    keyCodec: Codec[K],
    valueCodec: Codec[V],
    addElement: ((K, V)) ⇒ Unit
  ) {
    def decodeSized(mapProtocol: SizedMapProtocol) {
      @tailrec def decodeElement(index: Int, size: Int) {
        if(index < size) {
          val kv = mapProtocol.readMapElement(keyCodec, valueCodec)
          addElement(kv)
          decodeElement(index + 1, size)
        }
      }

      val size = mapProtocol.readMapBegin()
      decodeElement(0, size)
      mapProtocol.readMapEnd()
    }
{'fraction_non_alphanumeric': 0.09590389917290272, 'fraction_numerical': 0.0035447026388341868, 'mean_word_length': 2.970680218921032, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, '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': '5904959', 'n_tokens_mistral': 3105, 'n_tokens_neox': 3094, 'n_words': 731}