1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.domain;
20
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 import org.jclouds.util.Patterns;
24
25
26
27
28
29
30
31
32 public class JsonBall implements java.io.Serializable, Comparable<String>, CharSequence {
33
34 private static final long serialVersionUID = -8168997021767065199L;
35 private final String value;
36
37 @Override
38 public boolean equals(Object obj) {
39 if (this == obj)
40 return true;
41 if (obj == null)
42 return false;
43 if (getClass() != obj.getClass())
44 return false;
45 JsonBall other = (JsonBall) obj;
46 if (value == null) {
47 if (other.value != null)
48 return false;
49 } else if (!value.equals(other.value))
50 return false;
51 return true;
52 }
53
54 @Override
55 public int hashCode() {
56 final int prime = 31;
57 int result = 1;
58 result = prime * result + ((value == null) ? 0 : value.hashCode());
59 return result;
60 }
61
62 @Override
63 public String toString() {
64 return value;
65 }
66
67 public JsonBall(double value) {
68 this.value = value + "";
69 }
70
71 public JsonBall(int value) {
72 this.value = value + "";
73 }
74
75 public JsonBall(long value) {
76 this.value = value + "";
77 }
78
79 public JsonBall(String value) {
80 this.value = quoteStringIfNotNumber(checkNotNull(value, "value"));
81 }
82
83 static String quoteStringIfNotNumber(String in) {
84 if (Patterns.JSON_STRING_PATTERN.matcher(in).find() && !Patterns.JSON_NUMBER_PATTERN.matcher(in).find()) {
85 return "\"" + in + "\"";
86 }
87 return in;
88 }
89
90 @Override
91 public char charAt(int index) {
92 return value.charAt(index);
93 }
94
95 @Override
96 public int length() {
97 return value.length();
98 }
99
100 @Override
101 public CharSequence subSequence(int start, int end) {
102 return value.subSequence(start, end);
103 }
104
105 @Override
106 public int compareTo(String o) {
107 return value.compareTo(o);
108 }
109
110 }