use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonValueLite method writeTo.
final void writeTo(IonWriter writer, SymbolTableProvider symbolTableProvider) {
if (writer.isInStruct() && !((_Private_IonWriter) writer).isFieldNameSet()) {
SymbolToken tok = getFieldNameSymbol(symbolTableProvider);
if (tok == null) {
throw new IllegalStateException("Field name not set");
}
writer.setFieldNameSymbol(tok);
}
SymbolToken[] annotations = getTypeAnnotationSymbols(symbolTableProvider);
writer.setTypeAnnotationSymbols(annotations);
try {
writeBodyTo(writer, symbolTableProvider);
} catch (IOException e) {
throw new IonException(e);
}
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonValueLite method getFieldNameSymbol.
public final SymbolToken getFieldNameSymbol(SymbolTableProvider symbolTableProvider) {
int sid = _fieldId;
String text = _fieldName;
if (text != null) {
if (sid == UNKNOWN_SYMBOL_ID) {
SymbolToken tok = symbolTableProvider.getSymbolTable().find(text);
if (tok != null) {
return tok;
}
}
} else if (sid > 0) {
text = symbolTableProvider.getSymbolTable().findKnownSymbol(sid);
} else if (sid != 0) {
// not a struct field
return null;
}
return _Private_Utils.newSymbolToken(text, sid);
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonValueLite method attemptClearSymbolIDValues.
/**
* Sets this value's symbol table to null, and erases any SIDs here and
* recursively.
*
* @return true if all SID's have been successfully removed, otherwise false
*/
boolean attemptClearSymbolIDValues() {
boolean sidsRemain = false;
if (_fieldName != null) {
_fieldId = UNKNOWN_SYMBOL_ID;
} else if (_fieldId > UNKNOWN_SYMBOL_ID) {
// retaining the field SID, as it couldn't be cleared due to loss of context
// TODO - for SID handling consistency; this should attempt resolution first
sidsRemain = true;
}
if (_annotations != null) {
for (int i = 0; i < _annotations.length; i++) {
SymbolToken annotation = _annotations[i];
// _annotations may have nulls at the end.
if (annotation == null)
break;
String text = annotation.getText();
// TODO - for SID handling consistency; this should attempt resolution first, not just drop entry
if (text != null && annotation.getSid() != UNKNOWN_SYMBOL_ID) {
_annotations[i] = newSymbolToken(text, UNKNOWN_SYMBOL_ID);
}
}
}
return !sidsRemain;
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonValueLite method getTypeAnnotationSymbols.
public final SymbolToken[] getTypeAnnotationSymbols(SymbolTableProvider symbolTableProvider) {
// first we have to count the number of non-null
// elements there are in the annotations array
int count = 0;
if (_annotations != null) {
for (int i = 0; i < _annotations.length; i++) {
if (_annotations[i] != null) {
count++;
}
}
}
// if there aren't any, we're done
if (count == 0) {
return SymbolToken.EMPTY_ARRAY;
}
SymbolToken[] users_copy = new SymbolToken[count];
for (int i = 0; i < count; i++) {
SymbolToken token = _annotations[i];
String text = token.getText();
if (text != null && token.getSid() == UNKNOWN_SYMBOL_ID) {
// TODO amzn/ion-java/issues/27 We should memoize the result of symtab lookups
// into _annotations.
// See getFieldNameSymbol() for challenges doing so.
SymbolToken interned = symbolTableProvider.getSymbolTable().find(text);
if (interned != null) {
token = interned;
}
}
users_copy[i] = token;
}
return users_copy;
}
use of com.amazon.ion.SymbolToken in project ion-java by amzn.
the class IonValueLite method hashTypeAnnotations.
protected int hashTypeAnnotations(final int original, SymbolTableProvider symbolTableProvider) {
final SymbolToken[] tokens = getTypeAnnotationSymbols(symbolTableProvider);
if (tokens.length == 0) {
return original;
}
// prime to salt sid of annotation
final int sidHashSalt = 127;
// prime to salt text of annotation
final int textHashSalt = 31;
final int prime = 8191;
int result = original ^ TYPE_ANNOTATION_HASH_SIGNATURE;
result = prime * original + tokens.length;
for (final SymbolToken token : tokens) {
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 << 19) ^ (tokenHashCode >> 13);
result = prime * result + tokenHashCode;
// mixing at each step to make the hash code order-dependent
result ^= (result << 25) ^ (result >> 7);
}
return result;
}
Aggregations