use of com.amazon.ion.IonSymbol 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;
}
use of com.amazon.ion.IonSymbol in project ion-java by amzn.
the class IonWriterSystemTree method writeSymbolAsIs.
@Override
public void writeSymbolAsIs(String value) {
IonSymbol v = _factory.newSymbol(value);
append(v);
}
use of com.amazon.ion.IonSymbol in project ion-java by amzn.
the class IonWriterSystemTree method writeSymbolAsIs.
@Override
void writeSymbolAsIs(int symbolId) {
String name = getSymbolTable().findKnownSymbol(symbolId);
SymbolTokenImpl is = new SymbolTokenImpl(name, symbolId);
IonSymbol v = _factory.newSymbol(is);
append(v);
}
use of com.amazon.ion.IonSymbol in project ion-java by amzn.
the class _Private_CurriedValueFactory method newSymbol.
public IonSymbol newSymbol(String value) {
IonSymbol v = myFactory.newSymbol(value);
handle(v);
return v;
}
use of com.amazon.ion.IonSymbol in project ion-java by amzn.
the class PrinterTest method testPrintingSymbol.
@Test
public void testPrintingSymbol() throws Exception {
IonSymbol value = system().newNullSymbol();
checkNullRendering("null.symbol", value);
value.setValue("Adam E");
checkRendering("'Adam E'", value);
// Symbols that look like keywords.
value.setValue("null");
checkRendering("'null'", value);
value.setValue("true");
checkRendering("'true'", value);
value.setValue("false");
checkRendering("'false'", value);
value.setValue("null.int");
checkRendering("'null.int'", value);
// Operators standalone
value.setValue("%");
checkRendering("'%'", value);
value.setValue("Oh, \"Hello!\"");
checkRendering("'Oh, \"Hello!\"'", value);
// not: checkRendering("'Oh, \\\"Hello!\\\"'", value);
myPrinter.setPrintSymbolAsString(true);
checkRendering("\"Oh, \\\"Hello!\\\"\"", value);
myPrinter.setPrintSymbolAsString(false);
value.setValue("Oh, 'Hello there!'");
checkRendering("'Oh, \\\'Hello there!\\\''", value);
myPrinter.setPrintSymbolAsString(true);
checkRendering("\"Oh, 'Hello there!'\"", value);
myPrinter.setPrintSymbolAsString(false);
value.addTypeAnnotation("an");
checkRendering("an::'Oh, \\\'Hello there!\\\''", value);
// TODO check escaping
value = system().newSymbol("Ab\u0000");
checkRendering("'Ab\\0'", value);
myPrinter.setPrintSymbolAsString(true);
checkRendering("\"Ab\\0\"", value);
myPrinter.setPrintStringAsJson(true);
checkRendering("\"Ab\\u0000\"", value);
myPrinter.setPrintSymbolAsString(false);
checkRendering("'Ab\\0'", value);
// Known, sidlike text
value = system().newSymbol("$99");
checkRendering("'$99'", value);
myPrinter.setPrintSymbolAsString(true);
checkRendering("\"$99\"", value);
myPrinter.setPrintStringAsJson(true);
checkRendering("\"$99\"", value);
myPrinter.setPrintSymbolAsString(false);
checkRendering("'$99'", value);
// Unknown symbol
value = (IonSymbol) system().singleValue("$0");
checkRendering("$0", value);
myPrinter.setPrintSymbolAsString(true);
checkRendering("\"$0\"", value);
myPrinter.setPrintStringAsJson(true);
checkRendering("\"$0\"", value);
myPrinter.setPrintSymbolAsString(false);
checkRendering("$0", value);
}
Aggregations