<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sunaina Goyal on Medium]]></title>
        <description><![CDATA[Stories by Sunaina Goyal on Medium]]></description>
        <link>https://medium.com/@gosunaina?source=rss-44f38eb9082e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*b9UkSZlXLNL7wJJ3uVNTTQ.jpeg</url>
            <title>Stories by Sunaina Goyal on Medium</title>
            <link>https://medium.com/@gosunaina?source=rss-44f38eb9082e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:40:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gosunaina/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hexagonal Architecture in Java]]></title>
            <link>https://gosunaina.medium.com/hexagonal-architecture-in-java-e298045a9567?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e298045a9567</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[hexagonal-architecture]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Mon, 29 Nov 2021 08:14:11 GMT</pubDate>
            <atom:updated>2021-11-29T08:14:11.692Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Overview</h3><p>In this tutorial, we will implement a Spring application to exhibit a practical yet simple example of the usage of the <strong>Hexagonal Architecture</strong> pattern. It is an illustration of the approach and the implementation of the pattern principles in Java.</p><h3>2. Hexagonal Architecture</h3><p>The hexagonal architecture provides a clean separation between the business domain (or the inside), and the technical code (or the outside) of the application.</p><p>The Application layer depends on the Domain layer to fulfill a request. The outside layers are dependent on the inside layers, but they can be ignorant of what the inner layers are doing — they just need to know the methods to call and data to pass. Implementation details are safely encapsulated.</p><p>Now, the Domain layer will likely need database access to create some domain entities. This means our application depends on data storage.</p><p>We can see here conceptually, our inner layers are depending on the outside. But ideally, the domain should be independent of the outside. So, we <strong>invert the control by using interfaces</strong>. These allow our layers to dictate how they are used. This is an <strong>Inversion of Control</strong>.</p><p>Let’s understand this by dividing our <em>product-service</em> application into three layers — <strong>application</strong> (outside), <strong>domain</strong> (inside), and <strong>infrastructure</strong> (outside) :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DsNVB5R6Iro3NZSH.png" /></figure><h3>2. Domain</h3><p>Let’s start with the domain,<strong> to build value-based software</strong>. Firstly, create a simple <em>ProductDto </em>class :</p><pre>public class ProductDto {<br>	private Long id;<br>	private String type;<br>	private String description;</pre><pre>	// Getters and setters<br>}</pre><h3>3. Ports</h3><h3>3.1. Inbound Port</h3><p>Now let’s define an interface <em>ProductServicePort. </em>It exposes the core application to the application layer :</p><pre>public interface ProductServicePort {<br>    List&lt;ProductDto&gt; getProducts();<br>    ProductDto addProduct(ProductDto product);<br>    //..<br>}</pre><h3>3.2. Outbound Port</h3><p>Create another interface <em>ProductPersistencePort. </em>The implementation of the interface will be in the infrastructure layer :</p><pre>public interface ProductPersistencePort {<br>    List&lt;ProductDto&gt; getAllProducts();<br>    ProductDto addProduct(ProductDto product);<br>    //...<br>}</pre><h3>4. Adapters</h3><p>Adapters are the outside part of the application. They interact with the domain by using only the inbound and outbound ports.</p><h3>4.1. Primary Adapters</h3><p>They drive the application using the inbound port. Examples of primary adapters could be REST APIs or an interface contract with your clients. Let’s define a <em>ProductController </em>that provides endpoints for creating and fetching the resources :</p><pre>@RestController<br>@RequestMapping(&quot;/products&quot;)<br>public class ProductController {</pre><pre>    @Autowired<br>    private ProductServicePort productService;</pre><pre>    @GetMapping<br>    public ResponseEntity&lt;List&lt;ProductDto&gt;&gt; getProducts() {<br>        return new ResponseEntity&lt;List&lt;ProductDto&gt;&gt;(productService.getProducts(), HttpStatus.OK);<br>    }<br>    <br>    @PostMapping<br>    public ResponseEntity&lt;ProductDto&gt; addProduct(@RequestBody ProductDto product) {<br>    	return new ResponseEntity&lt;ProductDto&gt;(productService.addProduct(product), HttpStatus.CREATED);<br>    }<br> <br>    //..<br>}</pre><h3>4.2. Secondary Adapters</h3><p>Secondary adapters are an implementation of the outbound port. They offer the flexibility of changes in the data access layer (framework or database changes), without affecting the domain.</p><p>Create a <strong>Spring Data Jpa </strong>Repository <strong><em>ProductRepository </em></strong>in the<strong> infrastructure.</strong> This JPA repository <strong>saves the database entity Product</strong> to the database :</p><p>@Repository</p><p>public interface ProductRepository extends JpaRepository&lt;Product, Long&gt; {<br> }</p><p>Now we have to transform the request object <em>ProductDto </em>to entity class <em>Product</em> and then save it to the database<em>. </em>For this, create a <em>ProductJpaAdapter</em> class, using the inversion of control :</p><pre>@Service<br>public class ProductJpaAdapter implements ProductPersistencePort {<br>	<br>    @Autowired<br>    private ProductRepository productRepository;</pre><pre>    @Override<br>    public List&lt;ProductDto&gt; getAllProducts() {<br>        List&lt;Product&gt; products =  productRepository.findAll();<br>        return ProductMapper.INSTANCE.fromProductList(products);<br>    }<br>    <br>    @Override<br>    public ProductDto addProduct(ProductDto productDto) {<br>    	Product product = ProductMapper.INSTANCE.toProduct(productDto);<br>        Product productSaved = productRepository.save(product);<br>        return ProductMapper.INSTANCE.fromProduct(productSaved);<br>    }<br>    //..<br>}</pre><h3>5. Use case of the core application</h3><p>They are specific use case implementations of the inbound port. Let’s define a <em>ProductServiceImpl</em> class in the domain :</p><pre>class ProductServiceImpl implements ProductServicePort {<br>    private final ProductPersistencePort productRepository;<br>    ProductServiceImpl(ProductPersistencePort productRepository) {<br>        this.productRepository = productRepository;<br>    }</pre><pre>    @Override<br>    public List&lt;ProductDto&gt; getProducts() {<br>        return productRepository.getAllProducts();<br>    }<br>    //...<br>}</pre><h3>6. Conclusion</h3><p>In this article, we’ve learned to layer our classes/objects in such a way that the core logic is isolated from the external elements.</p><p>In our application above, we see that the REST endpoints internally use the inbound port to communicate with the domain layer. The domain uses the outbound port to communicate with the downstream system. It enables us to replace one adapter with another without any impact on the domain. For example, if you want to implement a new Jpa Provider, you just need to define a new repository with @Repository annotation, while removing the current ProductRepository. This will require no change in the ProductServiceImpl. The <strong>domain depends on nothing but itself:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*H6jlVeoOXdTeKZ13.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e298045a9567" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cache — Redis, EhCache or Caffeine?]]></title>
            <link>https://gosunaina.medium.com/cache-redis-ehcache-or-caffeine-45b383ae85ee?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/45b383ae85ee</guid>
            <category><![CDATA[caffeine]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[cache-memory]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[ehcache]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Wed, 23 Jun 2021 16:14:11 GMT</pubDate>
            <atom:updated>2021-06-23T16:28:36.543Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cache — Redis, EhCache or Caffeine?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/0*LmFnEFVoQql3guzA.png" /></figure><p>A cache is a reserved storage location that collects temporary data to help websites, browsers, and apps load faster. The data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere.</p><p>A <em>cache hit</em> occurs when the requested data can be found in a cache, while a <em>cache miss</em> occurs when it cannot.</p><h3>Types of Cache</h3><p><strong>Redis</strong> is an in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.</p><p><strong>EhCache</strong> is a pure Java in-process cache framework. It is fast and compact. It is the default cache provided by Hibernate and MyBatis.(Note: Although EhCache3 supports distributed caching, it is also based on Java process caching)</p><p><strong>Caffeine</strong> is a rewritten version of the Guava cache using Java8 that replaced Guava in Spring 5 and supports a variety of cache expiration policies. It is a near-optimal cache library known as Best Performance.</p><h3><strong>Redis vs EhCache:</strong></h3><h4>1. Multilingual projects</h4><p>You can think of Redis as a <strong>shared data structure</strong>, while Ehcache is a <em>memory block</em> storing serialized data objects. This is the main difference.</p><p>Redis as a <em>shared data structure</em> means you can put some <strong>predefined</strong> data structure (such as <em>String</em>, <em>List</em>, <em>Set,</em> <a href="https://redis.io/topics/data-types">etc</a>) in one language and retrieve it in another language. This is useful if your project is <strong>multilingual</strong>, for example, Java on the backend side, and PHP on the front side. You can use Redis for a shared cache. But it can only store a predefined data structure, you cannot insert any Java objects you want.</p><p>If your project is only Java, i.e. not <strong>multilingual</strong>, Ehcache is a convenient solution.</p><h4>2. Scalability</h4><p>You will meet issues with EhCache scaling and need resources to manage it during failover and etc. <strong>Redis benefits</strong> over EhCache:</p><ol><li>It uses a time-proven gossip protocol for Node discovery and synchronization.</li><li>Availability of fully managed services like <a href="https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/WhatIs.html">AWS ElastiCache</a>, <a href="https://azure.microsoft.com/en-us/services/cache/">Azure Redis Cache</a>. Such services offer full automation, support, and management of Redis, so developers can focus on their applications and not maintaining their databases.</li><li>Correct large memory amount handling (we all know that Redis can manage with hundreds of gigabytes of RAM on a single machine). It doesn’t have problems with Garbage collection like Java.</li><li>The existence of Java Developer friendly Redis client — <a href="https://redisson.org/">Redisson</a>, provides many Java friendly objects <strong>on top of Redis.</strong></li></ol><h3>Caffeine vs Ehcache</h3><ol><li>From the perspective of supported features, Ehcache <strong>supports more features</strong>, such as multi-level cache, distributed cache, cache listener, and so on.</li><li>If you just want to simply use a memory cache component and have <strong>high-performance</strong> requirements, you can give priority to Caffeine, which is better than Ehcache, and it uses a better cache elimination strategy. Caffeine uses the <em>Window TinyLfu</em> eviction policy, which provides a <strong>near-optimal hit rate</strong>.</li></ol><pre>Caffeine is higher than ehcache mostly in terms of <em>writing</em> performance.</pre><p>3. Caffeine has <strong>three strategies for value eviction</strong> — size-based, time-based, and reference-based.</p><p>Ehcache also provides <strong>three eviction algorithms</strong> to choose from for the MemoryStore — Least Recently Used (LRU), Least Frequently Used (LFU), and First In First Out (FIFO).</p><p>4. Ehcache provides <strong>support for memory and disk storage</strong>. When the MemoryStore gets full, elements are evicted. What happens on eviction depends on the cache configuration.</p><p>To implement a similar ‘post-eviction strategy for caffeine’, you can create a victim cache to capture recently expired entries from the cache and resurrect from it on a load (say if the DB is down). The victim cache would need its own bounding, e.g. a longer expiration threshold.</p><pre>Cache&lt;K, V&gt; victimCache = Caffeine.newBuilder()<br>    .expireAfterWrite(10, TimeUnit.MINUTE)<br>    .build();<br>LoadingCache&lt;K, V&gt; mainCache = Caffeine.newBuilder()<br>    .expireAfterWrite(1, TimeUnit.MINUTE)<br>    .writer(new CacheWriter&lt;K, V&gt;() {<br>      public void write(K key, V value) { /* ignored */ }<br>      public void delete(K key, V value, RemovalCause cause) {<br>        if (cause == RemovalCause.EXPIRED) {<br>          victimCache.put(key, value);<br>        }<br>      })<br>     .build(key -&gt; {<br>       try {<br>         // load from db<br>       } catch (DatabaseAccessException e) {<br>         return victimCache.asMap().remove(key);<br>       }<br>     });</pre><p>5. Ehcache also <strong>supports multiple cache manager instances</strong>, as well as multiple cache regions for one instance.</p><p>6. Caffeine provides <strong>three strategies for cache population</strong> — manual, synchronous, and asynchronous loading. On the other hand, Ehcache does not seem to provide asynchronous loading.</p><p>7. The API provided by Caffeine is <strong>easier to use</strong>, while the configuration of Ehcache is slightly more cumbersome.</p><h3>Redis vs Caffeine:</h3><ol><li>Redis is distributed cache, while (like Ehcache) caffeine is in-process or local cache.</li><li>Compared with Redis, caffeine has no network IO consumption.</li></ol><p>To optimize the application, caffeine can be used as a first-level cache and Redis as the second-level cache.</p><h3>Conclusion</h3><p>There are various caching methods, such as Redis, Caffeine, JCache, EhCache, and so on. But if only one kind of cache is used, it will either consume more network (such as Redis) or occupy too much memory (such as Caffeine, an application memory cache). In many scenarios, more than one cache can be combined to achieve the first and second-level caching, which can greatly improve the processing efficiency of the application.</p><p>There are many other types of cache available for your application, and I will be writing more about them as well in my next posts. If my post was helpful, please provide a ‘clap’, or else, please leave a comment to support or help me improve.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45b383ae85ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Change Data Capture (CDC) With Embedded Debezium and Spring Boot]]></title>
            <link>https://gosunaina.medium.com/change-data-capture-cdc-with-embedded-debezium-and-spring-boot-b2db0362ae2e?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2db0362ae2e</guid>
            <category><![CDATA[debezium]]></category>
            <category><![CDATA[change-data-capture]]></category>
            <category><![CDATA[cdc]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Wed, 31 Mar 2021 13:55:12 GMT</pubDate>
            <atom:updated>2021-03-31T13:55:12.678Z</atom:updated>
            <content:encoded><![CDATA[<p>While working with data or replicating data sources, you probably have heard the term <strong>Change Data Capture (CDC)</strong>. As the name suggests, “CDC” is a design pattern that continuously identifies and captures incremental changes to data. This pattern is used for real-time data replication across live databases to analytical data sources or read replicas. It can also be used to trigger events based on data changes, such as the <a href="https://dzone.com/articles/implementing-the-outbox-pattern">OutBox pattern</a>.</p><p>Most modern databases support CDC through <strong>transaction logs</strong>. A transaction log is a sequential record of all changes made to the database while the actual data is contained in a separate file.</p><h3>What Is Debezium?</h3><p>Debezium is a distributed platform built for CDC. It uses database transaction logs and creates event streams on row-level changes. Applications listening to these events can perform needed actions based on incremental data changes.</p><p>Debezium provides a library of <a href="https://debezium.io/documentation/reference/0.10/connectors/index.html">connectors</a>, supporting a variety of databases available today. These connectors can monitor and record row-level changes in the schemas of a database. They then publish the changes on to a streaming service like <a href="https://kafka.apache.org/">Kafka</a>.</p><p>Normally, one or more connectors are deployed into a <a href="https://docs.confluent.io/current/connect/index.html">Kafka Connect</a><strong> </strong>cluster and are configured to monitor databases and publish data-change events to Kafka. A distributed Kafka Connect cluster provides the fault tolerance and scalability needed, ensuring that all the configured connectors are always running.</p><h3>What Is Embedded Debezium?</h3><p>Applications that don’t need the level of fault tolerance and reliability Kafka Connect offers or want to minimize the cost of using them to run the entire platform, can run Debezium connectors within the application. This is done by embedding the Debezium engine and configuring the connector to run within the application. On data change events, the connectors send them directly to the application.</p><h3>Running Debezium With Spring Boot</h3><p>Keeping the example simple, let’s have a Spring Boot application, “Student CDC Relay,” running embedded Debezium and tailing the transaction logs of a Postgres database that houses the “Student” table. The Debezium connector configured within the Spring Boot application invokes a method within the application when a database operation like Insert/Update/Delete are made on the “Student” table. The method acts on these events and syncs the data within the Student index on ElasticSearch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YTivRimgua6EElCQ" /></figure><h3>Installing Tools</h3><p>All the needed tools can be installed running the <strong>docker-compose</strong> file below. This starts the Postgres database on port<em> 5432</em> and Elastic Search on port <em>9200(HTTP) </em>and<em> 9300(Transport)</em>.</p><p>version: “3.5”</p><p>services:<br> # Install postgres and setup the student database.<br> postgres:<br> container_name: postgres<br> image: debezium/postgres<br> ports:<br> — 5432:5432<br> environment:<br> — POSTGRES_DB=studentdb<br> — POSTGRES_USER=user<br> — POSTGRES_PASSWORD=password</p><p># Install Elasticsearch.<br> elasticsearch:<br> container_name: elasticsearch<br> image: docker.elastic.co/elasticsearch/elasticsearch:6.8.0<br> environment:<br> — discovery.type=single-node<br> ports:<br> — 9200:9200<br> — 9300:9300</p><p>We use the image debezium/postgres because it comes prebuilt with the <a href="https://www.postgresql.org/docs/9.6/logicaldecoding-explanation.html">logical decoding</a> feature. This is a mechanism that allows the extraction of changes that were committed to the transaction log, making CDC possible.</p><p>We use the image debezium/postgres because it comes prebuilt with the <a href="https://www.postgresql.org/docs/9.6/logicaldecoding-explanation.html">logical decoding</a> feature. This is a mechanism that allows the extraction of changes that were committed to the transaction log, making CDC possible. The documentation for installing the plugin to Postgres can be found <a href="https://debezium.io/documentation/reference/0.9/connectors/postgresql.html#output-plugin">here</a>.</p><h3>Postgres Logical Decoding Concepts</h3><h3>47.2.1. Logical Decoding</h3><p>Logical decoding is the process of extracting all persistent changes to a database’s tables into a coherent, easy to understand format which can be interpreted without detailed knowledge of the database’s internal state.</p><p>In PostgreSQL, logical decoding is implemented by decoding the contents of the <a href="https://www.postgresql.org/docs/9.6/wal.html">write-ahead log</a>, which describe changes on a storage level, into an application-specific form such as a stream of tuples or SQL statements.</p><h3>47.2.2. Replication Slots</h3><p>In the context of logical replication, a slot represents a stream of changes that can be replayed to a client in the order they were made on the origin server. Each slot streams a sequence of changes from a single database.</p><h3>47.2.3. Output Plugins</h3><p>Output plugins transform the data from the write-ahead log’s internal representation into the format the consumer of a replication slot desires.</p><h3>47.2.4. Exported Snapshots</h3><h3>Understanding the Code</h3><p><a href="https://github.com/sohangp/embedded-debezium">https://github.com/sohangp/embedded-debezium</a></p><p>The first step is to define the Maven dependencies in the pom.xml for debezium-embedded and debezium-connector. The sample reads the changes from Postgres, so we use the Postgres connector.</p><p>&lt;dependency&gt;<br> &lt;groupId&gt;io.debezium&lt;/groupId&gt;<br> &lt;artifactId&gt;debezium-embedded&lt;/artifactId&gt;<br> &lt;version&gt;${debezium.version}&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br> &lt;groupId&gt;io.debezium&lt;/groupId&gt;<br> &lt;artifactId&gt;debezium-connector-postgres&lt;/artifactId&gt;<br> &lt;version&gt;${debezium.version}&lt;/version&gt;<br>&lt;/dependency&gt;</p><p>Then, we configure the connector, which listens to changes on the Student table. We use the class PostgresConnector for the connector.class setting, which is provided by Debezium. This is the name of the Java class for the connector, which tails the source database.</p><p>More details on <a href="https://dzone.com/articles/change-data-capture-cdc-with-embedded-debezium-and">https://dzone.com/articles/change-data-capture-cdc-with-embedded-debezium-and</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2db0362ae2e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java — CyclicBarrier vs CountDownLatch]]></title>
            <link>https://gosunaina.medium.com/java-cyclicbarrier-vs-countdownlatch-360dc19415a?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/360dc19415a</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[countdownlatch]]></category>
            <category><![CDATA[cyclic-barrier]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Wed, 24 Mar 2021 06:01:11 GMT</pubDate>
            <atom:updated>2021-03-24T06:01:11.852Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java — CyclicBarrier vs CountDownLatch</h3><p>When it comes to concurrency, both <strong><em>CountDownLatch</em> and <em>CyclicBarrier</em> are used for managing multi-threaded applications</strong>.</p><p>And, <strong>they are both intended to express how a given thread or group of threads should wait.</strong></p><h3>CountDownLatch</h3><p>A <em>CountDownLatch</em> is a construct that a thread <em>wait</em>s on while other threads <em>count down</em> on the latch until it reaches zero.</p><p>We can think of this like a dish at a restaurant that is being prepared. No matter which cook prepares however many of the <em>n </em>items, the waiter must <em>wait</em> until all the items are on the plate. If a plate takes <em>n </em>items, any cook will <em>count down </em>on the latch for each item she puts on the plate.</p><h3>CyclicBarrier</h3><p>A <em>CyclicBarrier </em>is a reusable construct where a group of threads <em>waits</em> together until all of the threads <em>arrive</em>. At that point, the barrier is broken and an <em>action</em> can optionally be taken.</p><p>We can think of this like a group of friends. Every time they plan to eat at a restaurant they decide a common point where they can meet. They <em>wait </em>for each other there, and only when everyone <em>arrives</em> can they go to the restaurant to eat together.</p><h3>STATING THE DIFFERENCES</h3><h3>1. Tasks vs. Threads</h3><p>Let’s take a deeper dive into some of the semantic differences between these two classes.</p><p>As stated in the definitions, <em>CyclicBarrier</em> allows a number of threads to wait on each other, whereas <em>CountDownLatch</em> allows one or more threads to wait for a number of tasks to complete.</p><p>In short, <strong><em>CyclicBarrier</em> maintains a count of <em>threads</em></strong> whereas <strong><em>CountDownLatch</em> maintains a count of <em>tasks</em></strong>.</p><p>In the following code, we define a <em>CountDownLatch </em>with a count of two. Next, we call <em>countDown()</em> twice from a single thread:</p><pre>CountDownLatch countDownLatch = new CountDownLatch(2);<br>Thread t = new Thread(() -&gt; {<br>    countDownLatch.countDown();<br>    countDownLatch.countDown();<br>});<br>t.start();<br>countDownLatch.await();</pre><pre>assertEquals(0, countDownLatch.getCount());</pre><p>Once the latch reaches zero, the call to <em>await </em>returns.</p><p>Note that in this case, <strong>we were able to have the same thread decrease the count twice.</strong></p><p><strong><em>CyclicBarrier, </em>though, is different on this point.</strong></p><p>Similar to the above example, we create a <em>CyclicBarrier, </em>again with a count of two and call <em>await()</em> on it, this time from the same thread:</p><pre>CyclicBarrier cyclicBarrier = new CyclicBarrier(2);<br>Thread t = new Thread(() -&gt; {<br>    try {<br>        cyclicBarrier.await();<br>        cyclicBarrier.await();    <br>    } catch (InterruptedException | BrokenBarrierException e) {<br>        // error handling<br>    }<br>});<br>t.start();</pre><pre>assertEquals(1, cyclicBarrier.getNumberWaiting());<br>assertFalse(cyclicBarrier.isBroken());</pre><p>The first difference here is that the threads that are waiting are themselves the barrier.</p><p>Second, and more importantly, <strong>the second <em>await()</em> is useless</strong>.<strong> A single thread can’t <em>count down </em>a barrier twice.</strong></p><p>Indeed, because <em>t</em> must <em>wait</em> for another thread to call <em>await()</em> — to bring the count to two — <em>t</em>‘s second call to <em>await() </em>won’t actually be invoked until the barrier is already broken!</p><p>In our test, <strong>the barrier hasn’t been crossed because we only have one thread waiting and not the two threads that would be required for the barrier to be tripped.</strong> This is also evident from the <em>cyclicBarrier.isBroken()</em> method, which returns <em>false</em>.</p><h3>2. Reusability</h3><p>The second most evident difference between these two classes is reusability. To elaborate, <strong>when the barrier trips in <em>CyclicBarrier</em>, the count resets to its original value.</strong> <strong><em>CountDownLatch </em>is different because the count never resets.</strong></p><p>In the given code, we define a <em>CountDownLatch</em> with count 7 and count it through 20 different calls:</p><pre>CountDownLatch countDownLatch = new CountDownLatch(7);<br>ExecutorService es = Executors.newFixedThreadPool(20);<br>for (int i = 0; i &lt; 20; i++) {<br>    es.execute(() -&gt; {<br>        long prevValue = countDownLatch.getCount();<br>        countDownLatch.countDown();<br>        if (countDownLatch.getCount() != prevValue) {<br>            outputScraper.add(&quot;Count Updated&quot;);<br>        }<br>    }); <br>} <br>es.shutdown();</pre><pre>assertTrue(outputScraper.size() &lt;= 7);</pre><p>We observe that even though 20 different threads call <em>countDown()</em>, the count doesn’t reset once it reaches zero.</p><p>Similar to the above example, we define a <em>CyclicBarrier </em>with count 7 and wait on it from 20 different threads:</p><pre>CyclicBarrier cyclicBarrier = new CyclicBarrier(7);</pre><pre>ExecutorService es = Executors.newFixedThreadPool(20);<br>for (int i = 0; i &lt; 20; i++) {<br>    es.execute(() -&gt; {<br>        try {<br>            if (cyclicBarrier.getNumberWaiting() &lt;= 0) {<br>                outputScraper.add(&quot;Count Updated&quot;);<br>            }<br>            cyclicBarrier.await();<br>        } catch (InterruptedException | BrokenBarrierException e) {<br>            // error handling<br>        }<br>    });<br>}<br>es.shutdown();</pre><pre>assertTrue(outputScraper.size() &gt; 7);</pre><p>In this case, we observe that the value decreases every time a new thread runs, by resetting to the original value, once it reaches zero.</p><p>All in all, <em>CyclicBarrier</em> and <em>CountDownLatch</em><strong><em> </em></strong>are both helpful tools for synchronization between multiple threads. However, they are fundamentally different in terms of the functionality they provide. Consider each carefully when determining which is right for the job.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=360dc19415a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[March-leetcoding-challenge-2021 : Encode and Decode TinyURL —  step by step]]></title>
            <link>https://gosunaina.medium.com/march-leetcoding-challenge-2021-encode-and-decode-tinyurl-step-by-step-6b39beb0671c?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b39beb0671c</guid>
            <category><![CDATA[tinyurl-system-design]]></category>
            <category><![CDATA[design-tinyurl]]></category>
            <category><![CDATA[tinyurl]]></category>
            <category><![CDATA[encodeanddecodeurl]]></category>
            <category><![CDATA[url-shortner]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Mon, 22 Mar 2021 09:05:30 GMT</pubDate>
            <atom:updated>2021-03-22T09:05:30.660Z</atom:updated>
            <content:encoded><![CDATA[<h3>March-leetcoding-challenge-2021 : <strong>Encode and Decode TinyURL —</strong> step by step</h3><h3>Problem statement</h3><p>TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as <a href="http://tinyurl.com/4e9iAk.">http://tinyurl.com/4e9iAk.</a></p><p>Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.</p><h3>Solution</h3><h4>1. Write a simple solution using hashcode for longUrl stored in a Map to encode and decode from longUrl to shortUrl and vice-versa.</h4><pre>public class <strong>Codec</strong> {<br> <br> Map&lt;String, String&gt; <strong>longToShort</strong> = new HashMap&lt;&gt;();<br> Map&lt;String, String&gt; <strong>shortToLong</strong> = new HashMap&lt;&gt;();<br> <br> String tinyUrlBase = “<a href="http://tinyurl.com/">http://tinyurl.com/</a>&quot;;</pre><pre>// Encodes a URL to a shortened URL.<br> public String <strong>encode</strong>(String longUrl) {</pre><pre>    if(longToShort.containsKey(longUrl))<br>      return <strong>longToShort</strong>.<strong>get(longUrl);</strong></pre><pre>     String index = longUrl.hashCode();<br>     String shortUrl = tinyUrlBase + index;<br>     longToShort.put(longUrl, shortUrl);<br>     shortToLong.put(shortUrl, longUrl);<br> <br>     return <strong>shortUrl</strong>;<br> }</pre><pre>// Decodes a shortened URL to its original URL.<br> public String <strong>decode</strong>(String shortUrl) {<br>     return <strong>shortToLong.get(shortUrl);</strong><br> }</pre><pre>}</pre><p><em>739 / 739</em> test cases passed.</p><p>Status: <em>Accepted</em></p><p>Runtime:<strong><em> </em></strong><em>7 ms</em></p><p>Memory Usage: <em>39 MB</em></p><h4>2. Replace hashcode for longUrl with a counter to store longUrl in a Map.</h4><p>Hashcode can result in collisions, even if two longUrl are not ‘equal’. So, let’s replace it with maintaining all longUrls in map and assign a counter to these entries.</p><pre>public class <strong>Codec</strong> {<br> <br> Long <strong>counter</strong> = 1L;<br> Map&lt;Long, String&gt; <strong>indexToUrl</strong> = new HashMap&lt;&gt;(); //id corresponding to long url<br> Map&lt;String, Long&gt; <strong>urlToIndex</strong> = new HashMap&lt;&gt;(); //long url corresponding to id<br> String tinyUrlBase = “<a href="http://tinyurl.com/">http://tinyurl.com/</a>”;</pre><pre>// Encodes a URL to a shortened URL.<br> public String <strong>encode</strong>(String longUrl) {<br>    if(!<strong>urlToIndex</strong>.containsKey(longUrl)){<br>        indexToUrl.put(<strong>counter</strong>, longUrl);<br>        urlToIndex.put(longUrl, <strong>counter</strong>);<br>        <strong>counter</strong>++;    <br>    }<br>    return tinyUrlBase + <strong>urlToIndex.get(longUrl)</strong>;<br> }</pre><pre>// Decodes a shortened URL to its original URL.<br> public String <strong>decode</strong>(String shortUrl) { <br>    String count = shortUrl.replace(tinyUrlBase,””);<br>    return <strong>indexToUrl.get(Long.parseLong(count));</strong><br> }<br>}</pre><p><em>739 / 739</em> test cases passed.</p><p>Status: <em>Accepted</em></p><p>Runtime: <em>5 ms</em></p><p>Memory Usage: <em>39.2 MB</em></p><h4>3. Make it scalable by using Base62 encoding.</h4><p>Please refer to my article at <a href="https://bit.ly/3rakXsw">https://bit.ly/3rakXsw</a> to understand why to choose Base64 encoding in detail.</p><pre>public class <strong>Codec</strong> {<br> <br>...</pre><pre>String <strong>base62</strong> = “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;<br>...</pre><pre> public String <strong>encode</strong>(String longUrl) {<br>   if(!urlToIndex.containsKey(longUrl)){<br>     indexToUrl.put(counter, longUrl);<br>     urlToIndex.put(longUrl, counter);<br>     counter++; <br>   }<br>   return tinyUrlBase + <strong>base62Encode</strong>(urlToIndex.get(longUrl));<br> }</pre><pre> public String <strong>decode</strong>(String shortUrl) { <br>   String base62Encoded =   shortUrl.substring(shortUrl.lastIndexOf(“/”)+1);<br>   <br>   long decode=0;<br>   for(int i=0;i&lt;base62Encoded.length();i++){<br>     decode = decode*62 + base62.indexOf(base62Encoded.charAt(i));<br>   }<br>   return <strong>indexToUrl.get(decode)</strong>;<br> }<br> <br> String <strong>base62Encode</strong>(long value){<br>   StringBuffer sb = new StringBuffer();<br>   while(value&gt;0){<br>     <strong>sb.append(base62.charAt((int)(value%62)));</strong><br>     value=value/62;<br>   }<br>   //to make it 7 char <br>   while(sb.length()&lt;7){<br>    <strong>sb.append(0);</strong><br>   }<br>   return sb.reverse().toString();<br> }</pre><pre>}</pre><p><em>739 / 739</em><strong><em> </em></strong>test cases passed.</p><p>Status: <em>Accepted</em></p><p>Runtime: 4 ms</p><p>Memory Usage: 39.3 MB</p><p>Hence, by alloting same space and faster runtime, we can make out Url-Shortner more scalable.</p><h4>4. Make it REST API accessible.</h4><p>To provide a stateless API to generate tiny url, and retreive the same using short Url, replace the Map and counter used in previous code with a database connection.</p><p>Saving longUrl to database, will generate a unique ID (as counter), which can be converted to Base62Encoded value and appended to base url to get a unique and scalable shortUrl.</p><p>I hope this article helps you to understand how to code step by step for designing a Url Shortner. Please do let me know if you have any questions. If you like the post, do share some claps :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b39beb0671c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing a scalable URL Shortener like Tiny URL]]></title>
            <link>https://gosunaina.medium.com/designing-a-scalable-url-shortener-like-tiny-url-72106a7018ee?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/72106a7018ee</guid>
            <category><![CDATA[url-shorteners]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[design-tinyurl]]></category>
            <category><![CDATA[tinyurl]]></category>
            <category><![CDATA[tinyurl-system-design]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Mon, 22 Mar 2021 06:29:26 GMT</pubDate>
            <atom:updated>2021-03-24T06:54:59.126Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Functional Requirements</h3><p>Url Shortener is a service that creates a short alias URL for a corresponding long URL. So, now whenever the user visits the short URL, she will be redirected to the original URL.</p><p>URL shortening services like <a href="https://bitly.com/">bit.ly</a> or <a href="https://tinyurl.com/">TinyURL</a> are very popular to generate shorter aliases for long URLs. You need to design this kind of web service where if a user gives a long URL then the service returns a short URL and if the user gives a short URL then it returns the original long URL. For example, shortening the given URL through bit.ly :</p><pre>Example: Let the original URL be: <br>Long URL: <a href="https://medium.com/p/3e17c65d05d3/edit">https://nlogn.in/horizontal-scaling-and-vertical-scaling/</a><br>Short URL is: <a href="https://bit.ly/2OYHewm">https://bit.ly/2OYHewm</a></pre><pre>Whenever you will click the second short url, you will automatically<br>be redirected to the page referred by the long URL. <br>Try Now.</pre><h3>2. Design Goals</h3><p>The URL shortening service should have the following features:</p><ol><li><strong>Mandatory Features</strong></li><li>Given a long URL it should be able to generate Unique Short URL.</li><li>Given a short URL it should redirect to the original URL.</li><li>The URL should expire after a timespan.</li><li>The system should be highly available. This is really important to consider because if the service goes down, all the URL redirection will start failing.</li><li>URL redirection should happen in real-time with minimal latency.</li><li>Shortened links should not be predictable.</li></ol><p><strong>2. Optional Features</strong></p><ol><li>The service should be REST API accessible.</li><li>It should provide analytics features i.e, how many times the URL is visited.</li><li>A user should be able to pick a custom URL.</li></ol><h3>3. Capacity Estimation</h3><p>Let’s start by making some assumptions about the traffic (for scalability) and the length of the URL.</p><p>The important thing to note is that the number of reading requests will be 1000 times more than the number of write requests.</p><p>Suppose, we are going to receive 1B(1 Billion) new URL’s shorting requests per month, with a 100:1 read/write ratio. So, the total number of redirections:</p><p>100*1B = <strong>100B</strong> redirections</p><p>If we are going to store, each URL(long and corresponding short URL) for almost 10 years, then the numbers of URLs we are going to store are:</p><p>1B*120 = <strong>120B</strong> URLs</p><p>Now if on average every URL is going to be of 200 bytes, then total storage required:</p><p>200B*120byte = <strong>24TB</strong> storages (Say Whhaaaat!!! )</p><figure><img alt="Be prepared for millions of user requests per day" src="https://cdn-images-1.medium.com/max/600/0*_G1yljGKxGnFpoxF.jpg" /></figure><h3>4. Algorithm REST Endpoints</h3><p>Let’s starts by making two functions accessible through REST API:</p><ol><li><strong>create_shortURL( </strong>long_url, api_key, custom_url, expiry_date<strong>)</strong></li><li>long_url: A long URL that needs to be shortened.</li><li>api_key: A unique API key provided to each user, to protect from the spammers, access, and resource control for the user, etc.</li><li>custom_url<em>(optional)</em>: The custom short link URL, user want to use.</li><li>expiry_date<em>(optional)</em>: The date after which the short URL becomes invalid.</li><li><strong>Return Value: </strong>The short Url generated, or error code in case of the inappropriate parameter.</li><li><strong>redirect_shortURL( </strong>short_url<strong>)</strong></li><li>short_url: The short URL generated from the above function.</li><li><strong>Return Value:</strong> The original long URL, or invalid URL error code.</li></ol><h3>5. Shortening Algorithm</h3><p>The URL that we have to generate should be a short URL. But how much short? Also, what type of encoding should be done?</p><p>Let’s consider we are using 7 characters to generate a short URL. These characters are a combination of 62 characters [A-Z, a-z, 0–9] something like <a href="http://ad.com/abXdef2."><strong>http://ad.com/abXdef2</strong>.</a></p><p><strong>URL Encoding</strong> — The encoding can be Base36( characters allowed [a-z][0–9]) or Base62( characters allowed [A-Z][a-z][0–9]) or <a href="https://stackoverflow.com/a/201491/5416395">Base64</a>(characters allowed [A-Z][a-z][0–9],’+’,’/’). Since all of the characters in Base64 are URL safe characters, we will choose <strong>Base64</strong> as our encoding technique.</p><p><strong>Url Length</strong> — Using Base64 encoding if we choose the</p><pre>URL with length 6, will give 64^6 = ~68.7 Billion URLs<br>URL with length 7, will give 64^7 = ~5 Trillion URLs<br>URL with length 8, will give 64^8 = ~281 Trillion URLs</pre><p>We have already estimated that we would be storing 120B URLs, we can safely choose the short URL to be 7 characters long.</p><p>Now, to generate a unique short URL, we can calculate the MD5 hash of the long URL, which will produce a 128-bit hash value. Now when we encode the <a href="https://stackoverflow.com/a/13296298/5416395">MD5 result to Base64</a> encoding the resultant string will be 22 characters long.</p><pre>(MD5 result = 32character output = 32*4bit = 128 bit output.<br>Base64 encoding will use 6 bit to represent each character,<br>MD5 -&gt; Base64 give (128/6) ~ 22 character output)</pre><p>For choosing the short URL, first, we can randomly swap some character of the Base64 encoding result and then pick any 7 characters randomly from the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/0*4DDg2nvPCxHUgkYg.png" /></figure><p><strong>MD5 hash generation</strong></p><pre>String generateMd5(){</pre><pre><em>// Static getInstance method is called with hashing MD5</em></pre><pre>MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);</pre><pre>// digest() method is called to calculate message digest of an input digest() return array of byte</pre><pre>byte[] messageDigest = md.digest(input.getBytes());</pre><pre>// Convert byte array into signum representation</pre><pre>BigInteger no = new BigInteger(1, messageDigest);</pre><pre>// Convert message digest into hex value</pre><pre>String hashtext = no.toString(16);</pre><pre><em>return hastext.substring(0,7);</em></pre><pre>}</pre><p><strong>Potential Issues</strong> — Using the above method we can come across 2 potential issues:</p><ol><li>MD5 can create a lot of collisions. For two or many different long URL inputs we may get the same unique id for short URL and that could cause data corruption.</li><li>If the user enters the URL in UTF-8 format or URL-encoded format, both are the same but encoding is different. Check <a href="https://www.url-encode-decode.com/">here</a>.</li><li>MD5 can create a lot of collisions. For two or many different long URL inputs we may get the same unique id for short URL and that could cause data corruption.</li></ol><p>Let’s solve the first and third issue:</p><p>We can append the user IP address to a the long URL and then do the shortening porcess, in this way we won’t get redundency or duplication. Correct?</p><p>No, two users can have same public IP addresses if they are connected via same router.</p><p>One solution is to add user-id or API key to the long URL and then do the shortening. This will work fine, but user have to be logged in to create short URL. Let’s stick to this for now.</p><p>Let’s solve the second issue:</p><p>For every request recieved we will use UTF-8 encoding only. If a URL is URL-encoded, we will first convert it into UTF-8 format. For this we can have our own service or we can use some 3rd party services.</p><h3>6. Database Design &amp; Choice</h3><p>Let’s see the data we need to store:</p><p><strong>Data Related to user</strong></p><ol><li><strong>User ID:</strong> A unique user id or API key to make user globally distinguishable.</li><li><strong>Name:</strong> The name of the user.</li><li><strong>Email:</strong> The email id of the user</li><li><strong>Password:</strong> Password of the user to facilitate login feature.</li><li><strong>Creation Date:</strong> The date on which the user was registered.</li></ol><p><strong>Data Related to ShortLink</strong></p><ol><li><strong>Short Url:</strong> 7 character long unique short URL.</li><li><strong>Original Url:</strong> The original long URL.</li><li><strong>UserId:</strong> The unique user id or API key of the user who created the short URL.</li><li><strong>Expiration Date:</strong> The date after which this short URL should become invalid.</li></ol><p><strong>Data Storage Observation</strong></p><ol><li>We will be storing Billions of links.</li><li>The short link redirection should be fast.</li><li>Our service is going to be read heavily.</li><li>The only relation that is going to exist is which user created which URL and that too is going to accessed very less.</li></ol><p>We have two different choices of databases: 1) Relational Databases(MySQL) 2) NoSQL Databases( <a href="https://en.wikipedia.org/wiki/Apache_Cassandra">Cassandra</a>).</p><p>In general, Relational Databases are good if we have lots of complex queires involving joins, but they are slow. NoSQL databases are pathetic at handling the relationship queries but they are faster.</p><p>Now, we don’t really need lots of relationship among data, but we do need the fast read and write speed. Hence we will <strong>choose NoSQL Database</strong>. The key for each row can be the shorturl, because it is going to be globally unique.</p><p>Database Sharding</p><p>To scale out our database, we need to partition it into several machines or nodes, so that it can store information about billions of URLs. Hence now we can store more data in memory because of more machines or nodes. For database sharding we will use <strong>hash based partition</strong> technique.</p><p>In this technique, we will find the hash of the shorturl we are going to store, and determine the machine/shard in which we are going to store this particular URL. The hash function will randomly distribute the URLs into different partation or shard. We can decide the number of shards we are going to make and then we can choose appropriate hash function that random number representing the partation/shard number.( Ex if we have chosen 512 shards, then hash function will generate a number between [1–512] or [0–511] ).</p><h3>7. Speeding Up the Read Operation</h3><p>We know that our database is going to be read heavily. Till now we have find the way to speed up the writing process, but the reads are still slow. So we have to find some way to speed up the reading process.</p><p><strong>Caching</strong> is the solution. We can cache the URLs that are going to be accessed frequently. For example, a url that appears on the trending page of any social networking website. Hence many people are going to visit the url. We can use the caching servies like <a href="https://en.wikipedia.org/wiki/Memcached">memcached.</a></p><p><strong>Things we need to consider after adding the caching layer:</strong></p><ol><li>When the cache is full, we need to replace the URLs in cache with the treanding ones. For this we will use LRU(Least Recently Used) policy. The URL in the cace which have been refered the least number of times will be removed.</li><li>Synchronizing the cache with the original URL. If the user updates or deletes the original link, the corresponding changes has to be reflected in the cache too.</li><li>We can shared the cache too. This will help us store more data in memory because of the more machines. For deciding which thing go to which shard can be done using “Hashing” or “Consistant Hashing”.</li></ol><p>Now this is now we have speed up our read and write request, but still our system is prone to network bandwidth bottleneck and single point of failure.</p><h3>8. Load Balancing</h3><p>To overcome the problem of limited network bandwidth and single point of failue in url shortener service, we will use <a href="https://nlogn.in/what-are-load-balancers-and-how-load-balancing-is-done/">Load Balancers</a>. Load Balancer does its magic by diving the traffic among a group of server thus resulting in improved response and availability of a website or application.</p><p>To distriubte the load among server we will use the <strong>Least Bandwidth Method. </strong>This algorithm will choose the server currently serving the least amount of traffic, measured in megabits per second (Mbps).</p><p>We can place the Load Balancers between:</p><ol><li>The client and the server.</li><li>The server and the database.</li></ol><p>This is how we will design a highly scalable URL Shortener that can work in realtime.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72106a7018ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serialization in java -NotSerializableException]]></title>
            <link>https://gosunaina.medium.com/serialization-in-java-notserializableexception-8bdf92184a4d?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bdf92184a4d</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[exception]]></category>
            <category><![CDATA[exception-handling]]></category>
            <category><![CDATA[serialization]]></category>
            <category><![CDATA[notserializable]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Thu, 18 Feb 2021 08:41:45 GMT</pubDate>
            <atom:updated>2021-06-17T07:23:45.225Z</atom:updated>
            <content:encoded><![CDATA[<p>Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.</p><h3>The Technical Rundown</h3><p>All Java errors implement the <a href="https://airbrake.io/blog/java-exception-handling/the-java-exception-class-hierarchy">java.lang.Throwable</a> interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:</p><ul><li><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html">java.lang.Object</a> extended by</li><li><a href="https://airbrake.io/blog/java-exception-handling/the-java-exception-class-hierarchy">java.lang.Throwable</a> extended by</li><li><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html">java.lang.E</a>xception extended by</li><li><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html">java.io.IOException</a> extended by</li><li><a href="https://docs.oracle.com/javase/8/docs/api/java/io/ObjectStreamException.html">java.io.ObjectStreamException</a> extended by</li><li>NotSerializableException</li></ul><h3><em>WHEN CAN IT OCCUR</em></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/0*0XebZAM4KWJPWAhc.png" /></figure><h4><em>1. When you serialize an </em><strong><em>inner class instance</em></strong></h4><p>java.io.NotSerializableException can occur when you serialize an <strong>inner class instance</strong> because:</p><blockquote><em>serializing such an inner class instance will result in serialization of its associated outer class instance as well</em></blockquote><blockquote><strong><em>Serialization of inner classes</em></strong><em> (i.e., nested classes that are not static member classes), including local and anonymous classes, is </em><strong><em>strongly discouraged</em></strong></blockquote><h4>2. When attempting to serialize or deserialize an object that does not implement the <a href="https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html">java.io.Serializable</a> interface</h4><p>Below is the full code sample:</p><pre>package io.airbrake;<br><br>import io.airbrake.utility.Logging;<br><br>import java.io.*;<br>import java.util.GregorianCalendar;<br><br>public class Main {<br><br>    public static void main(String[] args) {<br>        // Create book and test serialization.<br>        Book nameOfTheWind = new Book(&quot;The Name of the Wind&quot;, &quot;Patrick Rothfuss&quot;, 662, new GregorianCalendar(2007, 2, 27).getTime());<br>        testBookSerialization(nameOfTheWind);<br><br>        // Create and test another book.<br>        Book wiseMansFear = new Book(&quot;The Wise Man&#39;s Fear&quot;, &quot;Patrick Rothfuss&quot;, 994, new GregorianCalendar(2011, 2, 1).getTime());<br>        testBookSerialization(wiseMansFear);<br>    }<br><br>    /**<br>     * Tests serialization of passed Book object.<br>     *<br>     * @param book Book to be tested.<br>     */<br>    private static void testBookSerialization(Book book) {<br>        // Determine serialization file name.<br>        String fileName = String.format(&quot;%s.ser&quot;, book.toFilename());<br>        // Output file info.<br>        Logging.lineSeparator(String.format(&quot;SAVING TO FILE: %s&quot;, fileName), 75);<br>        // Serialize Book to file.<br>        <strong>serialize</strong>(book, fileName);<br>        // Deserialize Book from file.<br>        <strong>deserialize</strong>(fileName);<br>    }<br><br>    /**<br>     * Serializes the passed object to the passed filePath.<br>     *<br>     * @param object Object that is being serialized.<br>     * @param filePath File path where object should be stored.<br>     */<br>    private static void <strong>serialize</strong>(Object object, String filePath) {<br>        try {<br>            FileOutputStream fileOutputStream = new FileOutputStream(filePath);<br>            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);<br>            objectOutputStream.writeObject(object);<br>            fileOutputStream.close();<br>            Logging.log(String.format(&quot;SERIALIZED [%s]: %s&quot;, object.getClass().getName(), object));<br>        } catch (NotSerializableException exception) {<br>            // Output expected NotSerializeableExceptions.<br>            Logging.log(exception);<br>        } catch (IOException exception) {<br>            // Output unexpected IOException.<br>            Logging.log(exception, false);<br>        }<br>    }<br><br>    /**<br>     * Deserializes object found in passed filePath.<br>     *<br>     * @param filePath Path to file where serialized object is found.<br>     * @param &lt;T&gt; Type of object that is expected.<br>     */<br>    private static &lt;T&gt; void <strong>deserialize</strong>(String filePath) {<br>        try {<br>            FileInputStream fileInputStream = new FileInputStream(filePath);<br>            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);<br>            T object = (T) objectInputStream.readObject();<br>            objectInputStream.close();<br>            fileInputStream.close();<br>            Logging.log(String.format(&quot;DESERIALIZED [%s]: %s&quot;, object.getClass().getName(), object));<br>        } catch (NotSerializableException exception) {<br>            // Output expected NotSerializeableExceptions.<br>            Logging.log(exception);<br>        } catch (IOException | ClassNotFoundException exception) {<br>            // Output unexpected IOExceptions and ClassNotFoundExceptions.<br>            Logging.log(exception, false);<br>        }<br>    }<br>}</pre><pre>// Book.java<br>package io.airbrake;<br><br>import com.fasterxml.jackson.core.JsonProcessingException;<br>import com.fasterxml.jackson.databind.ObjectMapper;<br>import com.fasterxml.jackson.annotation.*;<br>import io.airbrake.utility.Logging;<br><br>import java.io.Serializable;<br>import java.io.UnsupportedEncodingException;<br>import java.security.MessageDigest;<br>import java.security.NoSuchAlgorithmException;<br>import java.text.DateFormat;<br>import java.util.Date;<br><br>/**<br> * Simple example class to store book instances.<br> */<br>@JsonIgnoreProperties(ignoreUnknown = true)<br>public class Book implements <strong>Serializable</strong><br>{<br>    private String author;<br>    private String title;<br>    private Integer pageCount;<br>    private Date publishedAt;<br>    private static String publicationType = &quot;Book&quot;;<br><br>    private static final Integer maximumPageCount = 4000;<br><br>    /**<br>     * Ensure publication type is upper case.<br>     */<br>    static {<br>        publicationType = publicationType.toUpperCase();<br>    }<br><br>    /**<br>     * Constructs an empty book.<br>     */<br>    public Book() { }<br><br>    /**<br>     * Constructs a basic book.<br>     *<br>     * @param title Book title.<br>     * @param author Book author.<br>     */<br>    public Book(String title, String author) {<br>        setAuthor(author);<br>        setTitle(title);<br>    }<br><br>    /**<br>     * Constructs a basic book, with page count.<br>     *<br>     * @param title Book title.<br>     * @param author Book author.<br>     * @param pageCount Book page count.<br>     */<br>    public Book(String title, String author, Integer pageCount) {<br>        setAuthor(author);<br>        setPageCount(pageCount);<br>        setTitle(title);<br>    }<br><br>    /**<br>     * Constructs a basic book, with page count.<br>     *<br>     * @param title Book title.<br>     * @param author Book author.<br>     * @param pageCount Book page count.<br>     */<br>    public Book(String title, String author, Integer pageCount, Date publishedAt) {<br>        setAuthor(author);<br>        setPageCount(pageCount);<br>        setTitle(title);<br>        setPublishedAt(publishedAt);<br>    }<br><br>    /**<br>     * Constructs a basic book, with page count.<br>     *<br>     * @param title Book title.<br>     * @param author Book author.<br>     * @param pageCount Book page count.<br>     */<br>    public Book(String title, String author, Integer pageCount, Date publishedAt, String publicationType) {<br>        setAuthor(author);<br>        setPageCount(pageCount);<br>        setTitle(title);<br>        setPublishedAt(publishedAt);<br>        setPublicationType(publicationType);<br>    }<br><br>    /**<br>     * Get author of book.<br>     *<br>     * @return Author name.<br>     */<br>    public String getAuthor() {<br>        return author;<br>    }<br><br>    /**<br>     * Get page count of book.<br>     *<br>     * @return Page count.<br>     */<br>    public Integer getPageCount() {<br>        return pageCount;<br>    }<br><br>    /**<br>     * Get publication type of book.<br>     *<br>     * @return Publication type.<br>     */<br>    public String getPublicationType() { return publicationType; }<br><br>    /**<br>     * Get published date of book.<br>     *<br>     * @return Published date.<br>     */<br>    public Date getPublishedAt() { return publishedAt; }<br><br>    /**<br>     * Get a formatted tagline with author, title, page count, and publication date.<br>     *<br>     * @return Formatted tagline.<br>     */<br>    public String getTagline() {<br>        return String.format(&quot;&#39;%s&#39; by %s is %d pages, published %s.&quot;,<br>                getTitle(),<br>                getAuthor(),<br>                getPageCount(),<br>                DateFormat.getDateInstance().format(getPublishedAt()));<br>    }<br><br>    /**<br>     * Get title of book.<br>     *<br>     * @return Title.<br>     */<br>    public String getTitle() {<br>        return title;<br>    }<br><br>    /**<br>     * Publish current book.<br>     * If book already published, throws IllegalStateException.<br>     */<br>    public void publish() throws IllegalStateException {<br>        Date publishedAt = getPublishedAt();<br>        if (publishedAt == null) {<br>            setPublishedAt(new Date());<br>            Logging.log(String.format(&quot;Published &#39;%s&#39; by %s.&quot;, getTitle(), getAuthor()));<br>        } else {<br>            throw new IllegalStateException(<br>                    String.format(&quot;Cannot publish &#39;%s&#39; by %s (already published on %s).&quot;,<br>                            getTitle(),<br>                            getAuthor(),<br>                            publishedAt));<br>        }<br>    }<br><br>    /**<br>     * Set author of book.<br>     *<br>     * @param author Author name.<br>     */<br>    public void setAuthor(String author) {<br>        this.author = author;<br>    }<br><br>    /**<br>     * Set page count of book.<br>     *<br>     * @param pageCount Page count.<br>     */<br>    public void setPageCount(Integer pageCount) throws IllegalArgumentException {<br>        if (pageCount &gt; maximumPageCount) {<br>            throw new IllegalArgumentException(String.format(&quot;Page count value [%d] exceeds maximum limit [%d].&quot;, pageCount, maximumPageCount));<br>        }<br>        this.pageCount = pageCount;<br>    }<br><br>    /**<br>     * Set publication type of book.<br>     *<br>     * @param type Publication type.<br>     */<br>    public void setPublicationType(String type) { this.publicationType = type; }<br><br>    /**<br>     * Set published date of book.<br>     *<br>     * @param publishedAt Page count.<br>     */<br>    public void setPublishedAt(Date publishedAt) {<br>        this.publishedAt = publishedAt;<br>    }<br><br>    /**<br>     * Set title of book.<br>     *<br>     * @param title Title.<br>     */<br>    public void setTitle(String title) {<br>        this.title = title;<br>    }<br><br>    /**<br>     * Get the filename formatted version of this Book.<br>     *<br>     * @return Filename.<br>     */<br>    String toFilename() {<br>        try {<br>            return java.net.URLEncoder.encode(String.format(&quot;%s-%s&quot;, getTitle(), getAuthor()).toLowerCase(), &quot;UTF-8&quot;);<br>        } catch (UnsupportedEncodingException exception) {<br>            Logging.log(exception);<br>        }<br>        return null;<br>    }<br><br>    /**<br>     * Output to JSON string.<br>     *<br>     * @return<br>     * @throws JsonProcessingException<br>     */<br>    public String toJsonString() throws JsonProcessingException {<br>        return new ObjectMapper().writeValueAsString(this);<br>    }<br><br>    /**<br>     * Get string representation of Book.<br>     *<br>     * @return String representation.<br>     */<br>    public String toString() {<br>        return getTagline();<br>    }<br><br>    /**<br>     * Throw an Exception.<br>     */<br>    public void throwException(String message) throws Exception {<br>        throw new Exception(message);<br>    }<br>}</pre><p>To test the serialization and deserialization process, we have the testBookSerialization(Book book) wrapper method:</p><pre>/**<br>* Tests serialization of passed Book object.<br>*<br>* @param book Book to be tested.<br>*/<br>private static void <strong>testBookSerialization</strong>(Book book) {<br>    // Determine serialization file name.<br>    String fileName = String.format(&quot;%s.ser&quot;, book.toFilename());<br>    // Output file info.<br>    Logging.lineSeparator(String.format(&quot;SAVING TO FILE: %s&quot;, fileName), 75);<br>    // Serialize Book to file.<br>    <strong>serialize</strong>(book, fileName);<br>    // Deserialize Book from file.<br>    <strong>deserialize</strong>(fileName);<br>}</pre><p>As you can see, all we’re doing here is creating a local file name (using the Book.toFilename()), then attempting to first serialize and then deserialize the passed Book book object.</p><p>To see if everything works as expected our Main.main(...) method creates two different Book instances and performs our serialization tests:</p><pre>public static void <strong>main</strong>(String[] args) {<br>    // Create book and test serialization.<br>    Book nameOfTheWind = new Book(&quot;The Name of the Wind&quot;, &quot;Patrick Rothfuss&quot;, 662, new GregorianCalendar(2007, 2, 27).getTime());<br>    <strong>testBookSerialization</strong>(nameOfTheWind);</pre><pre>    // Create and test another book.<br>    Book wiseMansFear = new Book(&quot;The Wise Man&#39;s Fear&quot;, &quot;Patrick Rothfuss&quot;, 994, new GregorianCalendar(2011, 2, 1).getTime());<br>    <strong>testBookSerialization</strong>(wiseMansFear);<br>}</pre><p>Executing the Main.main() method produces the following output:</p><pre>-------- SAVING TO FILE: the+name+of+the+wind-patrick+rothfuss.ser --------<br><strong>SERIALIZED</strong> [io.airbrake.Book]: &#39;The Name of the Wind&#39; by Patrick Rothfuss is 662 pages, published Mar 27, 2007.<br><strong>DESERIALIZED</strong> [io.airbrake.Book]: &#39;The Name of the Wind&#39; by Patrick Rothfuss is 662 pages, published Mar 27, 2007.<br>------- SAVING TO FILE: the+wise+man%27s+fear-patrick+rothfuss.ser --------<br><strong>SERIALIZED</strong> [io.airbrake.Book]: &#39;The Wise Man&#39;s Fear&#39; by Patrick Rothfuss is 994 pages, published Mar 1, 2011.<br><strong>DESERIALIZED</strong> [io.airbrake.Book]: &#39;The Wise Man&#39;s Fear&#39; by Patrick Rothfuss is 994 pages, published Mar 1, 2011.</pre><p>Everything seems to be working just as expected. Our encoded filenames are created, and our Book instances are successfully being serialized and saved to the file, then deserialized. To confirm, if we open up the the+name+of+the+wind-patrick+rothfuss.ser local file the contents look like this:</p><pre>�� sr io.airbrake.Bookm���smh4 L authort Ljava/lang/String;L  pageCountt Ljava/lang/Integer;L publishedAtt Ljava/util/Date;L titleq ~ xpt Patrick Rothfusssr java.lang.Integer⠤���8 I valuexr java.lang.Number������  xp  �sr java.util.Datehj�KYt  xpw  �1��xt The Name of the Wind</pre><p>Looks like a mess to us since it’s not stored in a human-readable format, but this further confirms that our serialization is working properly.</p><p>However, let’s pretend we did everything as before, expect for one minor mistake: We forgot to add implements Serializable to our Book class definition:</p><pre>public class Book<br>{<br>    // ...<br>}</pre><p>With all other testing code exactly as it was before, executing our Main.main() method again now produces the following output:</p><pre>-------- SAVING TO FILE: the+name+of+the+wind-patrick+rothfuss.ser --------<br>[<strong>EXPECTED</strong>] java.io.<strong>NotSerializableException</strong>: io.airbrake.Book<br>[<strong>UNEXPECTED</strong>] java.io.WriteAbortedException: writing aborted; java.io.<strong>NotSerializableException</strong>: io.airbrake.Book<br>------- SAVING TO FILE: the+wise+man%27s+fear-patrick+rothfuss.ser --------<br>[<strong>EXPECTED</strong>] java.io.<strong>NotSerializableException</strong>: io.airbrake.Book<br>[<strong>UNEXPECTED</strong>] java.io.WriteAbortedException: writing aborted; java.io.<strong>NotSerializableException</strong>: io.airbrake.Book</pre><p>Suddenly we experience our first NotSerializableExceptions, which indicate that the io.airbrake.Book class is not serializable. In this case, we know it’s because we didn’t implement the Serializable interface. However, it’s worth noting that serialization can <em>only</em> work if the entire series of related objects are serializable. This means that, if one of the fields of our Book class was private Publisher publisher, where Publisher was a unique class of its own, we could only serialize a Book instance if <em>both</em> Book and Publisher classes implemented the Serializable interface. Alternatively, we could also serialize a Book instance even if Publisher did not implement Serializable, but only <em>if</em> the private Publisher publisher field was annotated as a <a href="https://docs.oracle.com/javase/8/docs/api/java/beans/Transient.html">transient</a> field. The transient annotation indicates that the field should <em>not</em> be serialized.</p><h4><strong>3. If the superclass is serializable but we don’t want the subclass to be serialized.</strong></h4><p>It can be achieved by implementing the <em>writeObject()</em> and <em>readObject()</em> methods in the subclass and throwing <em>NotSerializableException</em> from these methods.</p><blockquote>// Java program to demonstrate</blockquote><blockquote>// how to prevent</blockquote><blockquote>// subclass from serialization</blockquote><blockquote><strong>import</strong> java.io.FileInputStream;</blockquote><blockquote><strong>import</strong> java.io.FileOutputStream;</blockquote><blockquote><strong>import</strong> java.io.IOException;</blockquote><blockquote><strong>import</strong> java.io.NotSerializableException;</blockquote><blockquote><strong>import</strong> java.io.ObjectInputStream;</blockquote><blockquote><strong>import</strong> java.io.ObjectOutputStream;</blockquote><blockquote><strong>import</strong> java.io.Serializable;</blockquote><blockquote>// superclass A</blockquote><blockquote>// implementing Serializable interface</blockquote><blockquote><strong>class</strong> A <strong>implements</strong> Serializable</blockquote><blockquote>{</blockquote><blockquote><strong>int</strong> i;</blockquote><blockquote>// parameterized constructor</blockquote><blockquote><strong>public</strong> A(<strong>int</strong> i)</blockquote><blockquote>{</blockquote><blockquote><strong>this</strong>.i = i;</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>// subclass B</blockquote><blockquote>// B class doesn&#39;t implement Serializable</blockquote><blockquote>// interface.</blockquote><blockquote><strong>class</strong> B <strong>extends</strong> A</blockquote><blockquote>{</blockquote><blockquote><strong>int</strong> j;</blockquote><blockquote>// parameterized constructor</blockquote><blockquote><strong>public</strong> B(<strong>int</strong> i,<strong>int</strong> j)</blockquote><blockquote>{</blockquote><blockquote><strong>super</strong>(i);</blockquote><blockquote><strong>this</strong>.j = j;</blockquote><blockquote>}</blockquote><blockquote>// By implementing writeObject method,</blockquote><blockquote>// we can prevent</blockquote><blockquote>// subclass from serialization</blockquote><blockquote><strong>private</strong> <strong>void</strong> writeObject(ObjectOutputStream out) <strong>throws</strong> IOException</blockquote><blockquote>{</blockquote><blockquote><strong>throw</strong> <strong>new</strong> NotSerializableException();</blockquote><blockquote>}</blockquote><blockquote>// By implementing readObject method,</blockquote><blockquote>// we can prevent</blockquote><blockquote>// subclass from de-serialization</blockquote><blockquote><strong>private</strong> <strong>void</strong> readObject(ObjectInputStream in) <strong>throws</strong> IOException</blockquote><blockquote>{</blockquote><blockquote><strong>throw</strong> <strong>new</strong> NotSerializableException();</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>// Driver class</blockquote><blockquote><strong>public</strong> <strong>class</strong> Test</blockquote><blockquote>{</blockquote><blockquote><strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args)</blockquote><blockquote><strong>throws</strong> Exception</blockquote><blockquote>{</blockquote><blockquote>B b1 = <strong>new</strong> B(10, 20);</blockquote><blockquote>System.out.println(&quot;i = &quot; + b1.i);</blockquote><blockquote>System.out.println(&quot;j = &quot; + b1.j);</blockquote><blockquote>// Serializing B&#39;s(subclass) object</blockquote><blockquote>//Saving of object in a file</blockquote><blockquote>FileOutputStream fos = <strong>new</strong> FileOutputStream(&quot;abc.ser&quot;);</blockquote><blockquote>ObjectOutputStream oos = <strong>new</strong> ObjectOutputStream(fos);</blockquote><blockquote>// Method for serialization of B&#39;s class object</blockquote><blockquote>oos.writeObject(b1);</blockquote><blockquote>// closing streams</blockquote><blockquote>oos.close();</blockquote><blockquote>fos.close();</blockquote><blockquote>System.out.println(&quot;Object has been serialized&quot;);</blockquote><blockquote>// De-Serializing B&#39;s(subclass) object</blockquote><blockquote>// Reading the object from a file</blockquote><blockquote>FileInputStream fis = <strong>new</strong> FileInputStream(&quot;abc.ser&quot;);</blockquote><blockquote>ObjectInputStream ois = <strong>new</strong> ObjectInputStream(fis);</blockquote><blockquote>// Method for de-serialization of B&#39;s class object</blockquote><blockquote>B b2 = (B)ois.readObject();</blockquote><blockquote>// closing streams</blockquote><blockquote>ois.close();</blockquote><blockquote>fis.close();</blockquote><blockquote>System.out.println(&quot;Object has been deserialized&quot;);</blockquote><blockquote>System.out.println(&quot;i = &quot; + b2.i);</blockquote><blockquote>System.out.println(&quot;j = &quot; + b2.j);</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><p>Output:</p><pre>i = 10<br>j = 20<br>Exception in thread &quot;main&quot; java.io.NotSerializableException<br>    at B.writeObject(Test.java:44)</pre><h4><strong>4. When superclass implements marker interface</strong></h4><p>The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata. In java, it is used as interfaces with no method specified.</p><p>A <em>major problem</em> with marker interfaces is that an interface defines a contract for implementing classes, and that contract is inherited by all subclasses. This means that you cannot “un-implement” a marker. In the example given, if you create a subclass that you do not want to serialize (perhaps because it depends on transient state), you must resort to explicitly throwing NotSerializableException (as shown above in point 3).</p><p>Please refer to my article on <a href="https://gosunaina.medium.com/serialization-in-java-inheritance-66da3c453e51"><strong>Serialization in java</strong></a> for more details. You may write to me in the comments, if you wish to contribute.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bdf92184a4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serialization in java — Inheritance]]></title>
            <link>https://gosunaina.medium.com/serialization-in-java-inheritance-66da3c453e51?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/66da3c453e51</guid>
            <category><![CDATA[serialization]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Thu, 18 Feb 2021 07:34:02 GMT</pubDate>
            <atom:updated>2021-02-18T07:34:02.775Z</atom:updated>
            <content:encoded><![CDATA[<h3>Serialization in java — Inheritance</h3><p>Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.</p><p>There are some cases of Serialization with respect to inheritance :</p><ol><li><strong>Case 1: If superclass is serializable then subclass is automatically serializable </strong>: If superclass is Serializable, then by default every subclass is serializable. Hence, even though subclass doesn’t implement Serializable interface( and if it’s superclass implements Serializable), then we can serialize subclass object.</li></ol><blockquote>// Java program to demonstrate <br>// that if superclass is <br>// serializable then subclass <br>// is automatically serializable <br> <br>import java.io.FileInputStream; <br>import java.io.FileOutputStream; <br>import java.io.ObjectInputStream; <br>import java.io.ObjectOutputStream; <br>import java.io.Serializable; <br> <br>// superclass A <br>// implementing Serializable interface <br>class A implements Serializable <br>{ <br> int i; <br> <br> // parameterized constructor <br> public A(int i) <br> { <br> this.i = i; <br> } <br> <br>} <br> <br>// subclass B <br>// B class doesn’t implement Serializable <br>// interface. <br>class B extends A <br>{ <br> int j; <br> <br> // parameterized constructor <br> public B(int i, int j) <br> { <br> super(i); <br> this.j = j; <br> } <br>} <br> <br>// Driver class <br>public class Test <br>{ <br> public static void main(String[] args) <br> throws Exception <br> { <br> B b1 = new B(10,20); <br> <br> System.out.println(“i = “ + b1.i); <br> System.out.println(“j = “ + b1.j); <br> <br> /* Serializing B’s(subclass) object */<br> <br> //Saving of object in a file <br> FileOutputStream fos = new FileOutputStream(“abc.ser”); <br> ObjectOutputStream oos = new ObjectOutputStream(fos); <br> <br> // Method for serialization of B’s class object <br> oos.writeObject(b1); <br> <br> // closing streams <br> oos.close(); <br> fos.close(); <br> <br> System.out.println(“Object has been serialized”); <br> <br> /* De-Serializing B’s(subclass) object */<br> <br> // Reading the object from a file <br> FileInputStream fis = new FileInputStream(“abc.ser”); <br> ObjectInputStream ois = new ObjectInputStream(fis); <br> <br> // Method for de-serialization of B’s class object <br> B b2 = (B)ois.readObject(); <br> <br> // closing streams <br> ois.close(); <br> fis.close(); <br> <br> System.out.println(“Object has been deserialized”); <br> <br> System.out.println(“i = “ + b2.i); <br> System.out.println(“j = “ + b2.j); <br> } <br>}</blockquote><p>Output:</p><pre>i = 10<br>j = 20<br>Object has been serialized<br>Object has been deserialized<br>i = 10<br>j = 20</pre><p><strong>If a superclass is not serializable then subclass can still be serialized : </strong>Even though superclass doesn’t implements Serializable interface, we can serialize subclass object if subclass itself implements Serializable interface. So we can say that to serialize subclass object, superclass need not to be serializable. But what happens with the instances of superclass during serialization in this case. The following procedure explain this.</p><p><strong>What happens when a class is serializable but its superclass is not ?</strong></p><ul><li><strong>Serialization:</strong> At the time of serialization, if any instance variable is inheriting from non-serializable superclass, then JVM ignores original value of that instance variable and save default value to the file.</li><li><strong>De- Serialization: </strong>At the time of de-serialization, if any non-serializable superclass is present, then JVM will execute instance control flow in the superclass. To execute instance control flow in a class, JVM will always invoke default(no-arg) constructor of that class. So <strong><em>every non-serializable superclass must necessarily contain default constructor, otherwise we will get runtime-exception.</em></strong></li></ul><blockquote>// Java program to demonstrate</blockquote><blockquote>// the case if superclass need</blockquote><blockquote>// not to be serializable</blockquote><blockquote>// while serializing subclass</blockquote><blockquote><strong>import</strong> java.io.FileInputStream;</blockquote><blockquote><strong>import</strong> java.io.FileOutputStream;</blockquote><blockquote><strong>import</strong> java.io.ObjectInputStream;</blockquote><blockquote><strong>import</strong> java.io.ObjectOutputStream;</blockquote><blockquote><strong>import</strong> java.io.Serializable;;</blockquote><blockquote>// superclass A</blockquote><blockquote>// A class doesn&#39;t implement Serializable</blockquote><blockquote>// interface.</blockquote><blockquote><strong>class</strong> A</blockquote><blockquote>{</blockquote><blockquote><strong>int</strong> i;</blockquote><blockquote>// parameterized constructor</blockquote><blockquote><strong>public</strong> A(<strong>int</strong> i)</blockquote><blockquote>{</blockquote><blockquote><strong>this</strong>.i = i;</blockquote><blockquote>}</blockquote><blockquote>// default constructor</blockquote><blockquote>// this constructor must be present</blockquote><blockquote>// otherwise we will get runtime exception</blockquote><blockquote><strong>public</strong> A()</blockquote><blockquote>{</blockquote><blockquote>i = 50;</blockquote><blockquote>System.out.println(&quot;A&#39;s class constructor called&quot;);</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>// subclass B</blockquote><blockquote>// implementing Serializable interface</blockquote><blockquote><strong>class</strong> B <strong>extends</strong> A <strong>implements</strong> Serializable</blockquote><blockquote>{</blockquote><blockquote><strong>int</strong> j;</blockquote><blockquote>// parameterized constructor</blockquote><blockquote><strong>public</strong> B(<strong>int</strong> i,<strong>int</strong> j)</blockquote><blockquote>{</blockquote><blockquote><strong>super</strong>(i);</blockquote><blockquote><strong>this</strong>.j = j;</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>// Driver class</blockquote><blockquote><strong>public</strong> <strong>class</strong> Test</blockquote><blockquote>{</blockquote><blockquote><strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args)</blockquote><blockquote><strong>throws</strong> Exception</blockquote><blockquote>{</blockquote><blockquote>B b1 = <strong>new</strong> B(10,20);</blockquote><blockquote>System.out.println(&quot;i = &quot; + b1.i);</blockquote><blockquote>System.out.println(&quot;j = &quot; + b1.j);</blockquote><blockquote>// Serializing B&#39;s(subclass) object</blockquote><blockquote>//Saving of object in a file</blockquote><blockquote>FileOutputStream fos = <strong>new</strong> FileOutputStream(&quot;abc.ser&quot;);</blockquote><blockquote>ObjectOutputStream oos = <strong>new</strong> ObjectOutputStream(fos);</blockquote><blockquote>// Method for serialization of B&#39;s class object</blockquote><blockquote>oos.writeObject(b1);</blockquote><blockquote>// closing streams</blockquote><blockquote>oos.close();</blockquote><blockquote>fos.close();</blockquote><blockquote>System.out.println(&quot;Object has been serialized&quot;);</blockquote><blockquote>// De-Serializing B&#39;s(subclass) object</blockquote><blockquote>// Reading the object from a file</blockquote><blockquote>FileInputStream fis = <strong>new</strong> FileInputStream(&quot;abc.ser&quot;);</blockquote><blockquote>ObjectInputStream ois = <strong>new</strong> ObjectInputStream(fis);</blockquote><blockquote>// Method for de-serialization of B&#39;s class object</blockquote><blockquote>B b2 = (B)ois.readObject();</blockquote><blockquote>// closing streams</blockquote><blockquote>ois.close();</blockquote><blockquote>fis.close();</blockquote><blockquote>System.out.println(&quot;Object has been deserialized&quot;);</blockquote><blockquote>System.out.println(&quot;i = &quot; + b2.i);</blockquote><blockquote>System.out.println(&quot;j = &quot; + b2.j);</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><p>Output:</p><pre>i = 10<br>j = 20<br>Object has been serialized<br>A&#39;s class constructor called<br>Object has been deserialized<br>i = 50<br>j = 20</pre><p><strong>If the superclass is serializable but we don’t want the subclass to be serialized : </strong>There is no direct way to prevent subclass from serialization in java. One possible way by which a programmer can achieve this is by implementing the <em>writeObject()</em> and <em>readObject()</em> methods in the subclass and needs to throw <em>NotSerializableException</em> from these methods. These methods are executed during serialization and de-serialization respectively. By overriding these methods, we are just implementing our own custom serialization.</p><blockquote>// Java program to demonstrate</blockquote><blockquote>// how to prevent</blockquote><blockquote>// subclass from serialization</blockquote><blockquote><strong>import</strong> java.io.FileInputStream;</blockquote><blockquote><strong>import</strong> java.io.FileOutputStream;</blockquote><blockquote><strong>import</strong> java.io.IOException;</blockquote><blockquote><strong>import</strong> java.io.NotSerializableException;</blockquote><blockquote><strong>import</strong> java.io.ObjectInputStream;</blockquote><blockquote><strong>import</strong> java.io.ObjectOutputStream;</blockquote><blockquote><strong>import</strong> java.io.Serializable;</blockquote><blockquote>// superclass A</blockquote><blockquote>// implementing Serializable interface</blockquote><blockquote><strong>class</strong> A <strong>implements</strong> Serializable</blockquote><blockquote>{</blockquote><blockquote><strong>int</strong> i;</blockquote><blockquote>// parameterized constructor</blockquote><blockquote><strong>public</strong> A(<strong>int</strong> i)</blockquote><blockquote>{</blockquote><blockquote><strong>this</strong>.i = i;</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>// subclass B</blockquote><blockquote>// B class doesn&#39;t implement Serializable</blockquote><blockquote>// interface.</blockquote><blockquote><strong>class</strong> B <strong>extends</strong> A</blockquote><blockquote>{</blockquote><blockquote><strong>int</strong> j;</blockquote><blockquote>// parameterized constructor</blockquote><blockquote><strong>public</strong> B(<strong>int</strong> i,<strong>int</strong> j)</blockquote><blockquote>{</blockquote><blockquote><strong>super</strong>(i);</blockquote><blockquote><strong>this</strong>.j = j;</blockquote><blockquote>}</blockquote><blockquote>// By implementing writeObject method,</blockquote><blockquote>// we can prevent</blockquote><blockquote>// subclass from serialization</blockquote><blockquote><strong>private</strong> <strong>void</strong> writeObject(ObjectOutputStream out) <strong>throws</strong> IOException</blockquote><blockquote>{</blockquote><blockquote><strong>throw</strong> <strong>new</strong> NotSerializableException();</blockquote><blockquote>}</blockquote><blockquote>// By implementing readObject method,</blockquote><blockquote>// we can prevent</blockquote><blockquote>// subclass from de-serialization</blockquote><blockquote><strong>private</strong> <strong>void</strong> readObject(ObjectInputStream in) <strong>throws</strong> IOException</blockquote><blockquote>{</blockquote><blockquote><strong>throw</strong> <strong>new</strong> NotSerializableException();</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><blockquote>// Driver class</blockquote><blockquote><strong>public</strong> <strong>class</strong> Test</blockquote><blockquote>{</blockquote><blockquote><strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args)</blockquote><blockquote><strong>throws</strong> Exception</blockquote><blockquote>{</blockquote><blockquote>B b1 = <strong>new</strong> B(10, 20);</blockquote><blockquote>System.out.println(&quot;i = &quot; + b1.i);</blockquote><blockquote>System.out.println(&quot;j = &quot; + b1.j);</blockquote><blockquote>// Serializing B&#39;s(subclass) object</blockquote><blockquote>//Saving of object in a file</blockquote><blockquote>FileOutputStream fos = <strong>new</strong> FileOutputStream(&quot;abc.ser&quot;);</blockquote><blockquote>ObjectOutputStream oos = <strong>new</strong> ObjectOutputStream(fos);</blockquote><blockquote>// Method for serialization of B&#39;s class object</blockquote><blockquote>oos.writeObject(b1);</blockquote><blockquote>// closing streams</blockquote><blockquote>oos.close();</blockquote><blockquote>fos.close();</blockquote><blockquote>System.out.println(&quot;Object has been serialized&quot;);</blockquote><blockquote>// De-Serializing B&#39;s(subclass) object</blockquote><blockquote>// Reading the object from a file</blockquote><blockquote>FileInputStream fis = <strong>new</strong> FileInputStream(&quot;abc.ser&quot;);</blockquote><blockquote>ObjectInputStream ois = <strong>new</strong> ObjectInputStream(fis);</blockquote><blockquote>// Method for de-serialization of B&#39;s class object</blockquote><blockquote>B b2 = (B)ois.readObject();</blockquote><blockquote>// closing streams</blockquote><blockquote>ois.close();</blockquote><blockquote>fis.close();</blockquote><blockquote>System.out.println(&quot;Object has been deserialized&quot;);</blockquote><blockquote>System.out.println(&quot;i = &quot; + b2.i);</blockquote><blockquote>System.out.println(&quot;j = &quot; + b2.j);</blockquote><blockquote>}</blockquote><blockquote>}</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66da3c453e51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[LeetCode] Contains Duplicate III]]></title>
            <link>https://gosunaina.medium.com/leetcode-contains-duplicate-iii-bfa8e0c13f69?source=rss-44f38eb9082e------2</link>
            <guid isPermaLink="false">https://medium.com/p/bfa8e0c13f69</guid>
            <category><![CDATA[leetcode-solution]]></category>
            <category><![CDATA[contains-duplicate]]></category>
            <dc:creator><![CDATA[Sunaina Goyal]]></dc:creator>
            <pubDate>Wed, 09 Sep 2020 05:40:20 GMT</pubDate>
            <atom:updated>2021-03-03T15:51:47.448Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*skLSECyXrYTbp2oH" /><figcaption>Contains Duplicate |||</figcaption></figure><p>Problem Statement<br>Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.</p><p>Example 1:<br>Input: nums = [1,2,3,1], k = 3, t = 0<br>Output: true<br>Example 2:<br>Input: nums = [1,0,1,1], k = 1, t = 2<br>Output: true<br>Example 3:<br>Input: nums = [1,5,9,1,5,9], k = 2, t = 3<br>Output: false</p><p>Basic Idea<br>Two requirements<br>The absolute difference between nums[i] and nums[j] is at most t.<br>The absolute difference between i and j is at most k.</p><p>Solution</p><p>The naive solution is to maintain a sliding window with size k, when we add an element nums[i], compare the nums[i] with each element in the window. If it is less or equal to t, return true. We return true because the distance between i and each element in the window must be less or equal to k. The complexity of this solution would be O(nk).</p><p>We could use a treeSet to improve the complexity. The treeSet is essentially a balanced binary search tree. We put k elements in the treeSet, which maintains a sliding window. So when we insert an element to the treeSet and the index is now greater than or equal to k, we need to slide the window by 1 by removing the first element (in the window) from the treeset.</p><p>So the basic idea is for each element nums[i], we check if there is any element between [nums[i] — t, nums[i] + t]. If yes, return true.</p><pre><strong>class</strong> Solution {<br> <br>boolean <strong>containsNearbyAlmostDuplicate</strong>(int[] nums, int k, int t) {<br> int n=nums.length;<br> TreeSet&lt;<strong>Integer</strong>&gt; set = new TreeSet&lt;&gt;();<br>        <br> for(int i=0; i&lt; n;i++){<br>  Integer floor = set.floor(nums[i]+t);<br>  Integer ceiling = set.ceiling(nums[i]-t);<br>            <br>  if((<strong>floor!=null </strong>&amp;&amp; floor&gt;=nums[i]) || (<strong>ceiling!=null</strong> &amp;&amp; ceiling&lt;=nums[i])){<br>       return <strong><em>true</em></strong>;<br>  }<br>            <br>  set.add(nums[i]);<br>  if(i&gt;=k){<br>      set.remove(nums[i-k]);<br>  }<br> }<br> return <em>false</em>;<br> }<br>}</pre><blockquote>Example 4:</blockquote><blockquote>[-2147483648,-2147483647],<strong> </strong>k = 3, t = 3</blockquote><blockquote>For the scenarios involving minimum(-2147483648) or maximum(<em>2147483647</em>) Integer values as elements in the array, using a TreeSet with generic type <strong>‘Integer’</strong> can cause <strong><em>Overflow or Underflow.</em></strong></blockquote><p>A simple fix, to handle the underflow or overflow, is to use a <em>different data type. </em>If we want to allow values larger than <em>2147483647</em> (or smaller than <em>-2147483648</em>), we can simply use the <em>long</em> data type or a <em>BigInteger</em> instead.</p><p>Though variables of type <em>long</em> can also overflow, the minimum and maximum values are much larger and are probably sufficient in most situations.</p><p>The value range of <em>BigInteger</em> is not restricted, except by the amount of memory available to the JVM.</p><p>So, here, we are going to use ‘Long’ as the data type of the TreeSet elements.</p><pre><strong>class</strong> FixedSolution {<br> <br>boolean <strong>containsNearbyAlmostDuplicate</strong>(int[] nums, int k, int t) {<br> int n=nums.length;<br> TreeSet&lt;<strong>Long</strong>&gt; set = new TreeSet&lt;&gt;();<br> <br> <br> for(int i=0; i&lt; n;i++){<br> Long floor = set.floor((long)nums[i]+t);<br> Long ceiling = set.ceiling((long)nums[i]-t);<br> <br> if((<strong>floor!=null</strong> &amp;&amp; floor&gt;=nums[i]) || (<strong>ceiling!=null</strong> &amp;&amp; ceiling&lt;=nums[i])){<br> return <strong>true</strong>;<br> }<br> <br> set.add((long)nums[i]);<br> <br> if(i&gt;=k){<br> set.remove((long)nums[i-k]);<br> }<br> }<br> <br> return false;<br> }<br>}</pre><p>Runtime: <strong>29 ms</strong></p><p>Memory Usage: <strong>41.8 MB</strong></p><p>Please give me a clap if this article helped you. This is my first article on medium, so suggest for improvements.</p><p>Thanks and keep reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bfa8e0c13f69" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>