下面列出了怎么用com.fasterxml.jackson.databind.node.DoubleNode的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Judge Java objects and create JsonNode.
*/
public static JsonNode get( final Object obj ) throws IOException {
if ( obj instanceof PrimitiveObject ) {
return PrimitiveObjectToJsonNode.get( (PrimitiveObject)obj );
} else if ( obj instanceof String ) {
return new TextNode( (String)obj );
} else if ( obj instanceof Boolean ) {
return BooleanNode.valueOf( (Boolean)obj );
} else if ( obj instanceof Short ) {
return IntNode.valueOf( ( (Short)obj ).intValue() );
} else if ( obj instanceof Integer ) {
return IntNode.valueOf( (Integer)obj );
} else if ( obj instanceof Long ) {
return new LongNode( (Long)obj );
} else if ( obj instanceof Float ) {
return new DoubleNode( ( (Float)obj ).doubleValue() );
} else if ( obj instanceof Double ) {
return new DoubleNode( (Double)obj );
} else if ( obj instanceof byte[] ) {
return new BinaryNode( (byte[])obj );
} else if ( obj == null ) {
return NullNode.getInstance();
} else {
return new TextNode( obj.toString() );
}
}
public JsonNode call(JsonNode input, JsonNode[] arguments) {
JsonNode array = arguments[0];
if (array.isNull())
return NullNode.instance;
else if (!array.isArray())
throw new JsltException("sum(): argument must be array, was " + array);
double sum = 0.0;
boolean integral = true;
for (int ix = 0; ix < array.size(); ix++) {
JsonNode value = array.get(ix);
if (!value.isNumber())
throw new JsltException("sum(): array must contain numbers, found " + value);
integral &= value.isIntegralNumber();
sum += value.doubleValue();
}
if (integral)
return new LongNode((long) sum);
else
return new DoubleNode(sum);
}
public JsonNode perform(JsonNode v1, JsonNode v2) {
if (v1.isNull() || v2.isNull())
return NullNode.instance;
// we only support the numeric operation and nothing else
v1 = NodeUtils.number(v1, true, location);
v2 = NodeUtils.number(v2, true, location);
if (v1.isIntegralNumber() && v2.isIntegralNumber()) {
long l1 = v1.longValue();
long l2 = v2.longValue();
if (l1 % l2 == 0)
return new LongNode(l1 / l2);
else
return new DoubleNode((double) l1 / (double) l2);
} else
return new DoubleNode(perform(v1.doubleValue(), v2.doubleValue()));
}
@Override
public Points deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
TreeNode n = jp.getCodec().readTree(jp);
Map<String, Object> points = new HashMap<>();
Iterator<String> namesIter = n.fieldNames();
while(namesIter.hasNext()) {
String field = namesIter.next();
TreeNode child = n.get(field);
Object o;
if (child instanceof DoubleNode || child instanceof FloatNode) {
o = ((NumericNode) child).doubleValue();
} else if (child instanceof IntNode || child instanceof LongNode) {
o = ((NumericNode) child).longValue();
} else {
throw new MergeException("Unsupported Type, " + child.getClass());
}
points.put(field, o);
}
return new Points(points);
}
@Test
@CmmnDeployment(resources = {
"org/flowable/cmmn/test/runtime/DecisionTaskTest.oneDecisionTaskCase.cmmn",
"org/flowable/cmmn/test/runtime/DecisionTaskTest.outputOrder.dmn"})
public void withOutputOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
CaseInstance caseInstance = this.cmmnRule.getCmmnRuntimeService().createCaseInstanceBuilder()
.caseDefinitionKey("oneDecisionTaskCase")
.variable("testInput", "second")
.start();
Map<String, Object> caseVariables = caseInstance.getCaseVariables();
Object resultObject = caseVariables.get("DecisionTable");
assertThat(resultObject).isInstanceOf(ArrayNode.class);
ArrayNode result = (ArrayNode) resultObject;
assertThat(result).hasSize(1);
assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
@Test
@CmmnDeployment(resources = {
"org/flowable/cmmn/test/runtime/DecisionTaskTest.oneDecisionTaskCase.cmmn",
"org/flowable/cmmn/test/runtime/DecisionTaskTest.ruleOrder.dmn"})
public void withRuleOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
CaseInstance caseInstance = this.cmmnRule.getCmmnRuntimeService().createCaseInstanceBuilder()
.caseDefinitionKey("oneDecisionTaskCase")
.variable("testInput", "second")
.start();
Map<String, Object> caseVariables = caseInstance.getCaseVariables();
Object resultObject = caseVariables.get("DecisionTable");
assertThat(resultObject).isInstanceOf(ArrayNode.class);
ArrayNode result = (ArrayNode) resultObject;
assertThat(result).hasSize(1);
assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
@Test
@Deployment(resources = {
"org/flowable/bpmn/test/runtime/DmnTaskTest.oneDecisionTaskProcess.bpmn20.xml",
"org/flowable/bpmn/test/runtime/DmnTaskTest.outputOrder.dmn"})
void withOutputOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
ProcessInstance processInstance = this.runtimeService.createProcessInstanceBuilder()
.processDefinitionKey("oneDecisionTaskProcess")
.variable("testInput", "second")
.start();
Map<String, Object> processVariables = processInstance.getProcessVariables();
Object resultObject = processVariables.get("DecisionTable");
assertThat(resultObject).isInstanceOf(ArrayNode.class);
ArrayNode result = (ArrayNode) resultObject;
assertThat(result).hasSize(1);
assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
@Test
@Deployment(resources = {
"org/flowable/bpmn/test/runtime/DmnTaskTest.oneDecisionTaskProcess.bpmn20.xml",
"org/flowable/bpmn/test/runtime/DmnTaskTest.ruleOrder.dmn"})
void withRuleOrder_ensureListOfItemIsReturnedEvenIfOnlyOneRowIsHit() {
ProcessInstance processInstance = this.runtimeService.createProcessInstanceBuilder()
.processDefinitionKey("oneDecisionTaskProcess")
.variable("testInput", "second")
.start();
Map<String, Object> processVariables = processInstance.getProcessVariables();
Object resultObject = processVariables.get("DecisionTable");
assertThat(resultObject).isInstanceOf(ArrayNode.class);
ArrayNode result = (ArrayNode) resultObject;
assertThat(result).hasSize(1);
assertThat(result.get(0)).isInstanceOf(ObjectNode.class);
assertThat(result.get(0).get("testOutput")).isInstanceOf(DoubleNode.class);
assertThat(result.get(0).get("testOutput").asDouble()).isEqualTo(2.0);
}
@Activate
public void activate() {
KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
.register(KryoNamespaces.API)
.register(ConfigKey.class, ObjectNode.class, ArrayNode.class,
JsonNodeFactory.class, LinkedHashMap.class,
TextNode.class, BooleanNode.class,
LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class,
NullNode.class);
configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder()
.withSerializer(Serializer.using(kryoBuilder.build()))
.withName("onos-network-configs")
.withRelaxedReadConsistency()
.build();
configs.addListener(listener);
log.info("Started");
}
@Override
public JsonNode sample() {
double step;
if (stepDistribution == null) {
step = rand.nextGaussian() * sd.sample().asDouble() + mean.sample().asDouble();
} else {
step = stepDistribution.sample().asDouble();
}
double newState = state.addAndGet(step);
if (verbose) {
ObjectNode r = new ObjectNode(JsonNodeFactory.withExactBigDecimals(false));
r.set("value", new DoubleNode(newState));
r.set("step", new DoubleNode(step));
return r;
} else {
return new DoubleNode(newState);
}
}
@SuppressWarnings("UnusedDeclaration")
public void setPrecision(final JsonNode value) throws IOException {
if (value.isObject()) {
sd = new FieldSampler() {
FieldSampler base = FieldSampler.newSampler(value.toString());
@Override
public JsonNode sample() {
return new DoubleNode(Math.sqrt(1 / base.sample().asDouble()));
}
};
} else {
this.sd = constant(Math.sqrt(1 / value.asDouble()));
}
init();
}
@SuppressWarnings("UnusedDeclaration")
public void setVariance(final JsonNode value) throws IOException {
if (value.isObject()) {
sd = new FieldSampler() {
FieldSampler base = FieldSampler.newSampler(value.toString());
@Override
public JsonNode sample() {
return new DoubleNode(Math.sqrt(base.sample().asDouble()));
}
};
} else {
this.sd = constant(Math.sqrt(value.asDouble()));
}
init();
}
@SuppressWarnings("UnusedDeclaration")
public void setSD(final JsonNode value) throws IOException {
if (value.isObject()) {
sd = new FieldSampler() {
FieldSampler base = FieldSampler.newSampler(value.toString());
@Override
public JsonNode sample() {
return new DoubleNode(base.sample().asDouble());
}
};
} else {
sd = constant(value.asDouble());
}
init();
}
/**
* Converts JsonNode to PrimitiveObject.
*/
public static PrimitiveObject get( final JsonNode jsonNode ) throws IOException {
if ( jsonNode instanceof TextNode ) {
return new StringObj( ( (TextNode)jsonNode ).textValue() );
} else if ( jsonNode instanceof BooleanNode ) {
return new BooleanObj( ( (BooleanNode)jsonNode ).booleanValue() );
} else if ( jsonNode instanceof IntNode ) {
return new IntegerObj( ( (IntNode)jsonNode ).intValue() );
} else if ( jsonNode instanceof LongNode ) {
return new LongObj( ( (LongNode)jsonNode ).longValue() );
} else if ( jsonNode instanceof DoubleNode ) {
return new DoubleObj( ( (DoubleNode)jsonNode ).doubleValue() );
} else if ( jsonNode instanceof BigIntegerNode ) {
return new StringObj( ( (BigIntegerNode)jsonNode ).bigIntegerValue().toString() );
} else if ( jsonNode instanceof DecimalNode ) {
return new StringObj( ( (DecimalNode)jsonNode ).decimalValue().toString() );
} else if ( jsonNode instanceof BinaryNode ) {
return new BytesObj( ( (BinaryNode)jsonNode ).binaryValue() );
} else if ( jsonNode instanceof POJONode ) {
return new BytesObj( ( (POJONode)jsonNode ).binaryValue() );
} else if ( jsonNode instanceof NullNode ) {
return NullObj.getInstance();
} else if ( jsonNode instanceof MissingNode ) {
return NullObj.getInstance();
} else {
return new StringObj( jsonNode.toString() );
}
}
/**
* Convert PrimitiveObject to JsonNode.
*/
public static JsonNode get( final PrimitiveObject obj ) throws IOException {
if ( obj == null ) {
return NullNode.getInstance();
}
switch ( obj.getPrimitiveType() ) {
case BOOLEAN:
return BooleanNode.valueOf( obj.getBoolean() );
case BYTE:
return IntNode.valueOf( obj.getInt() );
case SHORT:
return IntNode.valueOf( obj.getInt() );
case INTEGER:
return IntNode.valueOf( obj.getInt() );
case LONG:
return new LongNode( obj.getLong() );
case FLOAT:
return new DoubleNode( obj.getDouble() );
case DOUBLE:
return new DoubleNode( obj.getDouble() );
case STRING:
return new TextNode( obj.getString() );
case BYTES:
return new BinaryNode( obj.getBytes() );
default:
return NullNode.getInstance();
}
}
public JsonNode perform(JsonNode v1, JsonNode v2) {
if (v1.isNull() || v2.isNull())
return NullNode.instance;
v1 = NodeUtils.number(v1, true, location);
v2 = NodeUtils.number(v2, true, location);
if (v1.isIntegralNumber() && v2.isIntegralNumber())
return new LongNode(perform(v1.longValue(), v2.longValue()));
else
return new DoubleNode(perform(v1.doubleValue(), v2.doubleValue()));
}
@Override
public Number serialize(Object dataFetcherResult) {
if (dataFetcherResult instanceof DoubleNode) {
return ((DoubleNode) dataFetcherResult).numberValue();
} else {
throw serializationException(dataFetcherResult, DoubleNode.class);
}
}
@Override
public DoubleNode parseValue(Object input) {
if (input instanceof Number || input instanceof String) {
return DoubleNode.valueOf(new BigDecimal(input.toString()).doubleValue());
}
if (input instanceof DoubleNode) {
return (DoubleNode) input;
}
throw valueParsingException(input, Number.class, String.class, DoubleNode.class);
}
@Override
public DoubleNode parseLiteral(Object input) {
if (input instanceof IntValue) {
return DoubleNode.valueOf(((IntValue) input).getValue().doubleValue());
} if (input instanceof FloatValue) {
return DoubleNode.valueOf(((FloatValue) input).getValue().doubleValue());
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class, FloatValue.class));
}
}
private static Map<Type, GraphQLScalarType> getScalarMapping() {
Map<Type, GraphQLScalarType> scalarMapping = new HashMap<>();
scalarMapping.put(TextNode.class, JsonTextNode);
scalarMapping.put(BooleanNode.class, JsonBooleanNode);
scalarMapping.put(BinaryNode.class, JsonBinaryNode);
scalarMapping.put(BigIntegerNode.class, JsonBigIntegerNode);
scalarMapping.put(IntNode.class, JsonIntegerNode);
scalarMapping.put(ShortNode.class, JsonShortNode);
scalarMapping.put(DecimalNode.class, JsonDecimalNode);
scalarMapping.put(FloatNode.class, JsonFloatNode);
scalarMapping.put(DoubleNode.class, JsonDoubleNode);
scalarMapping.put(NumericNode.class, JsonDecimalNode);
return Collections.unmodifiableMap(scalarMapping);
}
public JacksonContainer(ObjectNode obj, JsonNode any, BinaryNode binary, TextNode text, IntNode integer,
DoubleNode dbl, BigIntegerNode bigInt, ArrayNode array) {
this.obj = obj;
this.any = any;
this.text = text;
this.binary = binary;
this.integer = integer;
this.dbl = dbl;
this.bigInt = bigInt;
this.array = array;
}
private JsonNode createNode(Object value) {
if (value instanceof Float) {
return new FloatNode((Float) value);
} else if (value instanceof Double) {
return new DoubleNode((Double) value);
}
return null;
}
private Example createExample(JsonNode node) {
if (node instanceof ObjectNode) {
ObjectExample obj = new ObjectExample();
ObjectNode on = (ObjectNode) node;
for (Iterator<Entry<String, JsonNode>> x = on.fields(); x.hasNext(); ) {
Entry<String, JsonNode> i = x.next();
String key = i.getKey();
JsonNode value = i.getValue();
obj.put(key, createExample(value));
}
return obj;
} else if (node instanceof ArrayNode) {
ArrayExample arr = new ArrayExample();
ArrayNode an = (ArrayNode) node;
for (JsonNode childNode : an) {
arr.add(createExample(childNode));
}
return arr;
} else if (node instanceof DoubleNode) {
return new DoubleExample(node.doubleValue());
} else if (node instanceof IntNode || node instanceof ShortNode) {
return new IntegerExample(node.intValue());
} else if (node instanceof FloatNode) {
return new FloatExample(node.floatValue());
} else if (node instanceof BigIntegerNode) {
return new BigIntegerExample(node.bigIntegerValue());
} else if (node instanceof DecimalNode) {
return new DecimalExample(node.decimalValue());
} else if (node instanceof LongNode) {
return new LongExample(node.longValue());
} else if (node instanceof BooleanNode) {
return new BooleanExample(node.booleanValue());
} else {
return new StringExample(node.asText());
}
}
@Override
public Optional<Float> deserialize(JsonNode json) {
if (json instanceof FloatNode) {
return Optional.of(json.floatValue());
} else if (json instanceof DoubleNode) {
return Optional.of(json.floatValue());
} else {
return Optional.empty();
}
}
@Override
public Optional<Double> deserialize(JsonNode json) {
if (json instanceof DoubleNode || json instanceof FloatNode) {
return Optional.of(json.asDouble());
} else {
return Optional.empty();
}
}
/**
* Add datapoint of double type value.
*
* @param time datapoint's timestamp
* @param value datapoint's value
* @return Datapoint
*/
public Datapoint addDoubleValue(long time, double value) {
initialValues();
checkType(TsdbConstants.TYPE_DOUBLE);
values.add(Lists.<JsonNode> newArrayList(new LongNode(time), new DoubleNode(value)));
return this;
}
private Object jsonNodeToValue(JsonNode node) {
Object value = node;
if (node instanceof NullNode) {
value = null;
} else if (node instanceof TextNode) {
value = node.asText();
} else if (node instanceof BooleanNode) {
value = node.asBoolean();
} else if (node instanceof IntNode) {
value = node.asInt();
} else if (node instanceof FloatNode) {
value = (float)((FloatNode)node).asDouble();
} else if (node instanceof DoubleNode) {
value = (float)((DoubleNode)node).asDouble();
} else if (node instanceof ArrayNode) {
int numItems = node.size();
String[] values = new String[numItems];
for (int i = 0; i < numItems; i++) {
values[i] = node.path(i).asText();
}
value = values;
}
return (value);
}
@Activate
public void activate() {
Serializer serializer = Serializer.using(KryoNamespaces.API,
ObjectNode.class, ArrayNode.class,
JsonNodeFactory.class, LinkedHashMap.class,
TextNode.class, BooleanNode.class,
LongNode.class, DoubleNode.class, ShortNode.class,
IntNode.class, NullNode.class, UiSessionToken.class);
prefsConsistentMap = storageService.<String, ObjectNode>consistentMapBuilder()
.withName(ONOS_USER_PREFERENCES)
.withSerializer(serializer)
.withRelaxedReadConsistency()
.build();
prefsConsistentMap.addListener(prefsListener);
prefs = prefsConsistentMap.asJavaMap();
tokensConsistentMap = storageService.<UiSessionToken, String>consistentMapBuilder()
.withName(ONOS_SESSION_TOKENS)
.withSerializer(serializer)
.withRelaxedReadConsistency()
.build();
tokens = tokensConsistentMap.asJavaMap();
register(core);
log.info("Started");
}
protected static FieldSampler constant(final double v) {
return new FieldSampler() {
private DoubleNode sd = new DoubleNode(v);
@Override
public JsonNode sample() {
return sd;
}
};
}
@Override
public JsonNode sample() {
double x;
do {
x = rand.nextDouble();
} while (x < min || x > max);
return new DoubleNode(x);
}