Re: Constrained ada type in Java

From: Brad BARCLAY (bbarclay_at_jsyncmanager.org)
Date: 12/11/03


Date: Thu, 11 Dec 2003 04:10:41 GMT

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


Relevant Pages

  • Re: PL/I, COBOL, Advantages, Equivalence, et al
    ... apply flow analysis to the constraints. ... While I prefer PL/I, there are definitely things in Ada ... and constraining variables to specific ranges is one ...
    (comp.lang.pl1)
  • Re: Ada.Strings.Bounded
    ... I assert that that Ada as currently defined has no bound ... delineated by ranges given by static simple_expression. ... There's no length limitation on an expression in the language, ... exhibit a bound on universal_integer as a counterexample, ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... >> ranges, but it can't definitely be done without chechinkg array ... > compact as the Ada counterpart. ... > bool assign(const int& i) ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... >> ranges, but it can't definitely be done without chechinkg array ... > compact as the Ada counterpart. ... > bool assign(const int& i) ...
    (comp.lang.cpp)
  • Re: A simple ADA puzzle (I havent the answer)
    ... But this programmer does understand what arrays and types are, ... just not how they manifest in Ada. ... And that bit of code with constraints that eats ...
    (comp.lang.ada)