Java enum (or int constants) vs c enum

I am trying to do something that usually looks in C:

typedef enum { HTTP =80, TELNET=23, SMTP =25, SSH =22, GOPHER=70} TcpPort; 

Approach 1

Here is what I have in Java using enum :

 public static enum TcpPort{ HTTP(80), TELNET(23), SMTP(25), SSH(22), GOPHER(70); private static final HashMap<Integer,TcpPort> portsByNumber; static{ portsByNumber = new HashMap<Integer,TcpPort>(); for(TcpPort port : TcpPort.values()){ portsByNumber.put(port.getValue(),port); } } private final int value; TcpPort(int value){ this.value = value; } public int getValue(){ return value; } public static TcpPort getForValue(int value){ return portsByNumber.get(value); } } 

Approach 1 - Problems

I find that I have to repeat this pattern in different places, but wondered: is there a better way? especially because:

  • it seems confusing and less elegant, and
  • it also shifts what will compile time to run. "

One of the reasons I use this mapping is because it looks better in switch statements, for example:

 switch(tcpPort){ case HTTP: doHttpStuff(); break; case TELNET: doTelnetStuff(); break; .... } 

I believe there are advantages to stronger enumerated type safety.

Approach 2 I know I could:

 public static class TcpPort{ public static final int HTTP = 80; public static final int TELNET = 23; public static final int SMTP = 25; public static final int SSH = 22; public static final int GOPHER = 70; } 

but I feel that enum is still better. Is my approach enum above the way? or is there another way?

+6
source share
3 answers

I feel that for expressive purposes only, the switch enum is redundant in your case, and it's better to just use the final static int constants. For example, saving memory.

In addition, Joshua Bloch in his Effective Java recommends using enum instead of int constants in his Item 30: Use enums instead of int constants . But IMHO is the right way to use enum for more complex cases, instead just replacing the c #define construct.

UPDATE : as the author mentioned in his comment on this answer of mine, he wonders if using enum better than int constants in general. In this case, such a question will be duplicated (see Java: Enum vs. Int ), and my answer will be: in general, enum better, and why - look at Joshua Bloch Item 30 , as I mentioned earlier.

+1
source

Here you mix two concepts. #define allows you to use preprocessor . The compiler will only know the value that you defined.

In Java using Enums only for port numbers, it seems unreasonable, since they must be configurable by imo.

I would recommend using an interface with factories (surrounded by an enumeration for default types, allowing third parties to add new [OOP] interfaces.

Generally speaking, I would not use Enumerations for constants, simply because they must be covered within the corresponding class.

You can import constants with static imports if you want to increase readability. *

* I would consider tho static import bad practice.

0
source

You can create a couple of such classes:

 package com.ggl.testing; import java.util.ArrayList; import java.util.List; public class TCPPort { private List<Port> ports; public TCPPort() { this.ports = new ArrayList<Port>(); this.ports.add(new Port("HTTP", 80)); this.ports.add(new Port("TELNET", 23)); this.ports.add(new Port("SMTP", 25)); this.ports.add(new Port("SSH", 22)); this.ports.add(new Port("GOPHER", 70)); } public Port getPortByName(String portName) { for (Port p : ports) { if (portName.equals(p.getPortName())) { return p; } } return null; } public Port getPortByNumber(int portNumber) { for (Port p : ports) { if (portNumber == p.getPortNumber()) { return p; } } return null; } public class Port { private final int portNumber; private final String portName; public Port(String portName, int portNumber) { this.portName = portName; this.portNumber = portNumber; } public int getPortNumber() { return portNumber; } public String getPortName() { return portName; } } } 
0
source

Source: https://habr.com/ru/post/986224/


All Articles