How to create own Custom Predicate to compare composite id in hazelcast - java

I want to create my own custome predicate to compare composite id's inside object. The need is to because i have to write specific date comparison logic on object inside object (composite id). I don't want to compare individual attributes.i want to use composite id because it comes from invoker and I can't get result using Predicate.in and Predicate.equals
My Object structure is like below
Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IST 1902, description='MNP'}
and inside IMap it is stored like below
key : BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}
value : Birth{id=BirthId{name='b3', dob=Wed Jan 01 10:53:20 IST 1902}, name='b3', dob=Wed Jan 01 10:53:20 IST 1902, description='MNP'}
My Java Classes(Birth and Birthid) Structure is below
public class Birth implements Serializable, Portable {
private static final long serialVersionUID = 1L;
private BirthId id;
private String name;
private Date dob;
private String description;
public BirthId getId() {
return id;
}
public void setId(BirthId id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public int hashCode() {
return (new HashCodeBuilder()).append(this.id).append(this.name).append(this.dob).toHashCode();
}
public boolean equals(Object other) {
if (other == this) {
return true;
} else if (!(other instanceof Birth)) {
return false;
} else {
Birth rhs = (Birth) other;
return (new EqualsBuilder()).append(this.id, rhs.id).append(this.name, rhs.name).append(this.dob, rhs.dob).isEquals();
}
}
#Override public String toString() {
return "Birth{" + "id=" + id + ", name='" + name + '\'' + ", dob=" + dob + ", description='" + description + '\'' + '}';
}
public int getFactoryId() {
return 1;
}
public int getClassId() {
return 1;
}
public void writePortable(PortableWriter portableWriter) throws IOException {
portableWriter.writePortable("idComposite", getId());
portableWriter.writeUTF("id", getId().toString());
portableWriter.writeUTF("name", getName());
portableWriter.writeUTF("description", getDescription());
Date date = getDob();
portableWriter.writeLong("dob", ((date == null) ? -1 : date.getTime()));
}
public void readPortable(PortableReader portableReader) throws IOException {
setId((BirthId) portableReader.readPortable("idComposite"));
setName(portableReader.readUTF("name"));
setDescription(portableReader.readUTF("description"));
long date = portableReader.readLong("dob");
setDob(((date == -1) ? null : new Date(date)));
}
}
public class BirthId implements Comparable<BirthId>, Serializable, Portable {
private static final long serialVersionUID = 1L;
private String name;
private Date dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public int hashCode() {
return (new HashCodeBuilder()).append(this.name).append(this.dob).toHashCode();
}
public boolean equals(Object other) {
if (other == this) {
return true;
} else if (!(other instanceof BirthId)) {
return false;
} else {
BirthId rhs = (BirthId) other;
return (new EqualsBuilder()).append(this.name, rhs.name).append(this.dob, rhs.dob).isEquals();
}
}
public int compareTo(BirthId rhs) {
return this == rhs ? 0 : (null == rhs ? -1 : (new CompareToBuilder()).append(this.name, rhs.name).append(this.dob, rhs.dob).toComparison());
}
#Override public String toString() {
return "BirthId{" + "name='" + name + '\'' + ", dob=" + dob + '}';
}
public int getFactoryId() {
return 1;
}
public int getClassId() {
return 2;
}
public void writePortable(PortableWriter portableWriter) throws IOException {
portableWriter.writeUTF("name", getName());
Date date = getDob();
portableWriter.writeLong("dob", ((date == null) ? -1 : date.getTime()));
}
public void readPortable(PortableReader portableReader) throws IOException {
setName(portableReader.readUTF("name"));
long date = portableReader.readLong("dob");
setDob(((date == -1) ? null : new Date(date)));
}
public static ClassDefinition getClassDefinition(int portableVersion) {
ClassDefinitionBuilder result = new ClassDefinitionBuilder(1, 2, portableVersion);
result.addUTFField("name");
result.addLongField("dob");
return result.build();
}
}
I have created own custom Predicate to compare dates like below
public class DatePredicate extends AbstractPredicate<Comparable, BirthId> {
Comparable[] values;
private volatile Set<Comparable> convertedInValues;
public DatePredicate() {
}
public DatePredicate(String attribute, Comparable... values) {
if (values == null) {
throw new NullPointerException("Array can't be null");
} else {
this.values = values;
}
}
protected boolean applyForSingleAttributeValue(Map.Entry entry, Comparable attributeValue) {
//My own date comparison logic
return true;
}
public int getId() {
return 99;
}
}
Caller code is
Predicate p = new DatePredicate("id", new BirthId("12345",passSpecifiedDate()));
Result res = imap.values(p);
I am getting below error
Exception in thread "main" com.hazelcast.nio.serialization.HazelcastSerializationException: com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory#5f007be3 is not be able to create an instance for id: 99 on factoryId: -32
I do not know the best way to create own custom predicate and hazelcast doc does not specify the also.
Could any please guide me how to do this?

#oomph-fortuity, your DatePredicate extends AbstractPredicate which implements IdentifiedDataSerializable and used by built-in Hazelcast predicates. Built-in Predicate Serializable Factory try to deserialize your class & fails since it only knows how to serialize/deserialize built-in Predicates.
Instead, just implement com.hazelcast.query.Predicate interface:
class DatePredicate implements Predicate<BirthId, Birth> {
BirthId birthIdToCompare;
public DatePredicate() {
}
public DatePredicate(BirthId birthId) {
this.birthIdToCompare = birthId;
}
#Override
public boolean apply(Map.Entry<BirthId, Birth> mapEntry) {
BirthId key = mapEntry.getKey();
///your custom logic
return true;
}
}
And call like this
Predicate p = new DatePredicate(new BirthId("12345",passSpecifiedDate()));
Result res = imap.values(p);
Let me know if that works.

Related

Java Object in list doesn't get found

I have a problem. I have the following class:
public class Cross implements Comparable<Cross> {
private Long openTime;
private String market;
private String coin;
private String period;
private String metric1;
private String metric2;
private Double close;
private String trend;
public Long getOpenTime() {
return this.openTime;
}
public void setOpenTime(long openTime) {
this.openTime = openTime;
}
public String getMarket() {
return this.market;
}
public void setMarket(String market) {
this.market = market;
}
public String getCoin() {
return this.coin;
}
public void setCoin(String coin) {
this.coin = coin;
}
public String getPeriod() {
return this.period;
}
public void setPeriod(String period) {
this.period = period;
}
public String getMetric1() {
return this.metric1;
}
public void setMetric1(String metric1) {
this.metric1 = metric1;
}
public String getMetric2() {
return this.metric2;
}
public void setMetric2(String metric2) {
this.metric2 = metric2;
}
public Double getClose() {
return this.close;
}
public void setClose(double close) {
this.close = close;
}
public String getTrend() {
return this.trend;
}
#Override
public boolean equals(Object object) {
if (object != null && object instanceof Cross) {
Cross cross = (Cross) object;
return (
openTime.equals(cross.getOpenTime()) &&
market.equals(cross.getMarket()) &&
coin.equals(cross.getCoin()) &&
period.equals(cross.getPeriod()) &&
metric1.equals(cross.getMetric1()) &&
metric2.equals(cross.getMetric2())
);
}
return false;
}
#Override
public int compareTo(Cross o) {
return this.getOpenTime().compareTo(o.getOpenTime());
}
}
Now I have a list containing 500 objects of these elements. Here are the first 4 shown:
{ openTime='1504332000000', market='USDT', coin='ETH', period='2h', metric1='EMA12', metric2='EMA26', close='363.7', trend='Down'}
{ openTime='1504663200000', market='USDT', coin='ETH', period='2h', metric1='EMA12', metric2='EMA26', close='325.73', trend='Up'}
{ openTime='1504879200000', market='USDT', coin='ETH', period='2h', metric1='EMA12', metric2='EMA26', close='294.05', trend='Down'}
{ openTime='1505181600000', market='USDT', coin='ETH', period='2h', metric1='EMA12', metric2='EMA26', close='304.41', trend='Up'}
In a variable I have stored the epoch of a specific datetime and with that I want to find the first valid cross, so I tried this:
private Cross getValidCross(List<Cross> crossList, LocalDateTime runDateTimeGMT0) {
long searchEpoch = runDateTimeGMT0.toEpochSecond(ZoneOffset.UTC) * 1000;
return crossList.stream().filter(cross -> cross.getOpenTime() < searchEpoch).max(Cross::compareTo).orElse(null);
}
But this code returns the value null When my searchEpoch is for example: 1514764800000. In the list I do see multiple objects that have a lower openTime than the searchEpoch. The result I want is the cross with the highest openTime, but it still has to be smaller than the searchEpoch.
Here is the error I get:
runDateTimeGMT0: 2018-01-01T00:00
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.hatop.drivers.SimulatorDriver.run(SimulatorDriver.java:297)
at com.hatop.drivers.HatopDriver.main(HatopDriver.java:120)
Caused by: java.lang.NullPointerException: Cannot invoke "com.hatop.models.Cross.getTrend()" because the return value of "com.hatop.drivers.HatopDriver.getValidCross(List<Cross>, LocalDateTime)"
is null
at com.hatop.strategies.modules.module_java001.run(module_java001.java:186)
... 6 more
Why is my return value null?
This is caused due to the value the argument runDateTimeGMT0 is given and this:
long searchEpoch = runDateTimeGMT0.toEpochSecond(ZoneOffset.UTC) * 1000;
Since this is working:
public class Demo {
private static Cross getValidCross(List<Cross> crossList, LocalDateTime runDateTimeGMT0) {
long searchEpoch = 1514764800000L;
return crossList.stream()
.filter(cross -> cross.getOpenTime() < searchEpoch)
.max(Cross::compareTo)
.orElse(null);
}
#SneakyThrows
public static void main(String[] args) {
List<Cross> crosses = Arrays.asList(
new Cross(1504332000000L, "USDT", "ETH", "2h", "EMA12", "EMA26", 363.7, "Down"),
new Cross(1504663200000L, "USDT", "ETH", "2h", "EMA12", "EMA26", 325.73, "Up"),
new Cross(1504879200000L, "USDT", "ETH", "2h", "EMA12", "EMA26", 294.05, "Down"),
new Cross(1505181600000L, "USDT", "ETH", "2h", "EMA12", "EMA26", 304.41, "Up")
);
Cross validCross = getValidCross(crosses, null);
System.out.println(validCross);
}
}
Output:
Cross(openTime=1505181600000, market=USDT, coin=ETH, period=2h, metric1=EMA12, metric2=EMA26, close=304.41, trend=Up)
By the way, equals() is implemented without hashcode() - a recipe for problems.
I tested you code by passing the value in a proper Cross constructor, as it appears in your list and it works fine. In particular, openingTime is passed as primitive type long.
The issue is NOT in the filter, as you can definitely use any operator with Long type.
In my opinion, there is something wrong in the file format of your Cross object. Highly likely is because of the openingTime field's value, which is probably converted in the wrong value.
Here is the test I wrote:
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
public class TestCross {
public static void main(String[] args) {
List<Cross> crossList = List.of(new Cross(1504332000000L,"USDT", "ETH", "2h","EMA12", "EMA26",363.7,"Down" ),
new Cross(1505181600000L,"USDT", "ETH", "2h","EMA12", "EMA26",304.41,"Up" ),
new Cross(1504663200000L,"USDT", "ETH", "2h","EMA12", "EMA26",325.73,"Up" ),
new Cross(1504879200000L,"USDT", "ETH", "2h","EMA12", "EMA26",294.05,"Down" ));
Cross cross = new TestCross().getValidCross(crossList, LocalDateTime.now());
System.out.println(cross);
}
private Cross getValidCross(List<Cross> crossList, LocalDateTime runDateTimeGMT0) {
long searchEpoch = runDateTimeGMT0.toEpochSecond(ZoneOffset.UTC) * 1000;
return crossList.stream().filter(cross -> cross.getOpenTime() < searchEpoch).max(Cross::compareTo).orElse(null);
}
}
Cross class
public class Cross implements Comparable<Cross> {
public Cross(Long openTime, String market, String coin, String period, String metric1, String metric2, Double close,
String trend) {
super();
this.openTime = openTime;
this.market = market;
this.coin = coin;
this.period = period;
this.metric1 = metric1;
this.metric2 = metric2;
this.close = close;
this.trend = trend;
}
private Long openTime;
private String market;
private String coin;
private String period;
private String metric1;
private String metric2;
private Double close;
private String trend;
public Long getOpenTime() {
return this.openTime;
}
public void setOpenTime(long openTime) {
this.openTime = openTime;
}
public String getMarket() {
return this.market;
}
public void setMarket(String market) {
this.market = market;
}
public String getCoin() {
return this.coin;
}
public void setCoin(String coin) {
this.coin = coin;
}
public String getPeriod() {
return this.period;
}
public void setPeriod(String period) {
this.period = period;
}
public String getMetric1() {
return this.metric1;
}
public void setMetric1(String metric1) {
this.metric1 = metric1;
}
public String getMetric2() {
return this.metric2;
}
public void setMetric2(String metric2) {
this.metric2 = metric2;
}
public Double getClose() {
return this.close;
}
public void setClose(double close) {
this.close = close;
}
public String getTrend() {
return this.trend;
}
#Override
public boolean equals(Object object) {
if (object != null && object instanceof Cross) {
Cross cross = (Cross) object;
return (
openTime.equals(cross.getOpenTime()) &&
market.equals(cross.getMarket()) &&
coin.equals(cross.getCoin()) &&
period.equals(cross.getPeriod()) &&
metric1.equals(cross.getMetric1()) &&
metric2.equals(cross.getMetric2())
);
}
return false;
}
#Override
public String toString() {
return "Cross [openTime=" + openTime + ", market=" + market + ", coin=" + coin + ", period=" + period
+ ", metric1=" + metric1 + ", metric2=" + metric2 + ", close=" + close + ", trend=" + trend + "]";
}
#Override
public int compareTo(Cross o) {
return this.getOpenTime().compareTo(o.getOpenTime());
}
}
And here is the output:
Cross [openTime=1505181600000, market=USDT, coin=ETH, period=2h, metric1=EMA12, metric2=EMA26, close=304.41, trend=Up]

Problem java Set data replaced/wiped on add

I have actually a problem on a Set in my project (code below)
public static Set<BeanObject> mapToSetBean(Collection<Object> listModel) {
Set<BeanObject> listToReturn = new HashSet<>();
for (Iterator<Object> iterator = listModel.iterator(); iterator.hasNext();) {
Object model = iterator.next();
BeanObject bean = new BeanObject();
bean = mapToBean(model);
listToReturn.add(bean);
}
return listToReturn;
}
When some beans are added to the list they replace another one.
For example:
List{}
add object1 / List{object1}
add object2 / List{object1, object2}
add object1 / List{object1, object2, object3}
add object4 / List{object4, object2, object3}
The equals and the hashcode of the object are override the hashcode are all different and in debug mode we don't enter in the override equals.
When I use an ArrayList everything works but I prefer not to change the type it has a huge impact on my project.
---------------- EDIT ---------------
public static BeanObject mapToBean(Object model) {
BeanObject bean = new BeanObject();
if (model != null) {
bean.setId(model.getId());
if(model.getId() != null){
bean.setIdString(model.getId().toString());
}
if (model.getName() != null) {
bean.setName(model.getName().toLowerCase());
}
bean.setActif(model.getActif());
if (model.getShortName() != null) {
bean.setShortName(model.getShortName().toUpperCase());
}
}
return bean;
}
BeanObject
public class BeanObject implements Comparable<BeanObject> {
/**
* serial
*/
private static final long serialVersionUID = 1L;
private BigInteger id;
private String name;
private String shortName;
private Short actif;
private String idString;
public BeanObject() {
}
public BeanObject(BigInteger id, String libelle) {
this.id = id;
this.name = libelle;
}
/*
* (non-Javadoc)
*
* #see java.lang.Object#equals(java.lang.Object)
*/
#Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof BeanObject)) {
return false;
}
BeanObject other = (BeanObject) o;
boolean result;
if (null == this.id) {
if (null == other.id) {
result = true;
} else {
result = false;
}
} else {
result = this.id.equals(other.id);
}
return result;
}
/*
* (non-Javadoc)
*
* #see java.lang.Object#hashCode()
*/
#Override
public int hashCode() {
return this.id.intValue() * name.hashCode() * shortName.hashCode();
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public Short getActif() {
return actif;
}
public void setActif(Short actif) {
this.actif = actif;
}
public String getIdString() {
return idString;
}
public void setIdString(String idString) {
this.idString = idString;
}
}
Are you certain the BeanObject's hashcode values are unique? That hashcode method seems like it would map a lot of objects to 0 if any of its fields hashed to 0 since it is straight multiplication. I would suggest updating to a more standardized approach for it like the following:
#Override
public int hashCode() {
return Objects.hash(id, name, shortName);
}
Objects.hash is from the java.util package.
If this still doesn't solve the problem, I would double check the hashcode results for each bean object at add time.

Lombok Maven different of Lombok in Eclipse or STS

I have a delta between compile by Maven and compile by Eclipse. By Maven is OK but not by Eclipse.
In compile by Eclipse, it misses the #ConstructorProperties({ "id", "profile" }) annotation on the constructor that has both parameters.
My java file:
#Data
#AllArgsConstructor
public class Author {
private String id;
private String profile;
}
Full class by Maven (OK):
import java.beans.ConstructorProperties;
public class Author {
private String id;
private String profile;
public void setId(String id) {
this.id = id;
}
public void setProfile(String profile) {
this.profile = profile;
}
#Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Author)) {
return false;
}
final Author other = (Author) o;
if (!other.canEqual(this)) {
return false;
}
final Object this$id = getId();
final Object other$id = other.getId();
if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
return false;
}
final Object this$profile = getProfile();
final Object other$profile = other.getProfile();
return this$profile == null ? other$profile == null : this$profile.equals(other$profile);
}
protected boolean canEqual(Object other) {
return other instanceof Author;
}
#Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $id = getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
final Object $profile = getProfile();
result = result * 59 + ($profile == null ? 43 : $profile.hashCode());
return result;
}
#Override
public String toString() {
return "Author(id=" + getId() + ", profile=" + getProfile() + ")";
}
#ConstructorProperties({ "id", "profile" })
public Author(String id, String profile) {
this.id = id;
this.profile = profile;
}
public String getId() {
return id;
}
public String getProfile() {
return profile;
}
}
Full class by Eclipse:
public class Author {
private String id;
private String profile;
public String getId() {
return id;
}
public String getProfile() {
return profile;
}
public void setId(String id) {
this.id = id;
}
public void setProfile(String profile) {
this.profile = profile;
}
#Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Author)) {
return false;
}
final Author other = (Author) o;
if (!other.canEqual(this)) {
return false;
}
final Object this$id = getId();
final Object other$id = other.getId();
if (this$id == null ? other$id != null : !this$id.equals(other$id)) {
return false;
}
final Object this$profile = getProfile();
final Object other$profile = other.getProfile();
return this$profile == null ? other$profile == null : this$profile.equals(other$profile);
}
protected boolean canEqual(Object other) {
return other instanceof Author;
}
#Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $id = getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
final Object $profile = getProfile();
result = result * 59 + ($profile == null ? 43 : $profile.hashCode());
return result;
}
#Override
public String toString() {
return "Author(id=" + getId() + ", profile=" + getProfile() + ")";
}
public Author(String id, String profile) {
this.id = id;
this.profile = profile;
}
}
#tobias_k find solution:
Eclipse need same version of Lombok installed that Maven project use.

cqengine cant index by equals

I'm trying to add an index where my override equals() determines if two objects are the same or not.
Car.java
public static class Car {
final String id;
private String name;
public Car(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static final Attribute<Car, Car> CAR = new SimpleAttribute<Car, Car>() {
#Override
public Car getValue(Car car, QueryOptions queryOptions) {
return car;
}
};
#Override
public String toString() {
return "Car{" + "id=" + id + ", name=" + name + '}';
}
}
Fetcher.java
public static final ResultSet<Car> get(final IndexedCollection<Car> indexedCollection, final Car car) {
return indexedCollection.retrieve(QueryFactory.equal(Car.CAR, car));
}
Main.java
public static void main(String args[]) {
IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
cars.addIndex(NavigableIndex.onAttribute(Car.CAR));
}
The problem is on this line cars.addIndex(NavigableIndex.onAttribute(Car.CAR)); where the error message is no suitable method found for onAttribute(Attribute<Car,Car>). Am I doing something wrong here or is there another call I should be using instead?
Remove cars.addIndex(NavigableIndex.onAttribute(Car.CAR));, because it is not really an usefull index... and I think this was not a motivation of the developer. You should create Attributes for CAR_ID and CAR_NAME and create an Query for comparison. In this case I misuse (to achieve what you expect) IndexedCollection as a simple Set. But... here is a possible solution, if I have understood you correctly:
Override equals in Car:
class Car {
private final int id;
private String name;
public Car(int i, String name) {
this.id = i;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if (!(obj instanceof Car)) return false;
Car car = (Car) obj;
if(car.getId() == this.getId())
if(car.getName().equals(this.getName()))
return true;
return false;
}
public static final Attribute<Car, Car> CAR = new SimpleAttribute<Car, Car>() {
#Override
public Car getValue(Car car, QueryOptions queryOptions) {
return car;
}
};
#Override
public String toString() {
return "Car{" + "id=" + id + ", name=" + name + '}';
}
}
Main:
IndexedCollection<Car> cars = new ConcurrentIndexedCollection<>();
cars.add(new Car(1, "test"));
cars.add(new Car(2, "test2"));
cars.add(new Car(3, "test3"));
Car s = new Car(2, "test2");
ResultSet<Car> cs= cars.retrieve(QueryFactory.equal(Car.CAR, s));
cs.forEach(c -> System.out.println(c.toString()));

Creating immutable view of object using Proxy classes

I'm newest at using Proxy classes. I need to create fabric method of immutable view for my object (MusicalInstrument.class)
View must throw an Exception when i'm trying to invoke setter and invoking of other methods must transfer to my object.
Maybe you have got some examples or sources where i can find answers! Thanx!
public class MusicalInstrument implements Serializable {
/**
* ID of instrument.
*/
private int idInstrument;
/**
* Price of instrument.
*/
private double price;
/**
* Name of instrument.
*/
private String name;
public MusicalInstrument() {
}
public MusicalInstrument(int idInstrument, double price, String name) {
this.idInstrument = idInstrument;
this.price = price;
this.name = name;
}
public int getIdInstrument() {
return idInstrument;
}
public void setIdInstrument(int idInstrument) {
this.idInstrument = idInstrument;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MusicalInstrument that = (MusicalInstrument) o;
if (idInstrument != that.idInstrument) return false;
if (Double.compare(that.price, price) != 0) return false;
return name != null ? name.equals(that.name) : that.name == null;
}
#Override
public int hashCode() {
int result;
long temp;
result = idInstrument;
temp = Double.doubleToLongBits(price);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
#Override
public String toString() {
return "MusicalInstrument{" +
"idInstrument=" + idInstrument +
", price=" + price +
", name='" + name + '\'' +
'}';
}
You can use ImmutableProxy of the reflection-util library.
Example:
MusicalInstrument instrument = new MusicalInstrument(1, 12.5, "Guitar");
MusicalInstrument immutableView = ImmutableProxy.create(instrument);
assertThat(immutableView.getName()).isEqualTo("Guitar");
// throws UnsupportedOperationException
immutableView.setName(…);

Categories

Resources