article banner

Exercise: Blog Post Properties

In your project, you're working with a BlogPost class that represents a blog post, including its title, content, and author details. Your task is to enhance the BlogPost class by adding and implementing the following properties:

  • authorName - a string that combines the author's name and surname. For example, if the author's name is "John" and the surname is "Smith", the value of this property should be "John Smith". You can assume that you will need to use this property many times per blog post object.
  • wordCount - an integer that represents the number of words in the blog post. You can assume that this property is expensive to calculate and you might need it more than once per blog post object
  • isLongRead - a boolean that indicates whether the blog post is longer than 1000 characters. You can assume that you will need to use this property at most once per blog post object.
  • summary - a string that is calculated using the generateSummary function. This object is very heavy to calculate, so you don’t want to calculate it more than once.

You need to decide how to implement each of these properties. Your options are:

  • A val property defined by a value
  • A val property defined by a getter
  • A lazy property

Starting code:

data class BlogPost( val title: String, val content: String, val author: Author, ) { // TODO: Add properties here private fun calculateWordCount(): Int { Thread.sleep(1) return content.split("\\s+".toRegex()).size } private fun calculateIsLongRead(): Boolean { Thread.sleep(1) return content.length > 1000 } private fun generateSummary(content: String): String { Thread.sleep(10) return content.take(100) + "..." } } data class Author( val key: String, val name: String, val surname: String, )

This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in advanced/delegates/Lazy.kt. You can find there starting code.

Once you are done with the exercise, you can check your solution here.

Playground

import kotlin.system.measureTimeMillis data class BlogPost( val title: String, val content: String, val author: Author, ) { // TODO: Add properties here private fun calculateWordCount(): Int { Thread.sleep(1) return content.split("\\s+".toRegex()).size } private fun calculateIsLongRead(): Boolean { Thread.sleep(1) return content.length > 1000 } private fun generateSummary(content: String): String { Thread.sleep(10) return content.take(100) + "..." } } data class Author( val key: String, val name: String, val surname: String, ) fun main() { val post = BlogPost( title = "Hello, World!", content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Sed non risus. Suspendisse lectus tortor, dignissim sit amet, " + "adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. " + "Maecenas ligula massa, varius a, semper congue, euismod non, mi. " + "Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, " + "non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, " + "scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. " + "Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum " + "augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. " + "Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum " + "primis in faucibus orci luctus et ultrices posuere cubilia Curae; " + "Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. " + "Maecenas adipiscing ante non diam sodales hendrerit.", author = Author( key = "alex", name = "Alex", surname = "Smith", ), ) // println(post.authorName) // // Alex Smith // println(post.wordCount) // // 153 // println(post.isLongRead) // // true // println(post.summary) // // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin portt... // Stress tests // val author = Author( // key = "alex", // name = "Alex", // surname = "Smith", // ) // val longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, " + // "dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, " + // "varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non " + // "fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. " + // "Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas " + // "leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum " + // "primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque " + // "fermentum. Maecenas adipiscing ante non diam sodales hendrerit." // // measureTimeMillis { // val post = BlogPost( // title = "Hello, World!", // content = longText, // author = author, // ) // repeat(1_000_000) { post.authorName } // }.let { println("Reading authorName 1 000 000 times from the same object took $it") } // // measureTimeMillis { // repeat(1_000) { // val post = BlogPost( // title = "Hello, World!", // content = longText, // author = author, // ) // repeat(it % 11) { post.wordCount } // } // }.let { println("Reading wordCount from 1 000 objects from 0 to 10 times took $it") } // // measureTimeMillis { // repeat(10_000) { // val post = BlogPost( // title = "Hello, World!", // content = longText, // author = author, // ) // repeat(if (it % 4 == 0) 1 else 0) { post.wordCount } // } // }.let { println("Reading isLongRead from 10 000 objects 0, 0, 0, or 1 times took $it") } // // measureTimeMillis { // repeat(100) { // val post = BlogPost( // title = "Hello, World!", // content = longText, // author = author, // ) // repeat(it % 11) { post.summary } // } // }.let { println("Reading summary from 100 objects from 0 to 10 times took $it") } }