Update immutable object without breaking immutability - java

how can I get updated immutable object from another immutable object without breaking the immutability in a good way. Something similar to how things are achieved in Scala or how they are achieved in Immutables library.
How can I achieve something like
Customer.Builder.from(anotherCustomer).replaceOrder(oldOrder,newOrder).with(newName).build()
To reuse the old objects and queue all the changes I want in order to construct a new one.
I am using Java.

public class Customer {
private String name;
private Order order
private Customer(CustomerBuilder builder) {
this.name = builder.name;
this.order = builder.order;
}
public Customer replace(Order order) {
return new CustomerBuilder().name(this.name).order(order).build();
}
public static class CustomerBuilder {
private String name;
private Order order
public CustomerBuilder name(String name) {
this.name = name;
return this;
}
public CustomerBuilder order(Order order) {
this.order = order;
return this;
}
public Customer build() {
return new Customer(this);
}
}
}

I recommend this book who cover the subject :
https://www.amazon.ca/Effective-Java-Edition-Joshua-Bloch/dp/0321356683
Best way I know to update immutable object is to return modified copy of the object, like java String
String uppercase = "hello".toUpperCase()
I make easy to use fluent typing
String uppercasewithoutletterL = "hello".toUpperCase().replace("L","")

Here's a little recursive function that creates a changed immutable. It re-creates every nested object that includes the change.
const changeImmutable = (immutable: any, path: string[], newVal: any): any => {
if(path.length) {
let copy = Array.isArray(immutable) ? [...immutable] : {...immutable};
copy[path[0]] = changeImmutable(copy[path[0]], path.slice(1), newVal);
return copy;
}
return newVal;
};
example of usage
let immutableObj = {a: {b: [{c: 'foo'}]}};
// change 'foo' to 'bar'
let newImmutableObj = changeImmutable(immutableObj, ['a', 'b', 0, 'c'], 'bar');

You can implement Cloneable interface in object type.
Another option is to have builder for your immutable objects which can create new instance from existing one. Something like those fellas done...

Related

Is it bad practice to return Enums in Java?

Lets say I have a class to model an item in a game like so:
public class Item {
private final EnumItem type;
public Item(EnumItem type) {
this.type = type;
}
public Item(String name) {
this.type = EnumItem.fromName(name);
}
}
public enum EnumItem {
MACHINE_GUN("machine_gun"),
SWORD("sword"),
BAT("bat"),
DEFAULT("default");
private final String name;
public EnumItem(name) {
this.name = name;
}
public String getName() { return name; }
public static EnumItem fromName(String name) {
for(EnumItem i: EnumItem.values()) {
if(i.name.equals(name)) {
return i;
} else {
return EnumItem.DEFAULT;
}
}
}
}
Assume that .equals() and .hashCode() of Item are overridden correctly to compare the internal Enum.
Now I want a way to distinguish these items with a getter in Item: should I return an Enum or the String name? Is it good practice to return an Enum in general? Or is there a better way to distinguish these Items? Because returning the enum kind of looks like exposing the rep to me and I don't want my colleagues to use EnumItem directly to compare Items.
The approaches I thought of are the following:
string getName() to do something like item1.getName().equals("machine_gun");
EnumItem getEnum() to do item1.getEnum().equals(EnumItem.MACHINE_GUN);
item1.equals(new Item("machine_gun"));
static name(String name) { new Item(name) } to do item1.equals(Item.name("machine_gun"));
I don't know what should I do, I'd appreciate some insight from experienced programmers.
I know they look like they would from context, but in my use case these items have no special functionality that would justify extending from the base Item class.
Is this good practice? Sure, you're using aggregation since Item doesn't depend on EnumItem, which is fine. That being said, could it be done better? Sure. Is the alternative I provide the only solution? No.
Alternative
If you want this to be extensible, consider using an interface to represent an item. Then allow the interface to extend this interface to provide some standard types. Alternatively you could use composition or aggregation to define a type inside EnumItem that implements the Item interface to ensure that equals/hashcode for the Item are always override and adhere to some contract.
interface Item {
String key();
}
enum EnumItem implement Item {
private final String key;
EnumItem(String key) {
this.key = key;
}
#Override
public String key() {
return key;
}
}
class AbstractItem implements Item {
// constructor, override name()
}
Item item = EnumItem.FOO_BAR;
Item item2 = new AbstractItem("FooBar");
Item item3 = () -> "FooBar";

Loop over object setters java [duplicate]

This question already has answers here:
Invoking all setters within a class using reflection
(4 answers)
Closed 5 years ago.
I have a POJO object and a collection of appropriate data.
import java.util.ArrayList;
import java.util.List;
public class TestPojo {
private String name;
private String number;
private String id;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static void main(String[] args) {
TestPojo test = new TestPojo();
List<String> sampleData = new ArrayList<>();
sampleData.add("Bob");
sampleData.add("641-613-623");
sampleData.add("id-1451");
sampleData.add("Male");
test.setName(sampleData.get(0));
test.setNumber(sampleData.get(1));
test.setId(sampleData.get(2));
test.setSex(sampleData.get(3));
}
}
My question is how can i fill my POJO object with data in a loop? Is it posible to iterate all object setters and set data from List in appropriate places? I know that reflection can help in this case.
Here is an simple example to call setters via reflection (which needs to be adjusted):
[if this is a good approach, is another question. But to answer your question:]
public static void main(String[] args) throws Exception
{
//this is only to demonstrate java reflection:
Method[] publicMethods = TestPojo.class.getMethods(); //get all public methods
TestPojo testObj = TestPojo.class.newInstance(); //when you have a default ctor (otherwise get constructors here)
for (Method aMethod : publicMethods) //iterate over methods
{
//check name and parameter-count (mabye needs some more checks...paramter types can also be checked...)
if (aMethod.getName().startsWith("set") && aMethod.getParameterCount() == 1)
{
Object[] parms = new Object[]{"test"}; //only one parm (can be multiple params)
aMethod.invoke(testObj, parms); //call setter-method here
}
}
}
You can also save all setter-methods in an list/set for later re-use...
But as others already said, you have to be careful by doing so (using reflection)!
Cheers!
You can't easily - and you shouldn't.
You see, your POJO class offers some setters. All of them have a distinct meaning. Your first mistake is that all of these fields are strings in your model:
gender is not a string. It would rather be an enum.
"number" is not a string. It should rather be int/long/double (whatever the idea behind that property is)
In other words: you premise that "input" data is represented as array/list is already flawed.
The code you have written provides almost no helpful abstractions. So - instead of worrying how to call these setter methods in some loop context - you should rather step back and improve your model.
And hint: if this is really about populating POJO objects from string input - then get your string into JSON format, and use tools such as gson or jackson to do that (reflection based) mapping for you.
"Iterating over methods" seems pretty much of a wrong idea in OO programming. You could simply add a constructor to your class setting all of your attributes, and then just call that constructor in a loop as desired to create new objects with data you desire.
In your class define:
public TestPojo(String name, String number, String id, String sex){
this.name = name;
this.number = number;
this.id = id;
this.sex = sex;
}
Also using a List makes no much sense here. I'd recommend using a HashMap to then iterate over it in a for loop making proper calls of the above constructor.

Good way to create a immutable class with modifiers (thread-safe)

I have a case when I want to avoid defensive copies, for data which might nevertheless be modified, but is usually simply read, and not written to. So, I'd like to use immutable objects, with functional mutator methods, which is kind of usual (java lombok is able to do it more or less automatically). The way I proceed is the following:
public class Person {
private String name, surname;
public Person(String name, String surname) {....}
// getters...
// and instead of setters
public Person withName(String name) {
Person p= copy(); // create a copy of this...
p.name= name;
return p;
}
public Person copy() {....}
}
So, to get a copy of the person with a different name, I would call
p= new Person("Bar", "Alfred");
...
p= p.withName("Foo");
In practice, the objects are rather large (and I ended up using serialization to avoid the burden of writing the copy code).
Now, while browsing the web, I see a potential concurrency problem with this implementation, as my fields are not final, and thus, concurrent access might see the returned copy, for instance, without the new name change (as there is no warrantee on the order of operation in this context).
Of course, I can't make my fields final, with the current implementation, as I first do a copy, and then change the data in the copy.
So, I'm looking for a good solution for this problem.
I might use volatile, but I feel it's not a good solution.
Another solution would be to use the builder pattern:
class PersonBuilder {
String name, surname; ....
}
public class Person {
private final String name, surname;
public Person(PersonBuilder builder) {...}
private PersonBuilder getBuilder() {
return new PersonBuilder(name, surname);
}
public Person withName(String name) {
PersonBuilder b= getBuilder();
b.setName(name);
return new Person(b);
}
}
Is there any problem here, and above all, is there a more elegant way of doing the same thing ?
I recommend you take a look at Guava's immutable collections, such as immutable list and how they create lists from builders etc.
The idiom is the following:
List<String> list1 = ImmutableList.of("a","b","c"); // factory method
List<String> list2 = ImmutableList.builder() // builder pattern
.add("a")
.add("b")
.add("c")
.build();
List<String> list3 = ... // created by other means
List<String> immutableList3 = ImmutableList.copyOf(list3); // immutable copy, lazy if already immutable
I really like the idiom above. For an entity builder I would take the following approach:
Person johnWayne = Person.builder()
.firstName("John")
.lastName("Wayne")
.dob("05-26-1907")
.build();
Person johnWayneClone = johnWayne.copy() // returns a builder!
.dob("06-25-2014")
.build();
The builder here can be obtained from an existing instance via the copy() method or via a static method on the Person class (a private constructor is recommended) that return a person builder.
Note that the above mimics a little Scala's case classes in that you can create a copy from an existing instance.
Finally, don't forget to follow the guidelines for immutable classes:
make the class final or make all getters final (if the class can be extended);
make all fields final and private;
initialize all fields in the constructor (which can be private if you provide a builder and/or factory methods);
make defensive copies from getters if returning mutable objects (mutable collections, dates, third party classes, etc.).
One possibility is to separate your interfaces surrounding such objects into an immutable variant (providing getters) and a mutable variant (providing getters and setters).
public interface Person {
String getName();
}
public interface MutablePerson extends Person {
void setName(String name);
}
It doesn't solve the mutability of the object per se but it does offer some guarantees that when you pass around the object using the immutable interface reference, you know that the code you're passing this to won't change your object. Obviously you need to control the references to the underlying object and determine the subset of functionality that has control of a reference via the mutable interface.
It doesn't solve the underlying problem and I would favour immutable objects until I definitely need a mutable version. The builder approach works nicely, and you can integrate it within the object to give a modifier thus:
Person newPerson = existingPerson.withAge(30);
Why not make your fields final and your modifier methods directly create new objects?
public class Person {
private final String name, surname;
public Person(String name, String surname) {....}
// getters...
// and instead of setters
public Person withName(String newName) {
return new Person(newName, surname);
}
}
Your problem boils down to this: You want a method that safely publishes an effectively immutable, almost-but-not-quite-faithful copy of an effectively immutable object.
I'd go with the builder solution: It's verbose as all get out, but Eclipse helps with that, and it allows all of the published objects to be actually immutable. Actual immutability makes safe publication a no-brainer.
If I wrote it, it'd look like this:
class Person {
public static final FooType DEFAULT_FOO = ...;
public static final BarType DEFAULT_BAR = ...;
public static final BazType DEFAULT_BAZ = ...;
...
private final FooType foo;
private final BarType bar;
private final BazType baz;
...
private Person(Builder builder) {
this.foo = builder.foo;
this.bar = builder.bar;
this.baz = builder.baz;
...
}
public FooType getFoo() { return foo; }
public BarType getBar() { return bar; }
public BazType getBaz() { return baz; }
...
public Person cloneWith(FooType foo) {
return new Builder(this).setFoo(foo).build();
}
public Person cloneWith(BarType bar) {
return new Builder(this).setBar(bar).build();
}
public Person cloneWith(FooType foo, BarType bar) {
return new Builder(this).setFoo(foo).setBar(bar).build();
}
...
public class Builder{
private FooType foo;
private BarType bar;
private BazType baz;
...
public Builder() {
foo = DEFAULT_FOO;
bar = DEFAULT_BAR;
baz = DEFAULT_BAZ;
...
}
public Builder(Person person) {
foo = person.foo;
bar = person.bar;
baz = person.baz;
...
}
public Builder setFoo(FooType foo) {
this.foo = foo;
return this;
}
public Builder setBar(BarType bar) {
this.bar = bar;
return this;
}
public Builder setBaz(BazType baz) {
this.baz = baz;
return this;
}
...
public Person build() {
return new Person(this);
}
}
}
Depends on how many fields you intend to change. You could make special Changed objects like:
interface Person {
public String getForeName();
public String getSurName();
}
class RealPerson implements Person {
private final String foreName;
private final String surName;
public RealPerson (String foreName, String surName) {
this.foreName = foreName;
this.surName = surName;
}
#Override
public String getForeName() {
return foreName;
}
#Override
public String getSurName() {
return surName;
}
public Person setSurName (String surName) {
return new PersonWithSurnameChanged(this, surName);
}
}
class PersonWithSurnameChanged implements Person {
final Person original;
final String surName;
public PersonWithSurnameChanged (Person original, String surName) {
this.original = original;
this.surName = surName;
}
#Override
public String getForeName() {
return original.getForeName();
}
#Override
public String getSurName() {
return surName;
}
}
This may also mitigate the problem you have with cloning heavy objects.

How to apply range criteria for List of Objects?

I have one Object say
class ABC{
private String name;
private String code;
private BigDecimal priceM;
private BigDecimal priceL;
private BigDecimal priceO;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public BigDecimal getPriceM() {
return priceM;
}
public void setPriceM(BigDecimal priceM) {
this.priceM = priceM;
}
public BigDecimal getPriceL() {
return priceL;
}
public void setPriceL(BigDecimal priceL) {
this.priceL = priceL;
}
public BigDecimal getPriceO() {
return priceO;
}
public void setPriceO(BigDecimal priceO) {
this.priceO = priceO;
}
}
Now, say i have a list of ABC and at some point i want that list to filter according to searchCriteria.
Is there any efficient way to achieve this...???
Example ,
I have
AIM :
List and i want new List which will contain List of ABC with priceM values between 100 to 200
Google Guava have some classes around Matchers and Predicate maybe that can help
http://java.dzone.com/articles/google-guava-goodness-matching
Maybe you can use reflection to achieve automatic matching
The new APIs in JDK 8
List<ABC> items = asList(...);
Stream<ABC> stream = items.stream();
stream.filter( item -> item.getPrice() >= 100 && item.getPrice() <= 200 )
.forEach ( System.out::println );
Java provide a Collection.sort(list, comp) method which takes a list and Comparator object as argument
In that case pass your original list which need to be sort and create a Comparator object and override the compare method which is rules for sorting.
You can create multiple Comparator object based on your requirement.
Please refer below link for more help.
http://www.vogella.com/blog/2009/08/04/collections-sort-java/
How do I sort a list by different parameters at different timed

Are multiple data types in arrays possible in Java?

In my function, in order to accept an array of values I have to define a data type...
myFunc( String[] myVar ) {}
What if I wanted an array like the below array that mixes objects and strings and nested arrays/ (Note: the below array is in PHP)
myFunc(array(
array(
'name' => 'Category1',
'products' => array(
array(
productObject,
productObject,
...
)
)
)
));
Is this possible in Java or is this a completely wrong technique?
You would need to use an object-oriented approach in Java (which will also work in PHP). The class structure for the object you've listed above would be as follows:
public class MyObject {
String name;
Product[] products;
public MyObject(String name, Product[] products) {
this.name = name;
this.products = products;
}
}
Then you can get an instance of that class by doing the following:
new MyObject("Category1", new Product[] { productObject1, productObject2 });
You can also have an array of this object type:
MyObject[] myObjs = {
new MyObject("Category1", new Product[] { productObject1, productObject2 });
new MyObject("Category2", new Product[] { productObject3, productObject4 });
new MyObject("Category3", new Product[] { productObject5, productObject6 });
}
It's possible (an array of Object), but it's still not a good solution.
It sounds like instead of an array that holds different types, you really want to create a new class. You have names defined for the array elements in your example, so that indicates that you know what types they should be. My guess is that you want to create a class that holds a String for the name and an array of Products (another custom class).
Most PHP code is not as strongly typed as code in a more "traditional" OO language like C++, C#, Java, etc. So an approach of porting PHP directly to Java is probably off on the wrong track.
Instead, try building a class to represent your data type. Start from the innermost type, which in your case seems to be a "Product". Example:
public class Product {
private int id;
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
}
Now you need to represent what appears to be a category of products, so you can create another class to hold those. Example:
public class ProductCategory {
private String name;
private List<Product> products;
public ProductCategory(String name) {
this.name = name;
this.products = new ArrayList<Product>();
}
public String getName() {
return this.name;
}
public List<Product> getProducts() {
return this.products;
}
}
To use these classes, you might write code like this:
Product p1 = new Product(1, "P1");
Product p2 = new Product(2, "P2");
Product p3 = new Product(3, "P3");
ProductCategory c1 = new ProductCategory("C1");
// Add product to category 1
c1.getProducts().add(p1);
ProductCategory c2 = new ProductCategory("C2");
// Add products to category 2
c2.getProducts().add(p2);
c2.getProducts().add(p3);
You can create an Object array, which would happily take multiple data types. You'll have to be careful with how you cast them to use them though - using instanceof is handy, or if you know exactly what data type to expect you can cast without the need to check the data type first. It's considered bad practice to use multiple data type arrays, although there's nothing stopping you from doing it if your implementation requires it.
It's possible with an array of Object, which is the base type in Java, but for your use case a custom class is more javaish.
class Category {
String name;
List<Product> products;
}
With that approach you have a type save way to define your data.

Categories

Resources