Reputation:
public enum HTTPHeaderKey {
CACHE_CONTROL("Cache-Control"), CONNECTION("Connection"), TRANSFER_ENCODING("Transfer-Encoding"), HOST("Host"), USER_AGENT("User-Agent"), CONTENT_LENGTH("Content-Length"), CONTENT_TYPE("Content-Type");
private final String str;
private HTTPHeaderKey(final String _str) {
str = _str;
}
/** Over ridden toString returns the HTTP/1.1 compatible header */
public String toString() {
return str;
}
};
I am trying to convert this enum to Delphi. I know that how to define enum variables but i have no idea, how can i insert a method in enum?
Or can someone suggest another way to convert this ?
Upvotes: 7
Views: 454
Reputation: 36654
Based on Davids answer, this modified version should be able to print the HTTP header names:
type
THTTPHeaderKey = (hkCACHE_CONTROL, hkCONNECTION, hkTRANSFER_ENCODING);
TTHTTPHeaderKeyHelper = record helper for THTTPHeaderKey
private
const
EnumNames: array [THTTPHeaderKey] of string = ('Cache-Control', 'Connection', 'Transfer-Encoding');
public
function ToString: string;
end;
// ToString returns the HTTP/1.1 compatible header
function TTHTTPHeaderKeyHelper.ToString: string;
begin
Result := EnumNames[Self];
end;
...
begin
Writeln(hkCACHE_CONTROL.ToString);
Writeln(hkTRANSFER_ENCODING.ToString);
end.
should output
Cache-Control
Transfer-Encoding
Upvotes: 1
Reputation: 612963
You can get part way there with a record helper, which is available for value types from XE3 onwards. For instance:
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.TypInfo;
type
TMyEnum = (enumValue1, enumValue2);
TMyEnumHelper = record helper for TMyEnum
public
function ToString: string;
end;
function TMyEnumHelper.ToString: string;
begin
Result := GetEnumName(TypeInfo(TMyEnum), ord(Self));
end;
begin
Writeln(enumValue1.ToString);
Writeln(enumValue2.ToString);
end.
This program outputs the following:
enumValue1 enumValue2
Of course you may prefer to do it like this:
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.TypInfo;
type
TMyEnum = (enumValue1, enumValue2);
TMyEnumHelper = record helper for TMyEnum
private
const
EnumNames: array [TMyEnum] of string = ('Friendly name 1', 'Friendly name 2');
public
function ToString: string;
end;
function TMyEnumHelper.ToString: string;
begin
Result := EnumNames[Self];
end;
begin
Writeln(enumValue1.ToString);
Writeln(enumValue2.ToString);
end.
The output here is:
Friendly name 1 Friendly name 2
This would presumably allow you to tackle the fact the Delphi enumerated types don't support the textual naming that is available in Java.
The other method, the constructor HTTPHeaderKey
cannot be supported with an enumerated type. The reason being that it requires state, and the only state for a Delphi enumerated type is the enumerated type value itself. You cannot graft on an extra instance variable as is done in the Java code.
All things considered, I don't think an attempt at a literal translation with an enumerated type will work out. I suggest that you translate using either a record or a class, and build the equivalent functionality using the available Delphi language constructs.
Upvotes: 8