Skip to content

Commit 37a2e15

Browse files
committed
[bidi][java] Add primitive type
1 parent cf3adc7 commit 37a2e15

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
18+
package org.openqa.selenium.bidi.script;
19+
20+
public enum PrimitiveType {
21+
UNDEFINED("undefined"),
22+
NULL("null"),
23+
STRING("string"),
24+
NUMBER("number"),
25+
SPECIAL_NUMBER("number"),
26+
BOOLEAN("boolean"),
27+
BIGINT("bigint");
28+
private final String type;
29+
30+
PrimitiveType(String type) {
31+
this.type = type;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return type;
37+
}
38+
39+
public static PrimitiveType findByName(String name) {
40+
PrimitiveType result = null;
41+
for (PrimitiveType type : values()) {
42+
if (type.toString().equalsIgnoreCase(name)) {
43+
result = type;
44+
break;
45+
}
46+
}
47+
return result;
48+
}
49+
}

java/src/org/openqa/selenium/bidi/script/RegExpValue.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// under the License
1717
package org.openqa.selenium.bidi.script;
1818

19-
2019
import java.util.Map;
2120
import java.util.TreeMap;
2221
import org.openqa.selenium.json.JsonInput;

0 commit comments

Comments
 (0)