Thursday 29 March 2018

Modelling Java Annotations in UML

I was wondering how to put Java Annotations in an UML schema.

Turns out there is no support for it, but some smart people gave it a try anyway and wrote it down in a paper1. Of course they are using the already existing possibilities of UML, so the UML does not exactly match up with the idea of Annotations.

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. Since Java 8, also annotations are supported anywhere a type is used2.

However, some people3 do have a valid point when they say that modelling Annotations might be a severe case of micromodelling.

PlantUML

It's not a secret that I am a fan of plantuml4, and all the pretty pictures on this page are dynamically created by the PlantUML Online Server5 that they have running. Which also means, if the pictures are not visible, the server is down.

I wanted to see how far I could take PlantUML in processing the ideas in the paper.

1. Attributes as UML Stereotypes

@startuml
class  Mail <<@FunctionalInterface>> <<@Table(name = "mm_mailtable")>> {
  -@Id id: Long
  -@NotNull @Column subject: String
  +getId(): Long
  +setId(id: Long)
}
@enduml

2. Attributes as extra class subbox

@startuml
class Mail {
  @Entity
  @Table(name = "mm_mailtable")
  --
  -@NotNull @Column subject: String
  --
  +getId(): Long
  +setId(id: Long)
}
@enduml

3. Attributes as UML Template Parameter

@startuml
class Mail <@Entity \n @Table(name = "mm_mailtable")> {
  -@Id id: Long
  -@NotNull @Column subject: String
  --
  +getId(): Long
  +setId(id: Long)
}
@enduml

4. Attributes as separate Class

@startuml
class Mail {
  -@Id id: Long
  -@NotNull @Column subject: String
  --
  +getId(): Long
  +setId(id: Long)
}
class "@Entity \n @Table(name = "mm_mailtable")" as Entity
Entity - Mail : <<annotated>>
@enduml

5. Attributes as Comment boxes

@startuml
class Mail {
  -@Id id: Long
  -@NotNull @Column subject: String
  --
  +getId(): Long
  +setId(id: Long)
}
note right
@Entity
@Table(name = "mm_mailtable")
end note
@enduml

Conclusion

My personal opinion is that UML Stereotypes folllows the Java class most narrowly, so I like that. But I think the "Annotations as a separate class" follows UML conventions quite good.

The paper contains a nice table where they are considering the pros and cons of all the methods described above.

As there seems no standard defined in UML, if you need to model Annotations at all (and that's a big if), pick the one you like.

References

[1] Representing Explicit Attributes in UML
http://dawis2.icb.uni-due.de/events/AOM_MODELS2005/Cepa.pdf
[2] Oracle Tutorial - Annotations
https://docs.oracle.com/javase/tutorial/java/annotations/basics.html
[3] CodeRanch - UML / Class Diagram Syntax for Java Annotations
https://coderanch.com/t/100641/UML-Class-Diagram-Syntax-Java
[4] Plantuml
http://plantuml.com/
[5] Plantuml Online Server
http://www.plantuml.com/plantuml/uml/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000
GitHUb - Mail.java
https://github.com/maartenl/Land-of-Karchan/blob/master/karchangame/src/main/java/mmud/database/entities/game/Mail.java

Thursday 22 March 2018

Commitment vs. Forecast

One of my colleagues had pointed out (months ago) that the wording of the Scrum Guide has changed a little.

A bit late to mention it here perhaps, but the wording of the Scrum guide regarding Commitment has changed to Forecast.

The reasons for this can be found in the followoing article: Commitment vs. Forecast: A Subtle But Important Change to Scrum.

Thursday 15 March 2018

Comparing NULLs in SQL

Consider the following table "mm_mailtable", containing the following data:

id name toname whensent subject haveread newmail
27999 William Joe 2018-03-13 12:41:25 Golf next week? 1 0
28000 William Linda 2018-03-13 12:41:25 I'm out! 1 0
28001 Linda William 2018-03-13 12:41:25 Okay! 1 0
28002 Joe William 2018-03-13 12:41:25 Sure thing! 1 1
28003 Jim William 2018-03-13 12:41:25 The house 1 NULL

Also consider the following query I found in our source code somewhere.

SELECT *
FROM mm_mailtable
WHERE toname = :NAME
AND newmail = COALESCE(:NEWMAIL, newmail);

Let's say we try the query out with "William" as NAME and 0 as NEWMAIL.

We get an old mail to William with "Okay!" as subject.

Let's say we try the query out with "William" as NAME and 1 as NEWMAIL.

We get a new mail to William with "Sure thing!" as subject.

Let's say we try the query out with "William" as NAME and NULL as NEWMAIL.

Now I would expect to get all three entries returned, but the one containing the NULL value for "newmail" is not returned.

It takes some getting used to, but comparing null values is not possible in most databases, as it is undefined (a.k.a.. NULL).

See for more and better explanations the references below.

P.S. in this case, in our database, the column "newmail" should have been defined as "NOT NULL" and given a "DEFAULT" value, to prevent this sort of thing. Apparently it was forgotten.

Rewriting the query

This should work:

SELECT * 
FROM mm_mailtable 
WHERE toname = :NAME
AND (:NEWMAIL is null OR :NEWMAIL = newmail);

References

StackOverflow - why is null not equal to null false
https://stackoverflow.com/questions/1833949/why-is-null-not-equal-to-null-false
Baron Schwartz's Blog - Why NULL never compares false to anything in SQL
https://www.xaprb.com/blog/2006/05/18/why-null-never-compares-false-to-anything-in-sql/

Thursday 8 March 2018

Java EE is now Jakarta EE

Our architect mentioned that Oracle has put JEE into the hands of the Eclipse Foundation, under the new name Jakarta EE1.

Apparently the search for a new Logo for Jakarta EE is still on2.

References

[1] ZDNet - Goodbye JEE hello Jakarte EE
http://www.zdnet.com/article/good-bye-jee-hello-jakarta-ee/
[2] jaxenter - Jakarte EE needs a logo - Submissions accepted until March 14
https://jaxenter.com/jakarta-ee-needs-logo-march-14-142110.html
TheRegister - Java EE is now Jakarta EE
https://www.theregister.co.uk/2018/03/04/java_ee_is_now_jakarta_ee/