Constrained ada type in Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ed Trubia

    Constrained ada type in Java

    Have the following type that applies contraints. How could this be easily
    coded in Java

    type ET_ACTIVE_PORT_ CONFIGURATION is
    ( HDX_TX1_RX1,
    HDX_TX2_RX2,
    HDX_TX1_RX2,
    HDX_TX2_RX1,
    FDX_TX1_RX2,
    FDX_TX2_RX1);

    for ET_ACTIVE_PORT_ CONFIGURATION use
    ( HDX_TX1_RX1 => 62,
    HDX_TX2_RX2 => 23,
    HDX_TX1_RX2 => 34,
    HDX_TX2_RX1 => 45,
    FDX_TX1_RX2 => 54,
    FDX_TX2_RX1 => 61);


  • Brad BARCLAY

    #2
    Re: Constrained ada type in Java

    Ed Trubia wrote:[color=blue]
    > Have the following type that applies contraints. How could this be easily
    > coded in Java[/color]

    There isn't really an elegant way of handling such things in Java as
    you can in Ada.

    For type constraining, you'll probably want to create a class which
    manages these constraints. Thus, if you want an integer type that can
    only hold the ranges -45..150 (say, for storing temperature data),
    create a class to hold it and ensure that the value it holds doesn't
    exceed on underceed that range. For example:

    public class TemperatureData {
    private int temp = 0;

    public TemperatureData (int t) throws OutOfBoundsExce ption {
    setTemp(t);
    }

    public setTemp(int t) throws OutOfBoundsExce ption {
    if (t<-45 || t>150) throw new OutOfBoundsExce ption;
    else temp=t;
    }

    public getTemp() {
    return temp;
    }
    }

    (Note that you'd have to also define OutOfBoundsExce ption, as it
    doesn't already exist).

    To handle method-level constraints, you'll have to test each variable
    you want to constrain at the beginning of the method call, and then
    throw an exception if it's out-of-bounds. The setTemp() method above
    does just this.

    As I said, there isn't any nice way to do this like there is in Ada. I
    was never completely sold on Ada, but this is one of the things I miss
    (along with being able to call functions with your parameters in
    whatever order you want to provide them in).

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org

    Comment

    Working...