Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum PcapLinkType {
- DLT_NULL(0)
- DLT_EN10MB(1)
- DLT_EN3MB(2),
- DLT_AX25(3),
- /*snip, 200 more enums, not always consecutive.*/
- DLT_UNKNOWN(-1);
- private final int value;
- PcapLinkType(int value) {
- this.value= value;
- }
- }
- int val = in.readInt();
- PcapLinkType type = ???; /*convert val to a PcapLinkType */
- private static final Map<Integer, PcapLinkType> intToTypeMap = new HashMap<Integer, PcapLinkType>();
- static {
- for (PcapLinkType type : PcapLinkType.values()) {
- intToTypeMap.put(type.value, type);
- }
- }
- public static PcapLinkType fromInt(int i) {
- PcapLinkType type = intToTypeMap.get(Integer.valueOf(i));
- if (type == null)
- return PcapLinkType.DLT_UNKNOWN;
- return type;
- }
- enum MyEnum {
- FIRST, SECOND, THIRD;
- private static MyEnum[] allValues = values();
- public static MyEnum fromOrdinal(int n) {return allValues[n];}
- }
- public static PcapLinkType forCode(int code) {
- for (PcapLinkType typе : PcapLinkType.values()) {
- if (type.getValue() == code) {
- return type
- }
- }
- return null;
- }
- public enum PcapLinkType {
- DLT_NULL(0)
- DLT_EN10MB(1)
- DLT_EN3MB(2),
- DLT_AX25(3),
- DLT_UNKNOWN(-1);
- private final int value;
- PcapLinkType(int value) {
- this.value= value;
- }
- }
- PcapLinkType type = PcapLinkType.values()[1]; /*convert val to a PcapLinkType */
- public enum PcapLinkType {
- DLT_NULL(0),
- DLT_EN10MB(1),
- DLT_EN3MB(2),
- DLT_AX25(3),
- /*snip, 200 more enums, not always consecutive.*/
- DLT_UNKNOWN(-1);
- private static final Map<Integer, PcapLinkType> typesByValue = new HashMap<Integer, PcapLinkType>();
- static {
- for (PcapLinkType type : PcapLinkType.values()) {
- typesByValue.put(type.value, type);
- }
- }
- private final int value;
- private PcapLinkType(int value) {
- this.value = value;
- }
- public static PcapLinkType forValue(int value) {
- return typesByValue.get(value);
- }
- }
- static {
- for (PcapLinkType type : PcapLinkType.values()) {
- intToTypeMap.put(type.getValue(), type);
- }
- }
- public enum Quality {ENOUGH,BETTER,BEST;
- private static final int amount = EnumSet.allOf(Quality.class).size();
- private static Quality[] val = new Quality[amount];
- static{ for(Quality q:EnumSet.allOf(Quality.class)){ val[q.ordinal()]=q; } }
- public static Quality fromInt(int i) { return val[i]; }
- public Quality next() { return fromInt((ordinal()+1)%amount); }
- }
- public static PcapLinkType of(int linkType) {
- switch (linkType) {
- case -1: return DLT_UNKNOWN
- case 0: return DLT_NULL;
- //ETC....
- default: return null;
- }
- }
- static final PcapLinkType[] values = { DLT_NULL, DLT_EN10MB, DLT_EN3MB, null ...}
- ...
- public static PcapLinkType getPcapLinkTypeForInt(int num){
- try{
- return values[int];
- }catch(ArrayIndexOutOfBoundsException e){
- return DLT_UKNOWN;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement