list of dots Digital Research Alliance of Canada logo  NSERC logo  University of Ottawa logo / UniversitĂ© d'Ottawa

User Manual    [Previous]   [Next]   

Keys for Equality and Hashing

In any class you can specify a set of attributes and associations as keys. The associations must have a multiplicity of 1 at the other end. This allows Umple to generate code that defines what objects of the class are equal (they have the same values for the key).

Umple will also generate a hashCode method in any class that has a defined key. This helps when looking up an object in a set.

Place the comma-separated list of key elements in curly brackets, after the word key. If you have multiple key statements, a warning is generated.

Example

class Sport {
  name;
  description;
  code;
  key { code }
}

class League {
  name;
  id;
  geographicalArea;
  * -- 1 Sport;
  Date seasonStart;
  Date seasonEnd;
  key { id }
}

class Team {
  name;
  * -- 1 League;
}

class Player {
  name;
  Integer id;
  key { id }
 }

class PlayerOnTeam {
  Integer year;
  * -- 1 Player;
  * -- 1 Team;
  key { year, player, team }
}

      

Load the above code into UmpleOnline

 

Syntax


softwarePattern- : [[isA]] | [[singleton]] | [[immutable]] | [[keyDefinition]] | [[codeInjection]]

// For equality and hashing KeysforEqualityandHashing
keyDefinition- : [[defaultKey]] | [[key]]

// Keys are used to define quality and hash codes, plus Sql keys.
// See user manual page KeysforEqualityandHashing
defaultKey : key { }

key : key { [keyId] ( , [keyId] )* }