[rules-users] Problems with looping

Greg Barton greg_barton at yahoo.com
Fri Aug 1 12:03:55 EDT 2008


Did you implement equals() and hashCode()?

Implementing them isn't hard.  Implementing them well
can be tricky, but for these purposes a simple
implementation will do.  Basically, with equals() you
want to compare the stuff contained in the objects. 
With hashCode() you want to produce an integer that
can be used in HashMaps.  The only restriction is
that, if o1.equals(o2) == true, then o1.hashCode() ==
o2.hashCode().  (The reverse is not necessarily true.)

The easiest way to implement these is to just compare
the fields that define the state of your object:

public class Foo {

  int bar;
  String bas;

  public boolean equals(Object o) {
    if(o instanceof Foo) {
      Foo other = (Foo)o;
      return bar == other.bar && bas != null &&
bas.equals(other.bas);
    } else {
      return false;
    }
  }

  public int hashCode() {
    int hash = 37;
    hash ^= bar;
    if(bas != null) {
      hash ^= bas.hashCode();
    }
    return hash;
  }
}

One good way to make this easier is to use the jakarta
commons EqualsBuilder and HashCodeBuilder classes. 
They also help you implement them "well," especially
hashCode.

GreG

--- thomas kukofka <thomaskukofka at web.de> wrote:

> Hi, I used no-loop true but it still loops!!
> 
> Thomas
> 
> Hi
> 
> you must use "no-loop true" and the rule will be
> executed one for one
> InputObject.
> 
> 
> 2008/8/1 thomas kukofka <thomaskukofka at
>
web.de<https://lists.jboss.org/mailman/listinfo/rules-users>
> >:
> 
> > Hello,
> 
> >
> 
> > I have and less loop (the rule should only
> executed one for one
> InputObject)
> 
> > in the follwing rule although I use "no-loop":
> 
> >
> 
> > rule "rulename"
> 
> >   dialect "java"
> 
> >   no-loop
> 
> >     when
> 
> >         io: InputObject (type ==
> InputObject.Type.Typename)
> 
> >     then
> 
> >        
> io.setStringPropertyValue(InputObject.PROPERTYVALUE,
> "somestring");
> 
> >         update(oo);
> 
> >
> 
> > end
> 
> >
> 
> > I've read that I have to overwrite hashcode and
> equals, can this be the
> 
> > reason? If yes, do you have an example how to
> overwrite these methods?
> 
> >
> 
> > Thomas
> > _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
> 



      



More information about the rules-users mailing list