Skip to content

Commit

Permalink
Merge pull request #799 from moosetechnology/can-generate-equality-check
Browse files Browse the repository at this point in the history
Generate Equality Check for entities
  • Loading branch information
ClotildeToullec authored Jul 1, 2024
2 parents 17e4a3a + b39d6a4 commit d0830d6
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 6 deletions.
74 changes: 72 additions & 2 deletions src/Famix-MetamodelBuilder-Core/FmxMBClass.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Class {
#superclass : #FmxMBBehavior,
#instVars : [
'classGeneralization',
'isAbstractClass'
'isAbstractClass',
'propertiesForEqualityCheck'
],
#category : #'Famix-MetamodelBuilder-Core-Implementation'
}
Expand Down Expand Up @@ -80,7 +81,70 @@ FmxMBClass >> generate [
self generatePrecedenceInTraitComposition: aClass.

self generateNavigationGroupsFor: aClass.
self generateAddToCollectionFor: aClass
self generateAddToCollectionFor: aClass.

self generateEqualsMethodFor: aClass.
self generateHashMethodFor: aClass.
]

{ #category : #'generating-methods' }
FmxMBClass >> generateEqualsMethodFor: aClass [

propertiesForEqualityCheck ifNotNil: [
| variableName |
variableName := 'a' , aClass name.
self builder environment
compile: (String streamContents: [ :stream |
stream << '= '.
stream << variableName.
stream << '
<generated>
^ '.
stream
<< variableName;
<< ' ';
<< propertiesForEqualityCheck first;
<< ' = self ';
<< propertiesForEqualityCheck first.
propertiesForEqualityCheck size > 1 ifTrue: [
2 to: propertiesForEqualityCheck size do: [ :idx |
stream << ' and: [ '.
stream
<< variableName;
<< ' ';
<< (propertiesForEqualityCheck at: idx);
<< ' = self ';
<< (propertiesForEqualityCheck at: idx) ].
2 to: propertiesForEqualityCheck size do: [ :idx |
stream << '] ' ] ] ])
in: aClass instanceSide
classified: #comparing ]
]

{ #category : #'generating-methods' }
FmxMBClass >> generateHashMethodFor: aClass [

propertiesForEqualityCheck ifNotNil: [
self builder environment
compile: (String streamContents: [ :stream |
stream << 'hash'.
stream << '
<generated>
^ '.
stream
<< 'self ';
<< propertiesForEqualityCheck first;
<< ' hash '.
propertiesForEqualityCheck size > 1 ifTrue: [
2 to: propertiesForEqualityCheck size do: [ :idx |
stream
<< 'bitXor: self ';
<< (propertiesForEqualityCheck at: idx);
<< ' hash ' ] ] ])
in: aClass instanceSide
classified: #comparing ]
]

{ #category : #generating }
Expand Down Expand Up @@ -183,3 +247,9 @@ FmxMBClass >> traitsFromRelations [

^ (self relations collect: [ :each | each side trait ] thenSelect: #isNotNil) asSet
]

{ #category : #generalization }
FmxMBClass >> withEqualityCheckOn: aCollectionOfPropertySymbol [

propertiesForEqualityCheck := aCollectionOfPropertySymbol
]
35 changes: 35 additions & 0 deletions src/Famix-Test4-Entities/FamixTest4Book.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
| `person` | `FamixTest4Book` | `books` | `FamixTest4Person` | |
## Properties
======================
| Name | Type | Default value | Comment |
|---|
| `id` | `Number` | nil | |
"
Class {
#name : #FamixTest4Book,
#superclass : #FamixTest4Entity,
#instVars : [
'#id => FMProperty',
'#person => FMOne type: #FamixTest4Person opposite: #books'
],
#category : #'Famix-Test4-Entities-Entities'
Expand All @@ -28,6 +35,34 @@ FamixTest4Book class >> annotation [
^ self
]

{ #category : #comparing }
FamixTest4Book >> = aFamixTest4Book [

<generated>
^ aFamixTest4Book id = self id
]

{ #category : #comparing }
FamixTest4Book >> hash [

<generated>
^ self id hash
]

{ #category : #accessing }
FamixTest4Book >> id [

<FMProperty: #id type: #Number>
<generated>
^ id
]

{ #category : #accessing }
FamixTest4Book >> id: anObject [
<generated>
id := anObject
]

{ #category : #accessing }
FamixTest4Book >> person [
"Relation named: #person type: #FamixTest4Person opposite: #books"
Expand Down
67 changes: 67 additions & 0 deletions src/Famix-Test4-Entities/FamixTest4Person.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
| `books` | `FamixTest4Person` | `person` | `FamixTest4Book` | |
## Properties
======================
| Name | Type | Default value | Comment |
|---|
| `age` | `Number` | nil | |
| `firstName` | `String` | nil | |
| `lastName` | `String` | nil | |
"
Class {
#name : #FamixTest4Person,
#superclass : #FamixTest4Entity,
#instVars : [
'#firstName => FMProperty',
'#lastName => FMProperty',
'#age => FMProperty',
'#books => FMMany type: #FamixTest4Book opposite: #person'
],
#category : #'Famix-Test4-Entities-Entities'
Expand All @@ -36,12 +47,33 @@ FamixTest4Person class >> isAbstract [
^ self == FamixTest4Person
]

{ #category : #comparing }
FamixTest4Person >> = aFamixTest4Person [

<generated>
^ aFamixTest4Person firstName = self firstName and: [ aFamixTest4Person lastName = self lastName and: [ aFamixTest4Person age = self age] ]
]

{ #category : #adding }
FamixTest4Person >> addBook: anObject [
<generated>
^ self books add: anObject
]

{ #category : #accessing }
FamixTest4Person >> age [

<FMProperty: #age type: #Number>
<generated>
^ age
]

{ #category : #accessing }
FamixTest4Person >> age: anObject [
<generated>
age := anObject
]

{ #category : #accessing }
FamixTest4Person >> books [
"Relation named: #books type: #FamixTest4Book opposite: #person"
Expand All @@ -64,3 +96,38 @@ FamixTest4Person >> booksGroup [
<navigation: 'Books'>
^ MooseSpecializedGroup withAll: self books asSet
]

{ #category : #accessing }
FamixTest4Person >> firstName [

<FMProperty: #firstName type: #String>
<generated>
^ firstName
]

{ #category : #accessing }
FamixTest4Person >> firstName: anObject [
<generated>
firstName := anObject
]

{ #category : #comparing }
FamixTest4Person >> hash [

<generated>
^ self firstName hash bitXor: self lastName hash bitXor: self age hash
]

{ #category : #accessing }
FamixTest4Person >> lastName [

<FMProperty: #lastName type: #String>
<generated>
^ lastName
]

{ #category : #accessing }
FamixTest4Person >> lastName: anObject [
<generated>
lastName := anObject
]
4 changes: 2 additions & 2 deletions src/Famix-Test4-Entities/FamixTest4School.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Class {
#superclass : #FamixTest4Entity,
#instVars : [
'#principal => FMOne type: #FamixTest4Principal opposite: #school',
'#rooms => FMMany type: #FamixTest4Room opposite: #school',
'#students => FMMany type: #FamixTest4Student opposite: #school',
'#teachers => FMMany type: #FamixTest4Teacher opposite: #school'
'#teachers => FMMany type: #FamixTest4Teacher opposite: #school',
'#rooms => FMMany type: #FamixTest4Room opposite: #school'
],
#category : #'Famix-Test4-Entities-Entities'
}
Expand Down
2 changes: 1 addition & 1 deletion src/Famix-Test4-Entities/FamixTest4TEntityCreator.trait.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It provides an API for creating entities and adding them to the model.
"
Trait {
#name : #FamixTest4TEntityCreator,
#category : #'Famix-Test4-Entities-Traits'
#category : #'Famix-Test4-Entities-Model'
}

{ #category : #meta }
Expand Down
16 changes: 16 additions & 0 deletions src/Famix-Test4-Tests/FamixTest4Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ FamixTest4Test >> setUp [
student1 := FamixTest4Student new.
student1 := FamixTest4Student new.
student2 := FamixTest4Student new.
student2 firstName: 'firstname'.
student2 lastName: 'lastName'.
student3 := FamixTest4Student new.
teacher1 := FamixTest4Teacher new.
teacher1 firstName: 'John'.
teacher2 := FamixTest4Teacher new.
teacher2 firstName: 'Bob'.
book1 := FamixTest4Book new.
book2 := FamixTest4Book new.

Expand All @@ -58,6 +62,18 @@ FamixTest4Test >> testAbstractClasses [
self assert: FamixTest4Person isAbstract
]

{ #category : #tests }
FamixTest4Test >> testEqualityStudent [

| sameStudent |
"A copy of student2 to test same"
sameStudent := FamixTest4Student new.
sameStudent firstName: 'firstname'.
sameStudent lastName: 'lastName'.

self assert: student2 equals: sameStudent
]

{ #category : #tests }
FamixTest4Test >> testRelations [
self assert: (principal books includes: book2).
Expand Down
11 changes: 10 additions & 1 deletion src/Famix-TestGenerators/FamixTest4Generator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ FamixTest4Generator >> defineHierarchy [

{ #category : #definition }
FamixTest4Generator >> defineProperties [

super defineProperties.

(builder ensureClassNamed: #Entity) property: #name type: #String
(builder ensureClassNamed: #Entity) property: #name type: #String.

person property: #firstName type: #String.
person property: #lastName type: #String.
person property: #age type: #Number.
person withEqualityCheckOn: { #firstName. #lastName. #age }.

book property: #id type: #Number.
book withEqualityCheckOn: { #id }
]

{ #category : #definition }
Expand Down

0 comments on commit d0830d6

Please sign in to comment.