Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / json / CreatorsFactory.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.util.json;
20
21 import gov.nasa.jpf.vm.ElementInfo;
22 import gov.nasa.jpf.vm.MJIEnv;
23
24 import java.util.HashMap;
25
26 public class CreatorsFactory {
27
28   static private final HashMap<String, Creator> creatorsTable = new HashMap<String, Creator>();
29
30   static {
31     creatorsTable.put("java.lang.Boolean", new BoxedBoolCreator());
32     creatorsTable.put("java.lang.Byte", new BoxedByteCreator());
33     creatorsTable.put("java.lang.Short", new BoxedShortCreator());
34     creatorsTable.put("java.lang.Integer", new BoxedIntCreator());
35     creatorsTable.put("java.lang.Long", new BoxedLongCreator());
36     creatorsTable.put("java.lang.Float", new BoxedFloatCreator());
37     creatorsTable.put("java.lang.Double", new BoxedDoubleCreator());
38     creatorsTable.put("java.lang.String", new StringCreator());
39   }
40
41   public static Creator getCreator(String typeName) {
42
43     return creatorsTable.get(typeName);
44   }
45 }
46
47
48 class BoxedBoolCreator implements Creator {
49   @Override
50   public int create(MJIEnv env, String typeName, Value value) {
51     Boolean read = value.getBoolean();
52     int boolRef = MJIEnv.NULL;
53
54     if (read != null) {
55       boolRef = env.newObject("java.lang.Boolean");
56       ElementInfo ei = env.getModifiableElementInfo(boolRef);
57       ei.setBooleanField("value", (read == true));
58     }
59
60     return boolRef;
61   }
62 }
63
64 class BoxedByteCreator implements Creator {
65   @Override
66   public int create(MJIEnv env, String typeName, Value value) {
67     Double read = value.getDouble();
68     int byteRef = MJIEnv.NULL;
69
70     if (read != null) {
71       byteRef = env.newObject("java.lang.Byte");
72       ElementInfo ei = env.getModifiableElementInfo(byteRef);
73       ei.setByteField("value", read.byteValue());
74     }
75
76     return byteRef;
77   }
78 }
79
80 class BoxedShortCreator implements Creator {
81   @Override
82   public int create(MJIEnv env, String typeName, Value value) {
83     Double read = value.getDouble();
84     int shortRef = MJIEnv.NULL;
85
86     if (read != null) {
87       shortRef = env.newObject("java.lang.Short");
88       ElementInfo ei = env.getModifiableElementInfo(shortRef);
89       ei.setShortField("value", read.shortValue());
90     }
91
92     return shortRef;
93   }
94 }
95
96 class BoxedIntCreator implements Creator {
97   @Override
98   public int create(MJIEnv env, String typeName, Value value) {
99     Double read = value.getDouble();
100     int intRef = MJIEnv.NULL;
101
102     if (read != null) {
103       intRef = env.newObject("java.lang.Integer");
104       ElementInfo ei = env.getModifiableElementInfo(intRef);
105       ei.setIntField("value", read.intValue());
106     }
107
108     return intRef;
109   }
110 }
111
112 class BoxedLongCreator implements Creator {
113   @Override
114   public int create(MJIEnv env, String typeName, Value value) {
115     Double read = value.getDouble();
116     int longRef = MJIEnv.NULL;
117
118     if (read != null) {
119       longRef = env.newObject("java.lang.Long");
120       ElementInfo ei = env.getModifiableElementInfo(longRef);
121       ei.setLongField("value", read.longValue());
122     }
123
124     return longRef;
125   }
126 }
127
128 class BoxedFloatCreator implements Creator {
129   @Override
130   public int create(MJIEnv env, String typeName, Value value) {
131     Double read = value.getDouble();
132     int floatRef = MJIEnv.NULL;
133
134     if (read != null) {
135       floatRef = env.newObject("java.lang.Float");
136       ElementInfo ei = env.getModifiableElementInfo(floatRef);
137       ei.setFloatField("value", read.floatValue());
138     }
139
140     return floatRef;
141   }
142 }
143
144 class BoxedDoubleCreator implements Creator {
145   @Override
146   public int create(MJIEnv env, String typeName, Value value) {
147     Double read = value.getDouble();
148     int doubleRef = MJIEnv.NULL;
149
150     if (read != null) {
151       doubleRef = env.newObject("java.lang.Double");
152       ElementInfo ei = env.getModifiableElementInfo(doubleRef);
153       ei.setDoubleField("value", read.doubleValue());
154     }
155
156     return doubleRef;
157   }
158 }
159
160 class StringCreator implements Creator {
161
162   @Override
163   public int create(MJIEnv env, String typeName, Value value) {
164     String strVal = value.getString();
165     int stringRef = MJIEnv.NULL;
166
167     if (strVal != null) {
168       stringRef = env.newString(strVal);
169     }
170
171     return stringRef;
172   }
173 }