Search in sources :

Example 1 with IonLob

use of com.amazon.ion.IonLob in project ion-java by amzn.

the class IonAssert method doAssertIonEquals.

// ========================================================================
private static void doAssertIonEquals(String path, IonValue expected, IonValue actual) {
    if (expected == actual)
        return;
    IonType expectedType = expected.getType();
    assertSame(path + " type", expectedType, actual.getType());
    assertEqualAnnotations(path, expected, actual);
    if (expected.isNullValue() || actual.isNullValue()) {
        assertEquals(path, expected, actual);
        return;
    }
    switch(expectedType) {
        case BOOL:
        case DECIMAL:
        case FLOAT:
        case INT:
        case NULL:
        case STRING:
        case SYMBOL:
        case TIMESTAMP:
            {
                // "Normal" IonValue.equals()
                assertEquals(path + " IonValue", expected, actual);
                break;
            }
        case BLOB:
        case CLOB:
            {
                assertArrayEquals(path, ((IonLob) expected).getBytes(), ((IonLob) actual).getBytes());
                break;
            }
        // user data, not system data.
        case DATAGRAM:
        case LIST:
        case SEXP:
            {
                assertSequenceEquals(path, (IonSequence) expected, (IonSequence) actual);
                break;
            }
        case STRUCT:
            {
                assertStructEquals(path, (IonStruct) expected, (IonStruct) actual);
                break;
            }
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonType(com.amazon.ion.IonType) IonLob(com.amazon.ion.IonLob) IonSequence(com.amazon.ion.IonSequence)

Example 2 with IonLob

use of com.amazon.ion.IonLob in project ion-java by amzn.

the class Equivalence method ionCompareToImpl.

private static int ionCompareToImpl(final IonValue v1, final IonValue v2, final Configuration configuration) {
    int result = 0;
    if (v1 == null || v2 == null) {
        if (v1 != null)
            result = 1;
        if (v2 != null)
            result = -1;
        // otherwise v1 == v2 == null and result == 0
        return result;
    }
    // check type
    IonType ty1 = v1.getType();
    IonType ty2 = v2.getType();
    result = ty1.compareTo(ty2);
    if (result == 0) {
        boolean bo1 = v1.isNullValue();
        boolean bo2 = v2.isNullValue();
        if (bo1 || bo2) {
            // the same type
            if (!bo1)
                result = 1;
            if (!bo2)
                result = -1;
        // othersize they're equal (and null values)
        } else {
            // value compare only if both are not null
            switch(ty1) {
                case NULL:
                    // never visited, precondition is that both are not null
                    break;
                case BOOL:
                    if (((IonBool) v1).booleanValue()) {
                        result = ((IonBool) v2).booleanValue() ? 0 : 1;
                    } else {
                        result = ((IonBool) v2).booleanValue() ? -1 : 0;
                    }
                    break;
                case INT:
                    result = ((IonInt) v1).bigIntegerValue().compareTo(((IonInt) v2).bigIntegerValue());
                    break;
                case FLOAT:
                    double double1 = ((IonFloat) v1).doubleValue();
                    double double2 = ((IonFloat) v2).doubleValue();
                    if (configuration.epsilon != null && (double1 == double2 || Math.abs(double1 - double2) <= configuration.epsilon)) {
                        result = 0;
                    } else {
                        result = Double.compare(double1, double2);
                    }
                    break;
                case DECIMAL:
                    // TODO amzn/ion-java/issues/26
                    assert !PUBLIC_COMPARISON_API;
                    result = Decimal.equals(((IonDecimal) v1).decimalValue(), ((IonDecimal) v2).decimalValue()) ? 0 : 1;
                    break;
                case TIMESTAMP:
                    if (configuration.isStrict) {
                        // TODO amzn/ion-java/issues/26
                        assert !PUBLIC_COMPARISON_API;
                        result = (((IonTimestamp) v1).timestampValue().equals(((IonTimestamp) v2).timestampValue()) ? 0 : 1);
                    } else {
                        // This is kind of lying here, the 'strict' boolean
                        // (if false) denotes ONLY that annotations are not
                        // check for equality. But what this is doing here is
                        // that it is also ignoring IonTimesamps' precision and
                        // local offset.
                        result = ((IonTimestamp) v1).timestampValue().compareTo(((IonTimestamp) v2).timestampValue());
                    }
                    break;
                case STRING:
                    result = (((IonText) v1).stringValue()).compareTo(((IonText) v2).stringValue());
                    break;
                case SYMBOL:
                    result = compareSymbolTokens(((IonSymbol) v1).symbolValue(), ((IonSymbol) v2).symbolValue());
                    break;
                case BLOB:
                case CLOB:
                    result = compareLobContents((IonLob) v1, (IonLob) v2);
                    break;
                case STRUCT:
                    // TODO amzn/ion-java/issues/26
                    assert !PUBLIC_COMPARISON_API;
                    result = compareStructs((IonStruct) v1, (IonStruct) v2, configuration);
                    break;
                case LIST:
                case SEXP:
                case DATAGRAM:
                    result = compareSequences((IonSequence) v1, (IonSequence) v2, configuration);
                    break;
            }
        }
    }
    // comparison, then we check the annotations
    if ((result == 0) && configuration.isStrict) {
        // check tuple equality over the annotations
        // (which are symbol tokens)
        result = compareAnnotations(v1.getTypeAnnotationSymbols(), v2.getTypeAnnotationSymbols());
    }
    return result;
}
Also used : IonType(com.amazon.ion.IonType) IonInt(com.amazon.ion.IonInt) IonSequence(com.amazon.ion.IonSequence) IonBool(com.amazon.ion.IonBool) IonSymbol(com.amazon.ion.IonSymbol) IonText(com.amazon.ion.IonText) IonStruct(com.amazon.ion.IonStruct) IonTimestamp(com.amazon.ion.IonTimestamp) IonLob(com.amazon.ion.IonLob) IonFloat(com.amazon.ion.IonFloat)

Example 3 with IonLob

use of com.amazon.ion.IonLob in project ion-java by amzn.

the class IonReaderTreeSystem method newBytes.

public byte[] newBytes() {
    if (_curr instanceof IonLob) {
        IonLob lob = (IonLob) _curr;
        int loblen = lob.byteSize();
        byte[] buffer = new byte[loblen];
        InputStream is = lob.newInputStream();
        int retlen;
        try {
            retlen = readFully(is, buffer, 0, loblen);
            is.close();
        } catch (IOException e) {
            throw new IonException(e);
        }
        assert (retlen == -1 ? loblen == 0 : retlen == loblen);
        return buffer;
    }
    throw new IllegalStateException("current value is not an ion blob or clob");
}
Also used : IonException(com.amazon.ion.IonException) IonLob(com.amazon.ion.IonLob) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 4 with IonLob

use of com.amazon.ion.IonLob in project ion-java by amzn.

the class IonReaderTreeSystem method getBytes.

public int getBytes(byte[] buffer, int offset, int len) {
    if (_curr instanceof IonLob) {
        IonLob lob = (IonLob) _curr;
        int loblen = lob.byteSize();
        if (loblen > len) {
            throw new IllegalArgumentException("insufficient space in buffer for this value");
        }
        InputStream is = lob.newInputStream();
        int retlen;
        try {
            retlen = readFully(is, buffer, 0, loblen);
            is.close();
        } catch (IOException e) {
            throw new IonException(e);
        }
        assert retlen == loblen;
        return retlen;
    }
    throw new IllegalStateException("current value is not an ion blob or clob");
}
Also used : IonException(com.amazon.ion.IonException) IonLob(com.amazon.ion.IonLob) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 5 with IonLob

use of com.amazon.ion.IonLob in project ion-java by amzn.

the class IonIteratorImpl method readValue.

private IonValue readValue() {
    IonType type = _reader.getType();
    SymbolToken[] annotations = _reader.getTypeAnnotationSymbols();
    IonValue v;
    if (_reader.isNullValue()) {
        v = _valueFactory.newNull(type);
    } else {
        switch(type) {
            case NULL:
                // Handled above
                throw new IllegalStateException();
            case BOOL:
                v = _valueFactory.newBool(_reader.booleanValue());
                break;
            case INT:
                v = _valueFactory.newInt(_reader.bigIntegerValue());
                break;
            case FLOAT:
                v = _valueFactory.newFloat(_reader.doubleValue());
                break;
            case DECIMAL:
                v = _valueFactory.newDecimal(_reader.decimalValue());
                break;
            case TIMESTAMP:
                v = _valueFactory.newTimestamp(_reader.timestampValue());
                break;
            case STRING:
                v = _valueFactory.newString(_reader.stringValue());
                break;
            case SYMBOL:
                // TODO always pass the SID?  Is it correct?
                v = _valueFactory.newSymbol(_reader.symbolValue());
                break;
            case BLOB:
                {
                    IonLob lob = _valueFactory.newNullBlob();
                    lob.setBytes(_reader.newBytes());
                    v = lob;
                    break;
                }
            case CLOB:
                {
                    IonLob lob = _valueFactory.newNullClob();
                    lob.setBytes(_reader.newBytes());
                    v = lob;
                    break;
                }
            case STRUCT:
                {
                    IonStruct struct = _valueFactory.newEmptyStruct();
                    _reader.stepIn();
                    while (_reader.next() != null) {
                        SymbolToken name = _reader.getFieldNameSymbol();
                        IonValue child = readValue();
                        struct.add(name, child);
                    }
                    _reader.stepOut();
                    v = struct;
                    break;
                }
            case LIST:
                {
                    IonSequence seq = _valueFactory.newEmptyList();
                    _reader.stepIn();
                    while (_reader.next() != null) {
                        IonValue child = readValue();
                        seq.add(child);
                    }
                    _reader.stepOut();
                    v = seq;
                    break;
                }
            case SEXP:
                {
                    IonSequence seq = _valueFactory.newEmptySexp();
                    _reader.stepIn();
                    while (_reader.next() != null) {
                        IonValue child = readValue();
                        seq.add(child);
                    }
                    _reader.stepOut();
                    v = seq;
                    break;
                }
            default:
                throw new IllegalStateException();
        }
    }
    // TODO this is too late in the case of system reading
    // when v is a local symtab (it will get itself, not the prior symtab)
    SymbolTable symtab = _reader.getSymbolTable();
    ((_Private_IonValue) v).setSymbolTable(symtab);
    if (annotations.length != 0) {
        ((_Private_IonValue) v).setTypeAnnotationSymbols(annotations);
    }
    return v;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonLob(com.amazon.ion.IonLob) IonSequence(com.amazon.ion.IonSequence) SymbolTable(com.amazon.ion.SymbolTable)

Aggregations

IonLob (com.amazon.ion.IonLob)7 IonSequence (com.amazon.ion.IonSequence)3 IonStruct (com.amazon.ion.IonStruct)3 IonType (com.amazon.ion.IonType)3 IonDatagram (com.amazon.ion.IonDatagram)2 IonException (com.amazon.ion.IonException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 IonBlob (com.amazon.ion.IonBlob)1 IonBool (com.amazon.ion.IonBool)1 IonFloat (com.amazon.ion.IonFloat)1 IonInt (com.amazon.ion.IonInt)1 IonSymbol (com.amazon.ion.IonSymbol)1 IonText (com.amazon.ion.IonText)1 IonTimestamp (com.amazon.ion.IonTimestamp)1 IonValue (com.amazon.ion.IonValue)1 SymbolTable (com.amazon.ion.SymbolTable)1 SymbolToken (com.amazon.ion.SymbolToken)1