• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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: 11497
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: 22849
132
Eclipse IDE Spring 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.
reply
    Bookmark Topic Watch Topic
  • New Topic