The picture below is an Ada
Union type (Figure).
How can I convert this Union type from Ada to Object in Java? Please help me.
This is the code
The picture below is an Ada
Union type (Figure).
How can I convert this Union type from Ada to Object in Java? Please help me.
This is the code
I have doubts concerning the canonical way to designate such Ada records ('unions'), I always used 'variant record' or 'discriminant record'.
According to https://en.wikibooks.org/wiki/Ada_Programming/Types/record#Union Union
in an Ada context refers to variant record declaration + C convention union.
This is enforced by ARM §B.3.3, quoting:
Specifying aspect
Unchecked_Union
to have the valueTrue
defines an interface correspondence between a given discriminated type and some C union. EDIT: The aspect requires that the associated type shall be given a representation that allocates no space for its discriminant(s).
type T (Flag : Boolean := False) is
record
case Flag is
when False =>
F1 : Float := 0.0;
when True =>
F2 : Integer := 0;
end case;
end record
with Unchecked_Union;
32/2
X : T;
Y : Integer := X.F2; -- erroneous
Quoting ARM § 3.8.1, the OP record declaration is NOT an union.
Example of record type with a variant part:
type Device is (Printer, Disk, Drum);
type State is (Open, Closed);
type Peripheral(Unit : Device := Disk) is
record
Status : State;
case Unit is
when Printer =>
Line_Count : Integer range 1 .. Page_Size;
when others =>
Cylinder : Cylinder_Index;
Track : Track_Number;
end case;
end record;
Start by taking a hit on space and implementing this by defining enums and then adding all the fields to the same object.
class Figure {
public enum Shape { Circle, Triangle, Rectangle };
public enum Colors { Red, Green, Blue };
Shape form;
boolean Filled;
Colors color;
float Diameter;
int Leftside, Rightside;
float Angle;
int Side1, Side2;
}
operations on the object just need to check the value of 'form' to know which fields they should consider, and which fields to ignore.
Now that you've translated the idea (however roughly) to Java, you can apply your knowledge of Java to iterate on the idea.