Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. public enum PcapLinkType {
  2. DLT_NULL(0)
  3. DLT_EN10MB(1)
  4. DLT_EN3MB(2),
  5. DLT_AX25(3),
  6. /*snip, 200 more enums, not always consecutive.*/
  7. DLT_UNKNOWN(-1);
  8. private final int value;
  9.  
  10. PcapLinkType(int value) {
  11. this.value= value;
  12. }
  13. }
  14.  
  15. int val = in.readInt();
  16. PcapLinkType type = ???; /*convert val to a PcapLinkType */
  17.  
  18. private static final Map<Integer, PcapLinkType> intToTypeMap = new HashMap<Integer, PcapLinkType>();
  19. static {
  20. for (PcapLinkType type : PcapLinkType.values()) {
  21. intToTypeMap.put(type.value, type);
  22. }
  23. }
  24.  
  25. public static PcapLinkType fromInt(int i) {
  26. PcapLinkType type = intToTypeMap.get(Integer.valueOf(i));
  27. if (type == null)
  28. return PcapLinkType.DLT_UNKNOWN;
  29. return type;
  30. }
  31.  
  32. enum MyEnum {
  33. FIRST, SECOND, THIRD;
  34. private static MyEnum[] allValues = values();
  35. public static MyEnum fromOrdinal(int n) {return allValues[n];}
  36. }
  37.  
  38. public static PcapLinkType forCode(int code) {
  39. for (PcapLinkType typе : PcapLinkType.values()) {
  40. if (type.getValue() == code) {
  41. return type
  42. }
  43. }
  44. return null;
  45. }
  46.  
  47. public enum PcapLinkType {
  48. DLT_NULL(0)
  49. DLT_EN10MB(1)
  50. DLT_EN3MB(2),
  51. DLT_AX25(3),
  52. DLT_UNKNOWN(-1);
  53.  
  54. private final int value;
  55.  
  56. PcapLinkType(int value) {
  57. this.value= value;
  58. }
  59. }
  60.  
  61. PcapLinkType type = PcapLinkType.values()[1]; /*convert val to a PcapLinkType */
  62.  
  63. public enum PcapLinkType {
  64. DLT_NULL(0),
  65. DLT_EN10MB(1),
  66. DLT_EN3MB(2),
  67. DLT_AX25(3),
  68. /*snip, 200 more enums, not always consecutive.*/
  69. DLT_UNKNOWN(-1);
  70.  
  71. private static final Map<Integer, PcapLinkType> typesByValue = new HashMap<Integer, PcapLinkType>();
  72.  
  73. static {
  74. for (PcapLinkType type : PcapLinkType.values()) {
  75. typesByValue.put(type.value, type);
  76. }
  77. }
  78.  
  79. private final int value;
  80.  
  81. private PcapLinkType(int value) {
  82. this.value = value;
  83. }
  84.  
  85. public static PcapLinkType forValue(int value) {
  86. return typesByValue.get(value);
  87. }
  88. }
  89.  
  90. static {
  91. for (PcapLinkType type : PcapLinkType.values()) {
  92. intToTypeMap.put(type.getValue(), type);
  93. }
  94. }
  95.  
  96. public enum Quality {ENOUGH,BETTER,BEST;
  97. private static final int amount = EnumSet.allOf(Quality.class).size();
  98. private static Quality[] val = new Quality[amount];
  99. static{ for(Quality q:EnumSet.allOf(Quality.class)){ val[q.ordinal()]=q; } }
  100. public static Quality fromInt(int i) { return val[i]; }
  101. public Quality next() { return fromInt((ordinal()+1)%amount); }
  102. }
  103.  
  104. public static PcapLinkType of(int linkType) {
  105.  
  106. switch (linkType) {
  107. case -1: return DLT_UNKNOWN
  108. case 0: return DLT_NULL;
  109.  
  110. //ETC....
  111.  
  112. default: return null;
  113.  
  114. }
  115. }
  116.  
  117. static final PcapLinkType[] values = { DLT_NULL, DLT_EN10MB, DLT_EN3MB, null ...}
  118.  
  119. ...
  120.  
  121. public static PcapLinkType getPcapLinkTypeForInt(int num){
  122. try{
  123. return values[int];
  124. }catch(ArrayIndexOutOfBoundsException e){
  125. return DLT_UKNOWN;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement