Sujet : Re: OpenVMS system programming language
De : arne (at) *nospam* vajhoej.dk (Arne Vajhøj)
Groupes : comp.os.vmsDate : 25. Feb 2025, 04:42:56
Autres entêtes
Organisation : SunSITE.dk - Supporting Open source
Message-ID : <67bd3c41$0$712$14726298@news.sunsite.dk>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
On 2/24/2025 9:22 PM, Lawrence D'Oliveiro wrote:
And then there’s the long-windedness of code like
static final java.util.Map<Integer, String> TypeNames;
/* mapping from sensor type codes to symbolic names */
static
{
TypeNames = new java.util.HashMap<Integer, String>();
TypeNames.put(Sensor.TYPE_ACCELEROMETER, "accelerometer");
TypeNames.put(Sensor.TYPE_AMBIENT_TEMPERATURE, "ambient temperature");
TypeNames.put(Sensor.TYPE_GRAVITY, "gravity");
TypeNames.put(Sensor.TYPE_GYROSCOPE, "gyroscope");
TypeNames.put(Sensor.TYPE_LIGHT, "light");
TypeNames.put(Sensor.TYPE_LINEAR_ACCELERATION, "linear accel");
TypeNames.put(Sensor.TYPE_MAGNETIC_FIELD, "magnetic");
TypeNames.put(Sensor.TYPE_ORIENTATION, "orientation");
TypeNames.put(Sensor.TYPE_PRESSURE, "pressure");
TypeNames.put(Sensor.TYPE_PROXIMITY, "proximity");
TypeNames.put(Sensor.TYPE_RELATIVE_HUMIDITY, "relative humidity");
TypeNames.put(Sensor.TYPE_ROTATION_VECTOR, "rotation");
TypeNames.put(Sensor.TYPE_TEMPERATURE, "temperature");
} /*static*/
because the language doesn’t provide expressions that evaluate to
common structure types like dictionaries/maps.
If you are on Java 10+ then you could:
Map<Integer, String> typeNames = Map.of(Sensor.TYPE_ACCELEROMETER, "accelerometer",
Sensor.TYPE_AMBIENT_TEMPERATURE, "ambient temperature",
...);
But the Sensor.TYPE_* int approach has been obsolete since Java 5 where
Java got enums.
With enum the need for that Map disappears.
You can either hack it:
public class EnumHack {
public static enum SensorType {
TYPE_ACCELEROMETER, TYPE_AMBIENT_TEMPERATURE, TYPE_GRAVITY;
public String prettyName() {
return name().substring(5).toLowerCase().replace('_', ' ');
}
}
public static void main(String[] args) {
System.out.println(SensorType.TYPE_AMBIENT_TEMPERATURE.prettyName());
}
}
or do it the right way:
public class EnumRight {
public static enum SensorType {
TYPE_ACCELEROMETER("accelerometer"), TYPE_AMBIENT_TEMPERATURE("ambient temperature"), TYPE_GRAVITY("gravity");
private String prettyName;
private SensorType(String prettyName) {
this.prettyName = prettyName;
}
public String getPrettyName() {
return prettyName;
}
}
public static void main(String[] args) {
System.out.println(SensorType.TYPE_AMBIENT_TEMPERATURE.getPrettyName());
}
}
Arne