Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a series of classes that represent entities. As I'm trying to follow functional principles, they're methodless and their properties aren't meant to be changed once set.

Implementing equals would be needed, in a straightforward way - two instances are considered equal if so are their properties, with no extra logic.

I'm inexperienced in implementing hashCode, so I'm not sure if it could be programatically implemented.

Coding such classes seems unnecessarily verbose to me - looks like has to be a lightweight alternative to

public class SomeEntity {
    public final int prop0;
    public final long prop1;
    // ...
    public final String propN;

    @Override
    public boolean equals(Object arg){
        // check check that arg instanceof Entity,
        // and that each of their properties equal, one by one.
    }
}

. Are there any methods, annotations, collections or even compilers that can do this for me?

share|improve this question
Glad to see someone else playing around with (OO+F)P. Why do you need methodless objects though? There's nothing not-functional about methods (that don't alter an object's own state) - String.length() makes perfect sense from an FP perspective. – Frank Shearar Dec 21 '11 at 16:20
At least in the Clojure world it is often said that mixing data structures and behavior breaks separation of concerns. So I have in one package all my POJOs and in another one, static methods that operate on them (ideally, in a generic manner). – vemv Dec 21 '11 at 18:53
Unfortuantely the static external methods can cause additional objects to be created, that you might not need to otherwise. For instance, when creating an object that contains a List, you need to copy it (to prevent outside changes). For a method that changes some other attribute than that list - 1) If it's an internal method, the existing list copy can be simply re-used when the new object is created. 2) If it's an external method, it'll need to re-copy the list (or use an exposed non-copying constructor). – Clockwork-Muse Dec 21 '11 at 19:47

4 Answers

Many IDEs, for example NetBeans, can automatically generate hashCode based on equals, or vice versa.

Java 7 has Objects.deepEquals and Objects.hash utilities for generating equals and hash codes for an arbitrary bunch of fields. As stated in its doc, it's based on Arrays.hashCode, which is available in Java 6. You just need to wrap the fields in a new Object[]{field1, field2, field3...}.

Given that your object is immutable, it may make sense to precompute the hash code and store it in one more member. Or, compute lazily, as it's done in java.lang.String:

private int hash;
public int hashCode() {
    int h = hash;
    if (h == 0) {
       // ... compute hash code into h here ...
       hash = h;
    }
    return h;
}

The purpose of using a local variable h is thread safety. Curiously, it's done wrong in java.util.UUID.

share|improve this answer
1  
Eclipse has it in the Source menu too. – Raku Dec 21 '11 at 9:31

it probably took longer to write the question than it will the actual code.

Java's Guava and its equal and hashcode methods could be useful, it has lots of other goodies too.

share|improve this answer

See Project Lombok, especially @EqualsAndHashCode. Easy to use, and rids your classes from unnecessary boilerplate code.

share|improve this answer
its not all good though stackoverflow.com/questions/3852091/… – NimChimpsky Dec 21 '11 at 9:40
@NimChimpsky not being able to generate JavaDoc for get/setters seem a quite minor issue to me. Will try it, I just hope the IntelliJ integration works fine. – vemv Dec 21 '11 at 19:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.