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);
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations