1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.oracle.jmx.remote.rest.json; |
|
27 |
|
28 /** |
|
29 * @author harsha |
|
30 */ |
|
31 public class JSONPrimitive implements JSONElement { |
|
32 |
|
33 private final Object value; |
|
34 |
|
35 public JSONPrimitive(long i) { |
|
36 value = i; |
|
37 } |
|
38 |
|
39 public JSONPrimitive(double i) { |
|
40 value = i; |
|
41 } |
|
42 |
|
43 public JSONPrimitive(Boolean i) { |
|
44 value = i; |
|
45 } |
|
46 |
|
47 public JSONPrimitive(String s) { |
|
48 value = s; |
|
49 } |
|
50 |
|
51 public static String escape(String s) { |
|
52 StringBuilder sb = new StringBuilder(); |
|
53 for (int i = 0; i < s.length(); i++) { |
|
54 char ch = s.charAt(i); |
|
55 if (ch == '\\') { |
|
56 if (i < s.length() - 1 && (s.charAt(i + 1) == '\\' || s.charAt(i + 1) == '"')) { |
|
57 sb.append(ch).append(s.charAt(i + 1)); |
|
58 i++; |
|
59 } else { |
|
60 sb.append("\\\\"); |
|
61 } |
|
62 } else if (ch == '"') { |
|
63 sb.append("\\\""); |
|
64 } else { |
|
65 sb.append(ch); |
|
66 } |
|
67 } |
|
68 return sb.toString(); |
|
69 } |
|
70 |
|
71 public Object getValue() { |
|
72 return value; |
|
73 } |
|
74 |
|
75 @Override |
|
76 public String toJsonString() { |
|
77 if (value instanceof String) { |
|
78 return "\"" + escape(value.toString()) + "\""; |
|
79 } |
|
80 return value != null ? value.toString() : null; |
|
81 } |
|
82 |
|
83 @Override |
|
84 public int hashCode() { |
|
85 if (value instanceof String) { |
|
86 return ((String) value).hashCode(); |
|
87 } else if (value instanceof Long) { |
|
88 return ((Long) value).hashCode(); |
|
89 } else if (value instanceof Double) { |
|
90 return ((Double) value).hashCode(); |
|
91 } else if (value instanceof Boolean) { |
|
92 return ((Boolean) value).hashCode(); |
|
93 } else { |
|
94 return super.hashCode(); |
|
95 } |
|
96 } |
|
97 |
|
98 @Override |
|
99 public boolean equals(Object obj) { |
|
100 if (obj == this) { |
|
101 return true; |
|
102 } |
|
103 |
|
104 if (!(obj instanceof JSONPrimitive)) { |
|
105 return false; |
|
106 } |
|
107 |
|
108 JSONPrimitive o = (JSONPrimitive) obj; |
|
109 |
|
110 if (value == null && o.getValue() == null) { |
|
111 return true; |
|
112 } |
|
113 |
|
114 if (value != null && o.getValue() != null) { |
|
115 if (value.getClass().equals(o.getValue().getClass())) { |
|
116 return value.equals(o.getValue()); |
|
117 } |
|
118 } |
|
119 return false; |
|
120 } |
|
121 } |
|