By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,778 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,778 IT Pros & Developers. It's quick & easy.

Constrained ada type in Java

P: n/a
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);
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ed Trubia wrote:
Have the following type that applies contraints. How could this be easily
coded in Java


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 OutOfBoundsException {
setTemp(t);
}

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

public getTemp() {
return temp;
}
}

(Note that you'd have to also define OutOfBoundsException, 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
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.