Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Last active June 10, 2016 11:15
Show Gist options
  • Save JoolsF/1780546fc16fa921aa69e859c017959a to your computer and use it in GitHub Desktop.
Save JoolsF/1780546fc16fa921aa69e859c017959a to your computer and use it in GitHub Desktop.
Pretty printer with fields and delimiter param
/**
* Iterates through fields and prints each one with its name and value
*
* Delimiter parameter seperates fields. Defaults to line seperator
*/
def toStringPrettyPrint(delimiter: String = "\n"): String = {
val fields = this.getClass.getDeclaredFields.foldLeft(new TreeMap[String,Any]) { (acc,field) =>
field.setAccessible(true)
acc + (field.getName -> field.get(this))
}
s"${fields.mkString(delimiter)}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment