• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Convert a mutable class to immutable class

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have a class which is mutable. This class is available in a 3rd party jar and thus I do not own the source code.

I have to use this class in multithreaded scenario and I thus wanted to have a immutable version of it so that I can use it freely in my threads.

The existing class looks like

class Order
{
private int orderNum;

private ArrayList items;

private Date orderDate;

getters method

setters method

}

Can somebody tell me how do I create a immutable version of this class.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could write your own wrapper class. Each instance of your class contains an instance of Order. You write getter methods that delegate to Order, but don't write any setters. You'll need a constructor to set all Tue values on creation.

Any use?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew's suggestion is good, but ofcourse that will not allow you to use your own wrapper class in place of the original class - if the API of the 3rd party JAR expects Order objects to be passed to its methods, then you can only pass it Order objects (or objects of subclasses of Order).

There's no way to make an existing class, that you can't modify, immutable so that other existing code will suddenly treat it as if it is immutable.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pankaj Kumarkk wrote:Can somebody tell me how do I create a immutable version of this class.


Check this out http://www.javaranch.com/journal/2003/04/immutable.htm Of course what Jesper said, still stands.
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:You could write your own wrapper class. Each instance of your class contains an instance of Order. You write getter methods that delegate to Order, but don't write any setters. You'll need a constructor to set all Tue values on creation.


That's not enough. All getters must return defensive copies of the data it returns. For instance, it can't return order.getOrderDate() since Date is mutable. It must therefore return a clone of that date instead. The same needs to be done for all mutable values; the ArrayList can be wrapped using Collections.unmodifiableList.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic