|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License |
| 17 | +package org.openqa.selenium.bidi.script; |
| 18 | + |
| 19 | +import static java.util.Collections.unmodifiableMap; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | +import java.util.TreeMap; |
| 23 | +import org.openqa.selenium.internal.Require; |
| 24 | + |
| 25 | +public class PrimitiveProtocolValue extends LocalValue { |
| 26 | + |
| 27 | + public enum SpecialNumberType { |
| 28 | + NAN("NaN"), |
| 29 | + MINUS_ZERO("-0"), |
| 30 | + INFINITY("Infinity"), |
| 31 | + MINUS_INFINITY("-Infinity"); |
| 32 | + |
| 33 | + private final String type; |
| 34 | + |
| 35 | + SpecialNumberType(String type) { |
| 36 | + this.type = type; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String toString() { |
| 41 | + return type; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private final PrimitiveType type; |
| 46 | + private Object value; |
| 47 | + |
| 48 | + private PrimitiveProtocolValue(PrimitiveType type, Object value) { |
| 49 | + this.type = type; |
| 50 | + this.value = value; |
| 51 | + } |
| 52 | + |
| 53 | + private PrimitiveProtocolValue(PrimitiveType type) { |
| 54 | + Require.precondition( |
| 55 | + type.equals(PrimitiveType.UNDEFINED) || type.equals(PrimitiveType.NULL), |
| 56 | + "Only null and defined do not require values. " |
| 57 | + + "Rest all type require a corresponding value."); |
| 58 | + this.type = type; |
| 59 | + } |
| 60 | + |
| 61 | + public static LocalValue stringValue(String value) { |
| 62 | + return new PrimitiveProtocolValue(PrimitiveType.STRING, value); |
| 63 | + } |
| 64 | + |
| 65 | + public static LocalValue numberValue(long value) { |
| 66 | + return new PrimitiveProtocolValue(PrimitiveType.NUMBER, value); |
| 67 | + } |
| 68 | + |
| 69 | + public static LocalValue numberValue(double value) { |
| 70 | + return new PrimitiveProtocolValue(PrimitiveType.NUMBER, value); |
| 71 | + } |
| 72 | + |
| 73 | + public static LocalValue numberValue(SpecialNumberType specialNumber) { |
| 74 | + return new PrimitiveProtocolValue(PrimitiveType.SPECIAL_NUMBER, specialNumber.toString()); |
| 75 | + } |
| 76 | + |
| 77 | + public static LocalValue undefinedValue() { |
| 78 | + return new PrimitiveProtocolValue(PrimitiveType.UNDEFINED); |
| 79 | + } |
| 80 | + |
| 81 | + public static LocalValue nullValue() { |
| 82 | + return new PrimitiveProtocolValue(PrimitiveType.NULL); |
| 83 | + } |
| 84 | + |
| 85 | + public static LocalValue booleanValue(boolean value) { |
| 86 | + return new PrimitiveProtocolValue(PrimitiveType.BOOLEAN, value); |
| 87 | + } |
| 88 | + |
| 89 | + public static LocalValue bigIntValue(String value) { |
| 90 | + return new PrimitiveProtocolValue(PrimitiveType.BIGINT, value); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Map<String, Object> toJson() { |
| 95 | + Map<String, Object> toReturn = new TreeMap<>(); |
| 96 | + toReturn.put("type", this.type.toString()); |
| 97 | + |
| 98 | + if (!(this.type.equals(PrimitiveType.NULL) || this.type.equals(PrimitiveType.UNDEFINED))) { |
| 99 | + toReturn.put("value", this.value); |
| 100 | + } |
| 101 | + |
| 102 | + return unmodifiableMap(toReturn); |
| 103 | + } |
| 104 | +} |
0 commit comments