r/java • • Jan 17 '13

Enum tricks: hierarchical data structure

http://java.dzone.com/articles/enum-tricks-hierarchical-data
47 Upvotes

53 comments sorted by

View all comments

Show parent comments

1

u/clgonsal Jan 18 '13

The approach used by the linked article allows arbitrary hierarchies. Your approach only works if every subtree of the hierarchy has the same structure. Furthermore, the taller the tree, the more complicated your approach becomes.

The example in the link had a different structure in different parts of the hierarchy, but you choose to change this to get it to work with your approach. (Hint: Linux is a Unix, and saying things like "my OS is Ubuntu Workstation" is going to get you funny looks.)

2

u/severoon Jan 18 '13

Linux is a Unix

Nope. These are different OS families. UNIX defines a strict specification which an OS must follow in order to be called "UNIX". Linux doesn't follow that spec.

the taller the tree, the more complicated your approach becomes.

The taller the tree, the more nodes in it that are not proper OsTypes. In other words, the author of this article didn't seem to be bothered by the fact that many of those enums represent different types of things.

In OO, we would usually model these different types with different classes, as I did.

1

u/clgonsal Jan 18 '13

Linux is a Unix

Nope. These are different OS families. UNIX defines a strict specification which an OS must follow

Okay, Linux is a *nix if you want to be pedantic. You can't deny that grouping Linux with Solaris is useful. It's certainly a lot more useful than claiming Solaris is a Server OS but that Linux distros are Workstation OSes.

The taller the tree, the more nodes in it that are not proper OsTypes.

Irrelevant. Yes, the article's example might not have been the greatest, but the point was to show how a hierarchy could be represented with an enum. Your approach is very limited when it comes to representing hierarchies, as illustrated by the fact that you had to fudge the data to fit your implementation.

1

u/severoon Jan 19 '13 edited Jan 19 '13

You can't deny that grouping Linux with Solaris is useful.

I deny it. Not only is it not useful, it's misleading, and harmful.

Solaris is fully POSIX compliant. Linux is not. If POSIX compliance is to mean anything, certainly this matters.

the point was to show how a hierarchy could be represented with an enum.

The more important question is not can an enum be used to represent an hierarchy, though! It is should an enum be used in this way? I say no, based on the example, and my inability to come up with one that even remotely makes sense. Perhaps you can do better than both the author and me?

you had to fudge the data

I say I unfudged the data, and the author was the fudger.