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