BTW, this code already shows a bug that stems from the static method usage. It says ElasticIpVm is not a system vm (which I don't believe is true). Probably because whoever added elastic ip vm didn't see the static method and so didn't add the vm into the method.
--Alex > -----Original Message----- > From: Alex Huang [mailto:alex.hu...@citrix.com] > Sent: Monday, July 22, 2013 5:14 PM > To: dev@cloudstack.apache.org > Subject: [DESIGN] Why is enum a class... > > I just went over this code and thought it was related and might be interested > to other developers. > > What's the difference between declaring a enum like this > > public enum Type { > User, > DomainRouter, > ConsoleProxy, > SecondaryStorageVm, > ElasticIpVm, > ElasticLoadBalancerVm, > InternalLoadBalancerVm, > > /* > * UserBareMetal is only used for selecting VirtualMachineGuru, there > is > no > * VM with this type. UserBareMetal should treat exactly as User. > */ > UserBareMetal; > > public static boolean isSystemVM(VirtualMachine.Type vmtype) { > if (DomainRouter.equals(vmtype) > || ConsoleProxy.equals(vmtype) > || SecondaryStorageVm.equals(vmtype) || > InternalLoadBalancerVm.equals(vmtype)) { > return true; > } > return false; > } > } > > Vs > > public enum Type { > User(false), > DomainRouter(true), > ConsoleProxy(true), > SecondaryStorageVm(true), > ElasticIpVm(true), > ElasticLoadBalancerVm(true), > InternalLoadBalancerVm(true), > > /* > * UserBareMetal is only used for selecting VirtualMachineGuru, there > is > no > * VM with this type. UserBareMetal should treat exactly as User. > */ > UserBareMetal(false); > > private boolean _isSystemVm; > > private Type(Boolean isSystemVm) { > _isSystemVm = isSystemVm; > } > > public boolean isSystemVM() { > return _isSystemVm; > } > } > > The second declaration is more self-descriptive: > > - It tells developer when they add more to this enum, they have to specify > whether it is a system vm. They may have missed the static method in the > first declaration and causes failures later. > - It tells developers using the enum that there's a property that let's them > know it is a system vm so they can do discovery using a context-sensitive > editor like Eclipse. > > This is the reason why enum is not a simple constant declaration in Java (vs > C/C++). > --Alex