package mediawiki.info.wikibase.snaks; import javat.xml.Element; import org.json.JSONException; import org.json.JSONObject; import mediawiki.info.wikibase.ValueSnak; import mediawiki.info.wikibase.WikibaseQuantity; public class QuantitySnak extends ValueSnak<WikibaseQuantity> { public QuantitySnak(WikibaseQuantity value) { super(value); } @Override public JSONObject toJSONObject() throws JSONException { JSONObject o = new JSONObject(); o.put("amount", (getValue().getAmount() >= 0 ? "+" : "")+getValue().getAmount()); o.put("unit", getValue().getUnit()); o.put("upperBound", (getValue().getUpperBound() >= 0 ? "+" : "")+getValue().getUpperBound()); o.put("lowerBound", (getValue().getLowerBound() >= 0 ? "+" : "")+getValue().getLowerBound()); return o; } @Override public Object toReferenceRepresentation() throws JSONException { JSONObject o = new JSONObject(); o.put("type", "quantity"); o.put("value", toJSONObject()); return o; } @Override public Object toClaimRepresentation() throws JSONException { return toJSONObject(); } @Override public void convert(Element e) throws Exception { e = e.getChildren("value").get(0); double amount = Double.parseDouble(e.getAttribute("amount").getValue()); double upperBound = Double.parseDouble(e.getAttribute("upperBound").getValue()); double lowerBound = Double.parseDouble(e.getAttribute("lowerBound").getValue()); String unit = e.getAttribute("unit").getValue(); setValue(new WikibaseQuantity(amount,upperBound,lowerBound,unit)); } @Override public String getDatatype() { return "quantity"; } }