-2

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

screen shot

Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
Quan Bui
  • 1
  • 1
  • 4
  • 2
    Please post code, rather than pictures of code. Consider a Java `class` having attributes such as `java.lang.Boolean`, `java.awt.Color`, `java.awt.Shape`, etc. Then, please [edit] your question to include a [mcve] that shows your current approach. – trashgod Dec 26 '18 at 09:10
  • 1
    You also want to be a bit more specific about what you are wanting to do. The Ada example you are providing is not a Union, which is very different in function from a variant record (which is what you are showing). You can emulate some aspects of a variant record in Java, but it would be important to know what functionality specifically you want to emulate in Java that the Ada example provides you. – Jere Dec 26 '18 at 14:59
  • The image appears to be an excerpt from [CS 354](http://cs.boisestate.edu/~buff/syllabi/cs354-1f14.pdf), a lecture of which examines [design issues](http://cs.boisestate.edu/~alark/cs354/lectures/records_unions.pdf) with traditional unions. – trashgod Dec 27 '18 at 22:18
  • 1
    Completly off-topic of the OP, the second @trashgod links states at the end "With structural type equivalence, you cannot differentiate between types of the same structure (*e.g. different units of speed, both float*)". Ada 2012 or higher provides a very nice feature which I would call "dimension analysis". Basically, it allows to define both *units* and a mapped numerical type. Ada toolchain would then have a compile/exec time type *and* unit solving. A better overview here https://www.adacore.com/gems/gem-136-how-tall-is-a-kilogram – LoneWanderer Dec 21 '19 at 00:08
  • See also [_Converting different unit types in JScience library_](https://stackoverflow.com/q/44407365/230513). – trashgod Dec 21 '19 at 18:15

2 Answers2

1

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 value True 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;
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41
0

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.

Julian Fondren
  • 5,459
  • 17
  • 29
  • 1
    You might consider also making the member variables that correspond to the ones in the case statement in the Ada version to be private and use getters/setters to enforce the discriminant check. Though it all depends on the OP's actual requirements, which haven't been updated. – Jere Jan 03 '19 at 21:01