Search in sources :

Example 56 with SymbolToken

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

the class IonStructLite method hashCode.

// 
// updateFieldName is unnecessary since field names are immutable
// (except when the value is unattached to any struct)
// 
// protected void updateFieldName(String oldname, String name, IonValue field)
// {
// assert(name != null && name.equals(field.getFieldName()));
// 
// if (oldname == null) return;
// if (_field_map == null) return;
// 
// Integer idx = _field_map.get(oldname);
// if (idx == null) return;
// 
// IonValue oldfield = get_child(idx);
// 
// // yes, we want object identity in this test
// if (oldfield == field) {
// remove_field(oldname, idx);
// add_field(name, idx);
// }
// }
/**
 * Implements {@link Object#hashCode()} consistent with equals.
 * This is insensitive to order of fields.
 * <p>
 * This method must follow the contract of {@link Object#equals(Object)},
 * which is located at {@link Equivalence#ionEquals(IonValue, IonValue)}.
 *
 * @return  An int, consistent with the contracts for
 *          {@link Object#hashCode()} and {@link Object#equals(Object)}.
 */
@Override
int hashCode(SymbolTableProvider symbolTableProvider) {
    // prime to salt name of each Field
    final int nameHashSalt = 16777619;
    // prime to salt value of each Field
    final int valueHashSalt = 8191;
    // prime to salt sid of fieldname
    final int sidHashSalt = 127;
    // prime to salt text of fieldname
    final int textHashSalt = 31;
    int result = HASH_SIGNATURE;
    if (!isNullValue()) {
        for (IonValue v : this) {
            IonValueLite vlite = (IonValueLite) v;
            // If fieldname's text is unknown, use its sid instead
            SymbolToken token = vlite.getFieldNameSymbol(symbolTableProvider);
            String text = token.getText();
            int nameHashCode = text == null ? token.getSid() * sidHashSalt : text.hashCode() * textHashSalt;
            // mixing to account for small text and sid deltas
            nameHashCode ^= (nameHashCode << 17) ^ (nameHashCode >> 15);
            int fieldHashCode = HASH_SIGNATURE;
            fieldHashCode = valueHashSalt * fieldHashCode + vlite.hashCode(symbolTableProvider);
            fieldHashCode = nameHashSalt * fieldHashCode + nameHashCode;
            // another mix step for each Field of the struct
            fieldHashCode ^= (fieldHashCode << 19) ^ (fieldHashCode >> 13);
            // Additive hash is used to ensure insensitivity to order of
            // fields, and will not lose data on value hash codes
            result += fieldHashCode;
        }
    }
    return hashTypeAnnotations(result, symbolTableProvider);
}
Also used : IonValue(com.amazon.ion.IonValue) SymbolToken(com.amazon.ion.SymbolToken)

Example 57 with SymbolToken

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

the class IonStructLite method build_field_map.

protected void build_field_map() {
    int size = (_children == null) ? 0 : _children.length;
    _field_map = new HashMap<String, Integer>(size);
    _field_map_duplicate_count = 0;
    int count = get_child_count();
    for (int ii = 0; ii < count; ii++) {
        IonValueLite v = get_child(ii);
        SymbolToken fieldNameSymbol = v.getFieldNameSymbol();
        String name = fieldNameSymbol.getText();
        if (_field_map.get(name) != null) {
            _field_map_duplicate_count++;
        }
        // this causes the map to have the largest index value stored
        _field_map.put(name, ii);
    }
    return;
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 58 with SymbolToken

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

the class IonStructLite method doClone.

private IonStruct doClone(boolean keep, String... fieldNames) {
    IonStruct clone;
    if (isNullValue()) {
        clone = getSystem().newNullStruct();
    } else {
        Set<String> fields = new HashSet<String>(Arrays.asList(fieldNames));
        if (keep && fields.contains(null)) {
            throw new NullPointerException("Can't retain an unknown field name");
        }
        clone = getSystem().newEmptyStruct();
        for (IonValue value : this) {
            SymbolToken fieldNameSymbol = value.getFieldNameSymbol();
            String fieldName = fieldNameSymbol.getText();
            if (fields.contains(fieldName) == keep) {
                // This ensures that we don't copy an unknown field name.
                fieldName = value.getFieldName();
                clone.add(fieldName, value.clone());
            }
        }
    }
    clone.setTypeAnnotationSymbols(getTypeAnnotationSymbols());
    return clone;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) SymbolToken(com.amazon.ion.SymbolToken) HashSet(java.util.HashSet)

Example 59 with SymbolToken

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

the class IonSymbolLite method hashCode.

@Override
int hashCode(SymbolTableProvider symbolTableProvider) {
    // prime to salt sid
    final int sidHashSalt = 127;
    // prime to salt text
    final int textHashSalt = 31;
    int result = HASH_SIGNATURE;
    if (!isNullValue()) {
        SymbolToken token = symbolValue(symbolTableProvider);
        String text = token.getText();
        int tokenHashCode = text == null ? token.getSid() * sidHashSalt : text.hashCode() * textHashSalt;
        // mixing to account for small text and sid deltas
        tokenHashCode ^= (tokenHashCode << 29) ^ (tokenHashCode >> 3);
        result ^= tokenHashCode;
    }
    return hashTypeAnnotations(result, symbolTableProvider);
}
Also used : SymbolToken(com.amazon.ion.SymbolToken)

Example 60 with SymbolToken

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

the class IonManagedBinaryWriter method intern.

private SymbolToken intern(final String text) {
    if (text == null) {
        return null;
    }
    try {
        SymbolToken token = imports.importedSymbols.get(text);
        if (token != null) {
            if (token.getSid() > ION_1_0_MAX_ID) {
                // using a symbol from an import triggers emitting locals
                startLocalSymbolTableIfNeeded(/*writeIVM*/
                true);
            }
            return token;
        }
        // try the locals
        token = locals.get(text);
        if (token == null) {
            if (localsLocked) {
                throw new IonException("Local symbol table was locked (made read-only)");
            }
            // if we got here, this is a new symbol and we better start up the locals
            startLocalSymbolTableIfNeeded(/*writeIVM*/
            true);
            startLocalSymbolTableSymbolListIfNeeded();
            token = symbol(text, imports.localSidStart + locals.size());
            locals.put(text, token);
            symbols.writeString(text);
        }
        return token;
    } catch (final IOException e) {
        throw new IonException("Error synthesizing symbols", e);
    }
}
Also used : SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) IOException(java.io.IOException)

Aggregations

SymbolToken (com.amazon.ion.SymbolToken)68 SymbolTable (com.amazon.ion.SymbolTable)14 com.amazon.ion.impl._Private_Utils.newSymbolToken (com.amazon.ion.impl._Private_Utils.newSymbolToken)13 IonType (com.amazon.ion.IonType)10 IonValue (com.amazon.ion.IonValue)10 IonException (com.amazon.ion.IonException)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 IonStruct (com.amazon.ion.IonStruct)4 IOException (java.io.IOException)4 Event (com.amazon.tools.events.Event)3 EventType (com.amazon.tools.events.EventType)3 FakeSymbolToken (com.amazon.ion.FakeSymbolToken)2 IonDatagram (com.amazon.ion.IonDatagram)2 IonSequence (com.amazon.ion.IonSequence)2 IonString (com.amazon.ion.IonString)2 UnknownSymbolException (com.amazon.ion.UnknownSymbolException)2 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)2 ImportDescriptor (com.amazon.tools.events.ImportDescriptor)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2