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

org.beangle.template.freemarker.ParametersHashModel.scala Maven / Gradle / Ivy

There is a newer version: 0.0.3
Show newest version
package org.beangle.template.freemarker

import freemarker.template.TemplateHashModelEx
import freemarker.template.SimpleCollection
import freemarker.template.TemplateModel
import freemarker.template.TemplateCollectionModel
import freemarker.template.SimpleScalar
import java.{ util => ju }

class ParametersHashModel(val params: Map[String, Any]) extends TemplateHashModelEx {
  override def get(key: String): TemplateModel = {
    params.get(key) match {
      case Some(v) => {
        if (v.getClass.isArray) {
          new SimpleScalar(v.asInstanceOf[Array[_]](0).asInstanceOf[String])
        } else new SimpleScalar(v.asInstanceOf[String])
      }
      case None => null
    }
  }

  override def isEmpty: Boolean = {
    params.isEmpty
  }

  override def size: Int = {
    params.size
  }

  override def keys: TemplateCollectionModel = {
    import scala.collection.JavaConversions._
    new SimpleCollection(asJavaIterator(params.keys.iterator));
  }

  override def values: TemplateCollectionModel = {
    val iter = params.keys.iterator
    val javaIter = new ju.Iterator[Any]() {
      override def hasNext: Boolean = {
        iter.hasNext
      }
      override def next: Any = {
        params(iter.next)
      }
      override def remove {
        throw new UnsupportedOperationException();
      }
    }

    new SimpleCollection(javaIter)
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy