From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,URI_TRY_3LD autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,950b357d6db67d4e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-16 21:56:07 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!news.gtei.net!newsfeed1.easynews.com!easynews.com!easynews!c03.atl99!news.webusenet.com!news02.tsnz.net!news.iconz.co.nz From: Craig Carey Newsgroups: comp.lang.ada Subject: Re: Contrained ada type in java Message-ID: References: X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Original-NNTP-Posting-Host: ip-210-185-36-222.internet.co.nz X-Original-Trace: 17 Dec 2003 18:55:43 +1300, ip-210-185-36-222.internet.co.nz Organization: "ICONZ Ltd." Date: Wed, 17 Dec 2003 18:56:06 +1300 NNTP-Posting-Host: 210.48.22.5 X-Complaints-To: abuse@tsnz.net X-Trace: news02.tsnz.net 1071640566 210.48.22.5 (Wed, 17 Dec 2003 18:56:06 NZDT) NNTP-Posting-Date: Wed, 17 Dec 2003 18:56:06 NZDT Xref: archiver1.google.com comp.lang.ada:3497 Date: 2003-12-17T18:56:06+13:00 List-Id: On Wed, 10 Dec 2003 21:56:10 -0600, "David C. Hoos, Sr." wrote: >This is an enumerated type, a feature of many >languages, but not of Java. > >There are a number of ways to deal with this in Java > >See the following web site for some suggestions: >http://gethelp.devx.com/techtips/java_pro/10MinuteSolutions/10min0600.asp > >Of course, the best solution is to leave the program in Ada! > Mr Trubia explained in an earlier message that Ada was being ported to Java. It seems suspect. What is the private company, Lockheed Martin, up to ?. Is Lockheed going to produce an Ada to Java parser. I used the search engine at the Lockheen Martin website and it seemed to identify only the US Navy (subsea machines) as producing the interest in Java. Ada allows this: "type e1 is (a, b, c); type e2 is (a, b, d);", etc. Here is the ...["dogfood Sun won't eat"] version | Now we can guarantee that a Stereo instance can only have a Volume | equal to one of the discrete levels we have defined (and null). The | problem with this approach is that now we have lost the ordering of | the values. We also can no longer manipulate the values as integers, | disallowing their use in a switch statement. A natural addition | would be to add a member variable to Volume defining the ordering of | the levels as follows: | | public final class Volume implements Comparable { | public static final int SILENT_LEVEL = 0; | public static final int SOFT_LEVEL = 1; | public static final int AUDIBLE_LEVEL = 2; | public static final int LOUD_LEVEL = 3; | public static final int ELEVEN_LEVEL = 4; | | public static final Volume SILENT = new Volume(SILENT_LEVEL); | public static final Volume SOFT = new Volume(SOFT_LEVEL); | public static final Volume AUDIBLE = new Volume(AUDIBLE_LEVEL); | public static final Volume LOUD = new Volume(LOUD_LEVEL); | public static final Volume ELEVEN = new Volume(ELEVEN_LEVEL); | | private static final Volume __VOLUMES[] = { | SILENT, SOFT, AUDIBLE, LOUD, ELEVEN | }; | | private int __level; | | public static final Volume getVolume(int level) { | if(level >= SILENT_LEVEL && level <= ELEVEN_LEVEL) | return __VOLUMES[level]; | return null; | } | | private Volume(int level) { | __level = level; | } | | public boolean equals(Object obj) { | // Assume proper type was given | return (__level == ((Volume)obj).__level); | } | | public int compareTo(Object obj) { | // Assume proper type was given | int other = ((Volume)obj).__level; | if(__level == other) | return 0; | if(__level < other) | return -1; | return 1; | } | | public int getLevel() { return __level; } | } | | This solves the ordering problem and the manipulation of the values | as integers. You can determine ordering with equals() and | compareTo(). And if you want to use the values in a switch | statement, you can fetch a value to test with getLevel() and use the | LEVEL constants as the cases. You may be wondering why we added the | getVolume() factory method. If we omit the factory method, a small | problem arises with respect to serialization. The Volume class has a | private constructor and no setter methods so that we may limit the | range of values it can represent. This also makes it unserializable. | A serializable class must declare Volume variables as transient, and | manually store the getLevel() value. On deserialization, the class | may recreate its Volume member by using the getVolume() factory | method. A benefit of this approach is that it allows you to continue | to use the == operator for comparison operations, rather than | restrict you to invoking equals(). Another way to deal with | serialization to create a readRsolve() method like the following: | | private Object readResolve() throws ObjectStreamException { | return __VOLUMES[__level]; | } | | This prevents duplicate constants from being created during | deserialization. | | It is quite a lot of work just to get the functionality of | enumerated types. Simply using integer constants will be sufficient | for most programs, but when value restriction and type safety become | issues, you will have to resort to some variation of the more | complicated solution. | If the Navy is Java enthusiast then they must have rules that prevent a rational approach that leads to: (a) a quality (no murky Sun bugs) implies that a 3GL be used (b) the existing 3GL is Ada so that would be embedded (c) most of the existing Ada is left unchanged and what was required to be a Java project is best implemented with Java being a bindings shell. It might be some document requestable under US FOI laws. Still the Navy is unsoundly identified. Now that Mr Ed Trubia is sending in multiple messages on Java, I hope he can name the documents that could be requested under FOI provisions. Craig Carey