View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * As String is final, using a different marker to imply this is a json object
28   * 
29   * @author Adrian Cole
30   * @see <a href="http://code.google.com/p/google-gson/issues/detail?id=326"/>
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 }