I need to write this code in C#. Any help would be appreciated. The interface and the first class is where the issues are. How can I convert that into C#? EnumByteConverter & EnumByteConverter>? Is this even possible? What would my options be?
public interface EnumByteConverter<E extends Enum<E> & EnumByteConverter<E>>
{
byte convert();
E convert(byte val);
}
public class ReverseByteEnumMap<V extends Enum<V> & EnumByteConverter>
{
private Map<Byte, V> map = new HashMap<>();
public ReverseByteEnumMap(Class<V> valueType)
{
for (V v : valueType.getEnumConstants())
{
map.put(v.convert(), v);
}
}
public V get(byte num)
{
return map.get(num);
}
}
public enum Transport implements EnumByteConverter<Transport>
{
FrameFormatB(0),
FrameFormatA(1),
FrameDraft(254),
FrameUnknown(255);
private static ReverseByteEnumMap<Transport> map = new ReverseByteEnumMap<>(Transport.class);
private final byte value;
Transport(int value)
{
this.value = (byte) value;
}
public byte convert()
{
return value;
}
public Transport convert(byte val)
{
return map.get(val);
}
public static Transport get(byte val)
{
return map.get(val);
}
}
This is what I have tried. I managed to implement the interface using struct, and the reverse class with similar thing.
public interface IEnumByteConverter<E> where E : struct
{
byte Convert();
E Convert(byte val);
}
}
public class ReverseByteEnumMap<T> where T : struct, IEnumByteConverter<T>
{
private Dictionary<Byte, T> map = new Dictionary<Byte, T>();
private IEnumByteConverter<T> converter;
public ReverseByteEnumMap(T valueType)
{
foreach (T t in Enum.GetValues(typeof(T)))
map.Add(t.Convert() , t);
}
public T Get(byte num)
{
return map[num];
}
}
public class Transport : IEnumByteConverter<TransportEnums>
{
public enum TransportEnums
{
FrameFormatB = 0,
FrameFormatA = 1,
FrameDraft = 254,
FrameUnknown = 255
}
private static ReverseByteEnumMap<Transport> map = new ReverseByteEnumMap<Transport> ();
private byte value;
Transport(int value)
{
this.value = (byte)value;
}
public byte Convert()
{
return value;
}
public TransportEnums Convert(byte val)
{
return map.Get(val);
}
public static TransportEnums Get(byte val)
{
return map.Get(val);
}
}
But I got stuck in the last class. I cannot use the Transport.class nor enums. I cannot tell if this a right path I am heading on