PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/src/main/java/org/terasology/persistence/typeHandling/protobuf/ProtobufPersistedData.java

https://gitlab.com/darithorn/Terasology
Java | 403 lines | 350 code | 36 blank | 17 comment | 156 complexity | fc7de0e0ce149ee1448776592197c155 MD5 | raw file
  1. /*
  2. * Copyright 2013 MovingBlocks
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.terasology.persistence.typeHandling.protobuf;
  17. import com.google.common.collect.Lists;
  18. import com.google.common.collect.Maps;
  19. import com.google.common.primitives.Ints;
  20. import gnu.trove.list.TDoubleList;
  21. import gnu.trove.list.TFloatList;
  22. import gnu.trove.list.TIntList;
  23. import gnu.trove.list.TLongList;
  24. import gnu.trove.list.array.TDoubleArrayList;
  25. import gnu.trove.list.array.TFloatArrayList;
  26. import gnu.trove.list.array.TIntArrayList;
  27. import gnu.trove.list.array.TLongArrayList;
  28. import org.terasology.persistence.typeHandling.DeserializationException;
  29. import org.terasology.persistence.typeHandling.PersistedData;
  30. import org.terasology.persistence.typeHandling.PersistedDataArray;
  31. import org.terasology.persistence.typeHandling.PersistedDataMap;
  32. import org.terasology.persistence.typeHandling.inMemory.PersistedBoolean;
  33. import org.terasology.persistence.typeHandling.inMemory.PersistedDouble;
  34. import org.terasology.persistence.typeHandling.inMemory.PersistedFloat;
  35. import org.terasology.persistence.typeHandling.inMemory.PersistedInteger;
  36. import org.terasology.persistence.typeHandling.inMemory.PersistedLong;
  37. import org.terasology.persistence.typeHandling.inMemory.PersistedMap;
  38. import org.terasology.persistence.typeHandling.inMemory.PersistedString;
  39. import org.terasology.protobuf.EntityData;
  40. import java.nio.ByteBuffer;
  41. import java.util.Iterator;
  42. import java.util.List;
  43. import java.util.Map;
  44. /**
  45. */
  46. public class ProtobufPersistedData implements PersistedData, PersistedDataArray {
  47. private EntityData.Value data;
  48. public ProtobufPersistedData(EntityData.Value data) {
  49. this.data = data;
  50. }
  51. public EntityData.Value getValue() {
  52. return data;
  53. }
  54. @Override
  55. public String getAsString() {
  56. if (data.getStringCount() == 1) {
  57. return data.getString(0);
  58. } else if (data.getStringCount() > 1) {
  59. throw new IllegalStateException("Data is an array of size != 1");
  60. } else if (!isNull()) {
  61. throw new ClassCastException("Data is not a String");
  62. }
  63. return null;
  64. }
  65. @Override
  66. public double getAsDouble() {
  67. if (data.getDoubleCount() == 1) {
  68. return data.getDouble(0);
  69. } else if (data.getFloatCount() == 1) {
  70. return data.getFloat(0);
  71. } else if (data.getLongCount() == 1) {
  72. return data.getLong(0);
  73. } else if (data.getIntegerCount() == 1) {
  74. return data.getInteger(0);
  75. } else if (data.getDoubleCount() + data.getFloatCount() + data.getIntegerCount() + data.getLongCount() > 1) {
  76. throw new IllegalStateException("Data is an array of size != 1");
  77. } else {
  78. throw new ClassCastException("Data is not a number");
  79. }
  80. }
  81. @Override
  82. public float getAsFloat() {
  83. if (data.getFloatCount() == 1) {
  84. return data.getFloat(0);
  85. } else if (data.getDoubleCount() == 1) {
  86. return (float) data.getDouble(0);
  87. } else if (data.getLongCount() == 1) {
  88. return data.getLong(0);
  89. } else if (data.getIntegerCount() == 1) {
  90. return data.getInteger(0);
  91. } else if (data.getDoubleCount() + data.getFloatCount() + data.getIntegerCount() + data.getLongCount() > 1) {
  92. throw new IllegalStateException("Data is an array of size != 1");
  93. } else {
  94. throw new ClassCastException("Data is not a number");
  95. }
  96. }
  97. @Override
  98. public int getAsInteger() {
  99. if (data.getIntegerCount() == 1) {
  100. return data.getInteger(0);
  101. } else if (data.getDoubleCount() == 1) {
  102. return (int) data.getDouble(0);
  103. } else if (data.getFloatCount() == 1) {
  104. return (int) data.getFloat(0);
  105. } else if (data.getLongCount() == 1) {
  106. return (int) data.getLong(0);
  107. } else if (data.getDoubleCount() + data.getFloatCount() + data.getIntegerCount() + data.getLongCount() > 1) {
  108. throw new IllegalStateException("Data is an array of size != 1");
  109. } else {
  110. throw new ClassCastException("Data is not a number");
  111. }
  112. }
  113. @Override
  114. public long getAsLong() {
  115. if (data.getLongCount() == 1) {
  116. return (int) data.getLong(0);
  117. } else if (data.getIntegerCount() == 1) {
  118. return data.getInteger(0);
  119. } else if (data.getDoubleCount() == 1) {
  120. return (int) data.getDouble(0);
  121. } else if (data.getFloatCount() == 1) {
  122. return (int) data.getFloat(0);
  123. } else if (data.getDoubleCount() + data.getFloatCount() + data.getIntegerCount() + data.getLongCount() > 1) {
  124. throw new IllegalStateException("Data is an array of size != 1");
  125. } else {
  126. throw new ClassCastException("Data is not a number");
  127. }
  128. }
  129. @Override
  130. public boolean getAsBoolean() {
  131. if (data.getBooleanCount() == 1) {
  132. return data.getBoolean(0);
  133. } else if (data.getBooleanCount() > 1) {
  134. throw new IllegalStateException("Data is an array of size != 1");
  135. } else {
  136. throw new ClassCastException("Data is not a boolean");
  137. }
  138. }
  139. @Override
  140. public byte[] getAsBytes() {
  141. if (data.hasBytes()) {
  142. return data.getBytes().toByteArray();
  143. } else if (!isNull()) {
  144. throw new DeserializationException("Data is not bytes");
  145. } else {
  146. return new byte[0];
  147. }
  148. }
  149. @Override
  150. public ByteBuffer getAsByteBuffer() {
  151. if (data.hasBytes()) {
  152. return data.getBytes().asReadOnlyByteBuffer();
  153. } else if (!isNull()) {
  154. throw new DeserializationException("Data is not bytes");
  155. } else {
  156. return ByteBuffer.wrap(new byte[0]);
  157. }
  158. }
  159. @Override
  160. public PersistedDataArray getAsArray() {
  161. if (isArray()) {
  162. return this;
  163. }
  164. throw new IllegalStateException("Data is not an array");
  165. }
  166. @Override
  167. public PersistedDataMap getAsValueMap() {
  168. Map<String, PersistedData> result = Maps.newLinkedHashMap();
  169. if (data.getNameValueCount() > 0) {
  170. for (int i = 0; i < data.getNameValueCount(); ++i) {
  171. result.put(data.getNameValue(i).getName(), new ProtobufPersistedData(data.getNameValue(i).getValue()));
  172. }
  173. } else if (!isNull()) {
  174. throw new IllegalStateException("Data is not a value map");
  175. }
  176. return new PersistedMap(result);
  177. }
  178. @Override
  179. public boolean isString() {
  180. return data.getStringCount() == 1 || isNull();
  181. }
  182. @Override
  183. public boolean isNumber() {
  184. return data.getIntegerCount() == 1 || data.getFloatCount() == 1 || data.getLongCount() == 1 || data.getDoubleCount() == 1;
  185. }
  186. @Override
  187. public boolean isBoolean() {
  188. return data.getBooleanCount() == 1;
  189. }
  190. @Override
  191. public boolean isBytes() {
  192. return data.hasBytes() || isNull();
  193. }
  194. @Override
  195. public boolean isArray() {
  196. return true;
  197. }
  198. @Override
  199. public boolean isValueMap() {
  200. return data.getNameValueCount() != 0 || isNull();
  201. }
  202. @Override
  203. public boolean isNull() {
  204. return !data.hasBytes() && data.getBooleanCount() + data.getFloatCount() + data.getDoubleCount() + data.getIntegerCount() + data.getLongCount()
  205. + data.getStringCount() + data.getValueCount() + data.getNameValueCount() == 0;
  206. }
  207. @Override
  208. public String toString() {
  209. return data.toString();
  210. }
  211. @Override
  212. public int size() {
  213. return Math.max(Math.max(Math.max(data.getBooleanCount(), data.getFloatCount()), Math.max(data.getDoubleCount(), data.getIntegerCount())),
  214. Math.max(Math.max(data.getLongCount(), data.getStringCount()), data.getValueCount()));
  215. }
  216. @Override
  217. public PersistedData getArrayItem(int index) {
  218. if (data.getValueCount() > 0) {
  219. return new ProtobufPersistedData(data.getValue(index));
  220. } else if (data.getFloatCount() > 0) {
  221. return new PersistedFloat(data.getFloat(index));
  222. } else if (data.getIntegerCount() > 0) {
  223. return new PersistedInteger(data.getInteger(index));
  224. } else if (data.getDoubleCount() > 0) {
  225. return new PersistedDouble(data.getDouble(index));
  226. } else if (data.getBooleanCount() > 0) {
  227. return new PersistedBoolean(data.getBoolean(index));
  228. } else if (data.getLongCount() > 0) {
  229. return new PersistedLong(data.getLong(index));
  230. } else if (data.getStringCount() > 0) {
  231. return new PersistedString(data.getString(index));
  232. } else if (data.hasBytes()) {
  233. throw new IllegalStateException("Data is not an array");
  234. }
  235. throw new IndexOutOfBoundsException(index + " exceeds size of array data");
  236. }
  237. @Override
  238. public boolean isNumberArray() {
  239. return data.getIntegerCount() + data.getFloatCount() + data.getDoubleCount() + data.getLongCount() > 0 || isNull();
  240. }
  241. @Override
  242. public boolean isBooleanArray() {
  243. return data.getBooleanCount() > 0 || isNull();
  244. }
  245. @Override
  246. public boolean isStringArray() {
  247. return data.getStringCount() > 0 || isNull();
  248. }
  249. @Override
  250. public List<String> getAsStringArray() {
  251. return data.getStringList();
  252. }
  253. @Override
  254. public TDoubleList getAsDoubleArray() {
  255. if (data.getDoubleCount() != 0) {
  256. TDoubleList result = new TDoubleArrayList(data.getDoubleCount());
  257. for (int i = 0; i < data.getDoubleCount(); ++i) {
  258. result.add(data.getDouble(i));
  259. }
  260. return result;
  261. } else {
  262. TDoubleList result = new TDoubleArrayList(data.getFloatCount());
  263. for (int i = 0; i < data.getFloatCount(); ++i) {
  264. result.add(data.getFloat(i));
  265. }
  266. return result;
  267. }
  268. }
  269. @Override
  270. public TFloatList getAsFloatArray() {
  271. if (data.getFloatCount() != 0) {
  272. TFloatList result = new TFloatArrayList(data.getFloatCount());
  273. for (int i = 0; i < data.getFloatCount(); ++i) {
  274. result.add(data.getFloat(i));
  275. }
  276. return result;
  277. } else {
  278. TFloatList result = new TFloatArrayList(data.getDoubleCount());
  279. for (int i = 0; i < data.getDoubleCount(); ++i) {
  280. result.add((float) data.getDouble(i));
  281. }
  282. return result;
  283. }
  284. }
  285. @Override
  286. public TIntList getAsIntegerArray() {
  287. if (data.getIntegerCount() > 0) {
  288. TIntList result = new TIntArrayList(data.getIntegerCount());
  289. for (int i = 0; i < data.getIntegerCount(); ++i) {
  290. result.add(data.getInteger(i));
  291. }
  292. return result;
  293. } else {
  294. TIntList result = new TIntArrayList(data.getLongCount());
  295. for (int i = 0; i < data.getLongCount(); ++i) {
  296. result.add(Ints.saturatedCast(data.getLong(i)));
  297. }
  298. return result;
  299. }
  300. }
  301. @Override
  302. public TLongList getAsLongArray() {
  303. if (data.getLongCount() > 0) {
  304. TLongList result = new TLongArrayList(data.getLongCount());
  305. for (int i = 0; i < data.getLongCount(); ++i) {
  306. result.add(data.getLong(i));
  307. }
  308. return result;
  309. } else {
  310. TLongList result = new TLongArrayList(data.getIntegerCount());
  311. for (int i = 0; i < data.getIntegerCount(); ++i) {
  312. result.add(data.getInteger(i));
  313. }
  314. return result;
  315. }
  316. }
  317. @Override
  318. public boolean[] getAsBooleanArray() {
  319. boolean[] result = new boolean[data.getBooleanCount()];
  320. for (int i = 0; i < data.getBooleanCount(); ++i) {
  321. result[i] = data.getBoolean(i);
  322. }
  323. return result;
  324. }
  325. @Override
  326. public List<PersistedData> getAsValueArray() {
  327. List<PersistedData> result = Lists.newArrayList();
  328. if (data.getValueCount() > 0) {
  329. for (EntityData.Value val : data.getValueList()) {
  330. result.add(new ProtobufPersistedData(val));
  331. }
  332. } else if (data.getFloatCount() > 0) {
  333. for (int i = 0; i < data.getFloatCount(); ++i) {
  334. result.add(new PersistedFloat(data.getFloat(i)));
  335. }
  336. } else if (data.getIntegerCount() > 0) {
  337. for (int i = 0; i < data.getIntegerCount(); ++i) {
  338. result.add(new PersistedInteger(data.getInteger(i)));
  339. }
  340. } else if (data.getDoubleCount() > 0) {
  341. for (int i = 0; i < data.getDoubleCount(); ++i) {
  342. result.add(new PersistedDouble(data.getDouble(i)));
  343. }
  344. } else if (data.getBooleanCount() > 0) {
  345. for (int i = 0; i < data.getBooleanCount(); ++i) {
  346. result.add(new PersistedBoolean(data.getBoolean(i)));
  347. }
  348. } else if (data.getLongCount() > 0) {
  349. for (int i = 0; i < data.getLongCount(); ++i) {
  350. result.add(new PersistedLong(data.getLong(i)));
  351. }
  352. } else if (data.getStringCount() > 0) {
  353. for (int i = 0; i < data.getStringCount(); ++i) {
  354. result.add(new PersistedString(data.getString(i)));
  355. }
  356. } else if (data.getNameValueCount() > 0) {
  357. result.add(new ProtobufPersistedData(data));
  358. } else if (data.hasBytes()) {
  359. throw new IllegalStateException("Data is not an array");
  360. }
  361. return result;
  362. }
  363. @Override
  364. public Iterator<PersistedData> iterator() {
  365. return getAsValueArray().iterator();
  366. }
  367. }