1 /*
   2  * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.dynamicany;
  27 
  28 import org.omg.CORBA.TypeCode;
  29 import org.omg.CORBA.Any;
  30 import org.omg.CORBA.BAD_OPERATION;
  31 import org.omg.CORBA.INTERNAL;
  32 import org.omg.CORBA.TypeCodePackage.BadKind;
  33 import org.omg.CORBA.TypeCodePackage.Bounds;
  34 import org.omg.DynamicAny.*;
  35 import org.omg.DynamicAny.DynAnyPackage.*;
  36 
  37 import com.sun.corba.se.spi.orb.ORB ;
  38 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  39 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  40 
  41 public class DynEnumImpl extends DynAnyBasicImpl implements DynEnum
  42 {
  43     //
  44     // Instance variables
  45     //
  46 
  47     // This int and the any value are kept in sync at all times
  48     int currentEnumeratorIndex = NO_INDEX;
  49 
  50     //
  51     // Constructors
  52     //
  53 
  54     private DynEnumImpl() {
  55         this(null, (Any)null, false);
  56     }
  57 
  58     // The current position of a DynEnum is always -1.
  59     protected DynEnumImpl(ORB orb, Any anAny, boolean copyValue) {
  60         super(orb, anAny, copyValue);
  61         index = NO_INDEX;
  62         // The any doesn't have to be initialized. We have a default value in this case.
  63         try {
  64             currentEnumeratorIndex = any.extract_long();
  65         } catch (BAD_OPERATION e) {
  66             // _REVISIT_: Fix Me
  67             currentEnumeratorIndex = 0;
  68             any.type(any.type());
  69             any.insert_long(0);
  70         }
  71     }
  72 
  73     // Sets the current position to -1 and sets the value of the enumerator
  74     // to the first enumerator value indicated by the TypeCode.
  75     protected DynEnumImpl(ORB orb, TypeCode typeCode) {
  76         super(orb, typeCode);
  77         index = NO_INDEX;
  78         currentEnumeratorIndex = 0;
  79         any.insert_long(0);
  80     }
  81 
  82     //
  83     // Utility methods
  84     //
  85 
  86     private int memberCount() {
  87         int memberCount = 0;
  88         try {
  89             memberCount = any.type().member_count();
  90         } catch (BadKind bad) {
  91         }
  92         return memberCount;
  93     }
  94 
  95     private String memberName(int i) {
  96         String memberName = null;
  97         try {
  98             memberName = any.type().member_name(i);
  99         } catch (BadKind bad) {
 100         } catch (Bounds bounds) {
 101         }
 102         return memberName;
 103     }
 104 
 105     private int computeCurrentEnumeratorIndex(String value) {
 106         int memberCount = memberCount();
 107         for (int i=0; i<memberCount; i++) {
 108             if (memberName(i).equals(value)) {
 109                 return i;
 110             }
 111         }
 112         return NO_INDEX;
 113     }
 114 
 115     //
 116     // DynAny interface methods
 117     //
 118 
 119     // Returns always 0 for DynEnum
 120     public int component_count() {
 121         return 0;
 122     }
 123 
 124     // Calling current_component on a DynAny that cannot have components,
 125     // such as a DynEnum or an empty exception, raises TypeMismatch.
 126     public org.omg.DynamicAny.DynAny current_component()
 127         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch
 128     {
 129         if (status == STATUS_DESTROYED) {
 130             throw wrapper.dynAnyDestroyed() ;
 131         }
 132         throw new TypeMismatch();
 133     }
 134 
 135     //
 136     // DynEnum interface methods
 137     //
 138 
 139     // Returns the value of the DynEnum as an IDL identifier.
 140     public String get_as_string () {
 141         if (status == STATUS_DESTROYED) {
 142             throw wrapper.dynAnyDestroyed() ;
 143         }
 144         return memberName(currentEnumeratorIndex);
 145     }
 146 
 147     // Sets the value of the DynEnum to the enumerated value
 148     // whose IDL identifier is passed in the value parameter.
 149     // If value contains a string that is not a valid IDL identifier
 150     // for the corresponding enumerated type, the operation raises InvalidValue.
 151     public void set_as_string (String value)
 152         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
 153     {
 154         if (status == STATUS_DESTROYED) {
 155             throw wrapper.dynAnyDestroyed() ;
 156         }
 157         int newIndex = computeCurrentEnumeratorIndex(value);
 158         if (newIndex == NO_INDEX) {
 159             throw new InvalidValue();
 160         }
 161         currentEnumeratorIndex = newIndex;
 162         any.insert_long(newIndex);
 163     }
 164 
 165     // Returns the value of the DynEnum as the enumerated values ordinal value.
 166     // Enumerators have ordinal values 0 to n-1,
 167     // as they appear from left to right in the corresponding IDL definition.
 168     public int get_as_ulong () {
 169         if (status == STATUS_DESTROYED) {
 170             throw wrapper.dynAnyDestroyed() ;
 171         }
 172         return currentEnumeratorIndex;
 173     }
 174 
 175     // Sets the value of the DynEnum as the enumerated values ordinal value.
 176     // If value contains a value that is outside the range of ordinal values
 177     // for the corresponding enumerated type, the operation raises InvalidValue.
 178     public void set_as_ulong (int value)
 179         throws org.omg.DynamicAny.DynAnyPackage.InvalidValue
 180     {
 181         if (status == STATUS_DESTROYED) {
 182             throw wrapper.dynAnyDestroyed() ;
 183         }
 184         if (value < 0 || value >= memberCount()) {
 185             throw new InvalidValue();
 186         }
 187         currentEnumeratorIndex = value;
 188         any.insert_long(value);
 189     }
 190 }