|
| 1 | +/* |
| 2 | + Copyright 2014 Rustici Software |
| 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 com.rusticisoftware.tincan; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.JsonNode; |
| 19 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | +import com.rusticisoftware.tincan.json.JSONBase; |
| 22 | +import com.rusticisoftware.tincan.json.Mapper; |
| 23 | +import com.rusticisoftware.tincan.json.StringOfJSON; |
| 24 | +import lombok.Data; |
| 25 | +import lombok.EqualsAndHashCode; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.net.URISyntaxException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +@Data |
| 34 | +@EqualsAndHashCode(callSuper = false) |
| 35 | +public class About extends JSONBase{ |
| 36 | + private List<TCAPIVersion> version; |
| 37 | + private Extensions extensions; |
| 38 | + |
| 39 | + public About(String str) throws IOException, URISyntaxException { |
| 40 | + StringOfJSON jsonStr = new StringOfJSON(str); |
| 41 | + init(jsonStr.toJSONNode()); |
| 42 | + } |
| 43 | + |
| 44 | + public About(StringOfJSON jsonStr) throws IOException, URISyntaxException{ |
| 45 | + init(jsonStr.toJSONNode()); |
| 46 | + } |
| 47 | + |
| 48 | + public About(JsonNode jsonNode) throws URISyntaxException{ |
| 49 | + init(jsonNode); |
| 50 | + } |
| 51 | + |
| 52 | + private void init(JsonNode jsonNode) throws URISyntaxException{ |
| 53 | + |
| 54 | + if(jsonNode.hasNonNull("version")) { |
| 55 | + |
| 56 | + Iterator it = jsonNode.get("version").elements(); |
| 57 | + |
| 58 | + while(it.hasNext()){ |
| 59 | + if(version == null) { version = new ArrayList<TCAPIVersion>(); } |
| 60 | + version.add(TCAPIVersion.fromString(((JsonNode)it.next()).textValue())); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if(jsonNode.hasNonNull("extensions")) { |
| 65 | + extensions = new Extensions(jsonNode.get("extensions")); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public ObjectNode toJSONNode(TCAPIVersion v) { |
| 71 | + ObjectNode result = new ObjectNode(Mapper.getInstance().getNodeFactory()); |
| 72 | + if(!version.isEmpty()){ |
| 73 | + ArrayNode versions = Mapper.getInstance().createArrayNode(); |
| 74 | + for(TCAPIVersion tcapiVersion: this.getVersion()){ |
| 75 | + versions.add(tcapiVersion.toString()); |
| 76 | + } |
| 77 | + result.put("version", versions); |
| 78 | + } |
| 79 | + |
| 80 | + if(extensions != null){ |
| 81 | + result.put("extensions", extensions.toJSONNode()); |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments