equals

Correctly synchronizing equals() in Java

大憨熊 提交于 2020-01-02 01:18:06
问题 I have the following class which contains only one field i . Access to this field is guarded by the lock of the object ("this"). When implementing equals() I need to lock this instance (a) and the other (b). If thread 1 calls a.equals(b) and at the same time thread 2 calls b.equals(a), the locking order is reverse in the two implementations and may result in deadlock. How should I implement equals() for a class which has synchronized fields? public class Sync { // @GuardedBy("this") private

C# how to calculate hashcode from an object reference

半城伤御伤魂 提交于 2020-01-02 00:44:43
问题 Folks, here's a thorny problem for you! A part of the TickZoom system must collect instances of every type of object into a Dictionary<> type. It is imperative that their equality and hash code be based on the instance of the object which means reference equality instead of value equality. The challenge is that some of the objects in the system have overridden Equals() and GetHashCode() for use as value equality and their internal values will change over time. That means that their Equals and

Override equals on a cglib proxy

本秂侑毒 提交于 2020-01-01 19:35:16
问题 I would like to use a CGLIB proxy to add my own reusable equals() method to existing objects. The objects do not necessarily implement any interfaces and I need to be able to cast the proxied object to the original class (without getting the target of the proxy). Unfortunately, it seems that CGLIB implements its own equals() method and makes sure that only that method is called: there is a private static class (EqualsInterceptor) whose method intercept() implements a reasonable logic to

Can Java help me avoid boilerplate code in equals()?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 08:57:08
问题 I implement equals() the Java 7 way: @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MyClass other = (MyClass) obj; return Objects.equal(myFirstField, other.myFirstField) && Objects.equal(mySecondField, other.mySecondField); } Is there a way to reduce the code duplication? I would prefer something like @Override public boolean equals(Object obj) { if (Objects.equalsEarlyExit(this, obj))

Java - is there a “subclassof” like instanceof?

做~自己de王妃 提交于 2020-01-01 07:52:55
问题 Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! 回答1: instanceof can handle that just fine. 回答2: With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself. if(myObject instanceof Event && myObject.getClass() != Event.class) { // then I'm an instance of a subclass

Java - is there a “subclassof” like instanceof?

若如初见. 提交于 2020-01-01 07:52:08
问题 Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! 回答1: instanceof can handle that just fine. 回答2: With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself. if(myObject instanceof Event && myObject.getClass() != Event.class) { // then I'm an instance of a subclass

Is it proper for equals() to depend only on an ID?

て烟熏妆下的殇ゞ 提交于 2020-01-01 01:12:13
问题 Let's suppose I have class User : public class User { private Long id; private String name; private Integer age; private BigDecimal account; // other fields, getters and setters } Is it proper to override the equals method as follows? @Override public boolean equals(Object ob) { if (ob == null) { return false; } if (this == ob) { return true; } if (ob instanceof User) { User other = (User) ob; return this.id.equals(other.getId()); } return false; } It turns out that the uniqueness of an

Integer value comparison

帅比萌擦擦* 提交于 2019-12-31 11:37:29
问题 I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table . My goal is to figure out how to see if the value in integer count > 0 . I realize the count.compare(0) is the correct way? or is it count.equals(0) ? I know the count == 0 is incorrect. Is this right? Is there a value comparison

Integer value comparison

ε祈祈猫儿з 提交于 2019-12-31 11:37:22
问题 I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table . My goal is to figure out how to see if the value in integer count > 0 . I realize the count.compare(0) is the correct way? or is it count.equals(0) ? I know the count == 0 is incorrect. Is this right? Is there a value comparison

Why does my boolean test in java always fail?

戏子无情 提交于 2019-12-31 03:39:04
问题 I am trying to make a boolean test so that if one of the tire pressures is below 35 or over 45 the system outputs "bad inflation". In my class I must use a boolean, which is what I tried. However the boolean returned is always true. I don't understand why. public class tirePressure { private static double getDoubleSystem1 () //Private routine to simply read a double in from the command line { String myInput1 = null; //Store the string that is read form the command line double numInput1 = 0; /