corba/src/share/classes/com/sun/corba/se/impl/dynamicany/DynFixedImpl.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.dynamicany;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import org.omg.CORBA.TypeCode;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
import org.omg.CORBA.Any;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import org.omg.CORBA.NO_IMPLEMENT;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import org.omg.DynamicAny.*;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import org.omg.DynamicAny.DynAnyPackage.*;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import java.math.BigDecimal;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.math.BigInteger;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import org.omg.CORBA.TypeCodePackage.BadKind;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import com.sun.corba.se.spi.orb.ORB ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import com.sun.corba.se.spi.logging.CORBALogDomains ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
public class DynFixedImpl extends DynAnyBasicImpl implements DynFixed
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
    // Constructors
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
    private DynFixedImpl() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
        this(null, (Any)null, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
    protected DynFixedImpl(ORB orb, Any any, boolean copyValue) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
        super(orb, any, copyValue);
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
    // Sets the current position to -1 and the value to zero.
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    protected DynFixedImpl(ORB orb, TypeCode typeCode) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
        super(orb, typeCode);
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
        index = NO_INDEX;
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    // DynAny interface methods
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
    public int component_count() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
        return 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
*/
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
    // DynFixed interface methods
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
    public String get_value () {
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
        if (status == STATUS_DESTROYED) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
            throw wrapper.dynAnyDestroyed() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
        return any.extract_fixed().toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
    // Initializes the value of the DynFixed.
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
    // The val string must contain a fixed string constant in the same format
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
    // as used for IDL fixed-point literals.
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
    // It may consist of an integer part, an optional decimal point,
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
    // a fraction part and an optional letter d or D.
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
    // The integer and fraction parts both must be sequences of decimal (base 10) digits.
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
    // Either the integer part or the fraction part, but not both, may be missing.
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
    // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
    // the operation raises InvalidValue.
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
    // The return value is true if val can be represented as the DynFixed without loss of precision.
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
    // If val has more fractional digits than can be represented in the DynFixed,
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
    // fractional digits are truncated and the return value is false.
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
    // If val does not contain a valid fixed-point literal or contains extraneous characters
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
    // other than leading or trailing white space, the operation raises TypeMismatch.
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
    public boolean set_value (String val)
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
        throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
               org.omg.DynamicAny.DynAnyPackage.InvalidValue
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
        if (status == STATUS_DESTROYED) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
            throw wrapper.dynAnyDestroyed() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        int digits = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
        int scale = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        boolean preservedPrecision = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
            digits = any.type().fixed_digits();
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            scale = any.type().fixed_scale();
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
        } catch (BadKind ex) { // impossible
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
        // First get rid of leading or trailing whitespace which is allowed
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        String string = val.trim();
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
        if (string.length() == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
            throw new TypeMismatch();
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
        // Now scan for the sign
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
        String sign = "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
        if (string.charAt(0) == '-') {
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
            sign = "-";
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
            string = string.substring(1);
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
        } else if (string.charAt(0) == '+') {
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
            sign = "+";
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
            string = string.substring(1);
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
        // Now get rid of the letter d or D.
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
        int dIndex = string.indexOf('d');
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
        if (dIndex == -1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
            dIndex = string.indexOf('D');
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
        if (dIndex != -1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
            string = string.substring(0, dIndex);
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
        // Just to be sure
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        if (string.length() == 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
            throw new TypeMismatch();
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
        // Now look for the dot to determine the integer part
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
        String integerPart;
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
        String fractionPart;
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
        int currentScale;
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
        int currentDigits;
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
        int dotIndex = string.indexOf('.');
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
        if (dotIndex == -1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
            integerPart = string;
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
            fractionPart = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
            currentScale = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
            currentDigits = integerPart.length();
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
        } else if (dotIndex == 0 ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
            integerPart = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
            fractionPart = string;
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
            currentScale = fractionPart.length();
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
            currentDigits = currentScale;
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
            integerPart = string.substring(0, dotIndex);
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
            fractionPart = string.substring(dotIndex + 1);
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
            currentScale = fractionPart.length();
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
            currentDigits = integerPart.length() + currentScale;
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
        // Let's see if we have to drop some precision
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
        if (currentDigits > digits) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
            preservedPrecision = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
            // truncate the fraction part
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
            if (integerPart.length() < digits) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
                fractionPart = fractionPart.substring(0, digits - integerPart.length());
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
            } else if (integerPart.length() == digits) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
                // currentScale > 0
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
                // drop the fraction completely
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
                fractionPart = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
                // integerPart.length() > digits
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
                // unable to truncate fraction part
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
                throw new InvalidValue();
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        // If val contains a value whose scale exceeds that of the DynFixed or is not initialized,
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
        // the operation raises InvalidValue.
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
        // Reinterpreted to mean raise InvalidValue only if the integer part exceeds precision,
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
        // which is handled above (integerPart.length() > digits)
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        if (currentScale > scale) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
            throw new InvalidValue("Scale exceeds " + scale);
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
*/
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
        // Now check whether both parts are valid numbers
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
        BigDecimal result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
            new BigInteger(integerPart);
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
            if (fractionPart == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
                result = new BigDecimal(sign + integerPart);
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
                new BigInteger(fractionPart);
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
                result = new BigDecimal(sign + integerPart + "." + fractionPart);
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        } catch (NumberFormatException nfe) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
            throw new TypeMismatch();
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
        any.insert_fixed(result, any.type());
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
        return preservedPrecision;
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
    public String toString() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
        int digits = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
        int scale = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
            digits = any.type().fixed_digits();
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
            scale = any.type().fixed_scale();
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
        } catch (BadKind ex) { // impossible
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
        return "DynFixed with value=" + this.get_value() + ", digits=" + digits + ", scale=" + scale;
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
}